From bd5530173e465e1407a83463a8bf15baf13a06d7 Mon Sep 17 00:00:00 2001 From: BacLuc Date: Sun, 7 Sep 2025 00:35:51 +0200 Subject: [PATCH] refactor/api: remove usage of Prophecy in TagCollectorTest api-platform wants to get rid of it, and we don't have a lot of it, so we can remove it easily now. --- api/tests/HttpCache/TagCollectorTest.php | 72 ++++++++++++++++++------ 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/api/tests/HttpCache/TagCollectorTest.php b/api/tests/HttpCache/TagCollectorTest.php index 254c468350..ae2762877c 100644 --- a/api/tests/HttpCache/TagCollectorTest.php +++ b/api/tests/HttpCache/TagCollectorTest.php @@ -7,29 +7,32 @@ use App\HttpCache\ResponseTagger; use App\HttpCache\TagCollector; use App\Tests\HttpCache\Entity\Dummy; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; + +use function PHPUnit\Framework\exactly; +use function PHPUnit\Framework\never; +use function PHPUnit\Framework\once; /** * @internal */ class TagCollectorTest extends TestCase { - use ProphecyTrait; - private TagCollectorInterface $tagCollector; - private ObjectProphecy $responseTaggerProphecy; + private MockObject&ResponseTagger $responseTagger; protected function setUp(): void { // given - $this->responseTaggerProphecy = $this->prophesize(ResponseTagger::class); - $this->tagCollector = new TagCollector($this->responseTaggerProphecy->reveal()); + $this->responseTagger = $this->createMock(ResponseTagger::class); + $this->tagCollector = new TagCollector($this->responseTagger); } public function testNoTagForEmptyContext() { // then - $this->responseTaggerProphecy->addTags(Argument::any())->shouldNotBeCalled(); + $this->responseTagger + ->expects($this->never()) + ->method('addTags') + ; // when $this->tagCollector->collect([]); @@ -37,7 +40,11 @@ public function testNoTagForEmptyContext() { public function testWithIri() { // then - $this->responseTaggerProphecy->addTags(['/test-iri'])->shouldBeCalled(); + $this->responseTagger + ->expects($this->once()) + ->method('addTags') + ->with(['/test-iri']) + ; // when $this->tagCollector->collect(['iri' => '/test-iri']); @@ -49,7 +56,11 @@ public function testWithBaseEntity() { $object->setId('123'); // then - $this->responseTaggerProphecy->addTags(['123'])->shouldBeCalled(); + $this->responseTagger + ->expects(once()) + ->method('addTags') + ->with(['123']) + ; // when $this->tagCollector->collect(['iri' => '/dummy/123', 'object' => $object]); @@ -61,7 +72,11 @@ public function testWithRelation() { $object->setId('123'); // then - $this->responseTaggerProphecy->addTags(['123#propertyName'])->shouldBeCalled(); + $this->responseTagger + ->expects(once()) + ->method('addTags') + ->with(['123#propertyName']) + ; // when $this->tagCollector->collect([ @@ -78,8 +93,19 @@ public function testWithExtraCacheDependency() { $object->setId('123'); // then - $this->responseTaggerProphecy->addTags(['123#PROPERTY_NAME'])->shouldBeCalled(); - $this->responseTaggerProphecy->addTags(['123#OTHER_DEPENDENCY'])->shouldBeCalled(); + $seen = []; + $this->responseTagger + ->expects(exactly(2)) + ->method('addTags') + ->willReturnCallback(function ($tags) use (&$seen) { + $valid = $tags === ['123#PROPERTY_NAME'] || $tags === ['123#OTHER_DEPENDENCY']; + if ($valid) { + $seen[] = $tags[0]; + } + + return $valid; + }) + ; // when $this->tagCollector->collect([ @@ -92,11 +118,17 @@ public function testWithExtraCacheDependency() { ), 'api_attribute' => 'propertyName', ]); + + $this->assertContains('123#PROPERTY_NAME', $seen); + $this->assertContains('123#OTHER_DEPENDENCY', $seen); } public function testNoTagForHalLinks() { // then - $this->responseTaggerProphecy->addTags(Argument::any())->shouldNotBeCalled(); + $this->responseTagger + ->expects(never()) + ->method('addTags') + ; // when $this->tagCollector->collect([ @@ -108,7 +140,10 @@ public function testNoTagForHalLinks() { public function testNoTagForJsonLdLinks() { // then - $this->responseTaggerProphecy->addTags(Argument::any())->shouldNotBeCalled(); + $this->responseTagger + ->expects(never()) + ->method('addTags') + ; // when $this->tagCollector->collect([ @@ -120,7 +155,10 @@ public function testNoTagForJsonLdLinks() { public function testNoTagForJsonApiLinks() { // then - $this->responseTaggerProphecy->addTags(Argument::any())->shouldNotBeCalled(); + $this->responseTagger + ->expects(never()) + ->method('addTags') + ; // when $this->tagCollector->collect([