diff --git a/tests/src/Unit/OGImageManagerTest.php b/tests/src/Unit/OGImageManagerTest.php new file mode 100644 index 000000000..68d70a466 --- /dev/null +++ b/tests/src/Unit/OGImageManagerTest.php @@ -0,0 +1,71 @@ +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; + } + +}