Skip to content

Commit

Permalink
UHF-9712: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Mar 12, 2024
1 parent a6eb3fd commit 73c4b53
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tests/src/Unit/OGImageManagerTest.php
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;
}

}

0 comments on commit 73c4b53

Please sign in to comment.