-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\helfi_platform_config\Unit; | ||
|
||
use Drupal\Core\Entity\EntityInterface; | ||
use Drupal\helfi_platform_config\Token\OGImageBuilderInterface; | ||
use Drupal\helfi_platform_config\Token\OGImageManager; | ||
use Drupal\Tests\token\Kernel\UnitTest; | ||
use Prophecy\Argument; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
|
||
/** | ||
* Tests og image builder collector. | ||
* | ||
* @coversDefaultClass \Drupal\helfi_platform_config\Token\OGImageManager | ||
* @group helfi_platform_config | ||
*/ | ||
class OGImageManagerTest extends UnitTest { | ||
|
||
use ProphecyTrait; | ||
|
||
/** | ||
* Tests builder. | ||
* | ||
* @covers ::buildUrl | ||
* @covers ::add | ||
* @covers ::getBuilders | ||
*/ | ||
public function testBuildUrl() : void { | ||
$sut = new OGImageManager(); | ||
$entity = $this->prophesize(EntityInterface::class)->reveal(); | ||
|
||
// First builder does not apply. | ||
$sut->add($this->createImageBuilder('1', FALSE)->reveal()); | ||
$this->assertEquals(NULL, $sut->buildUrl($entity)); | ||
|
||
// Second builder applies but returns NULL. | ||
$sut->add($this->createImageBuilder(NULL)->reveal()); | ||
$this->assertEquals(NULL, $sut->buildUrl($entity)); | ||
|
||
// Third builder applies. | ||
$sut->add($this->createImageBuilder('3')->reveal(), 100); | ||
$this->assertEquals('3', $sut->buildUrl($entity)); | ||
|
||
// Builder with the highest priority gets overwritten by '3'. | ||
$builder4 = $this->createImageBuilder('4'); | ||
$sut->add($builder4->reveal(), 200); | ||
$this->assertEquals('3', $sut->buildUrl($entity)); | ||
$builder4->buildUrl(Argument::any())->shouldHaveBeenCalled(); | ||
} | ||
|
||
/** | ||
* Create image builder mock. | ||
* | ||
* @param string|null $url | ||
* Return value for buildUrl(). | ||
* @param bool $applies | ||
* Return value for applies(). | ||
* | ||
* @return \Drupal\helfi_platform_config\Token\OGImageBuilderInterface|\Prophecy\Prophecy\ObjectProphecy | ||
* Builder mock. | ||
*/ | ||
private function createImageBuilder(?string $url, bool $applies = TRUE) : OGImageBuilderInterface|ObjectProphecy { | ||
$builder = $this->prophesize(OGImageBuilderInterface::class); | ||
$builder->applies(Argument::any())->willReturn($applies); | ||
$builder->buildUrl(Argument::any())->willReturn($url); | ||
return $builder; | ||
} | ||
|
||
} |