Skip to content

Commit b70f8b6

Browse files
fix(httpcache): try to cover more in unit tests
1 parent 697c6d2 commit b70f8b6

File tree

1 file changed

+61
-7
lines changed

1 file changed

+61
-7
lines changed

src/Symfony/Tests/Doctrine/EventListener/PurgeHttpCacheListenerTest.php

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ public function testOnFlush(): void
102102
$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal());
103103
$listener->onFlush($eventArgs);
104104
$listener->postFlush();
105+
106+
$iriConverterProphecy->getIriFromResource(Argument::type(Dummy::class), UrlGeneratorInterface::ABS_PATH, new GetCollection())->shouldHaveBeenCalled();
107+
$iriConverterProphecy->getIriFromResource(Argument::type(DummyNoGetOperation::class), UrlGeneratorInterface::ABS_PATH, new GetCollection())->shouldHaveBeenCalled();
105108
}
106109

107110
public function testPreUpdate(): void
@@ -172,14 +175,17 @@ public function testNothingToPurge(): void
172175
public function testNotAResourceClass(): void
173176
{
174177
$containNonResource = new ContainNonResource();
175-
$nonResource = new NotAResource('foo', 'bar');
178+
$nonResource1 = new NotAResource('foo', 'bar');
179+
$nonResource2 = new NotAResource('baz', 'qux');
180+
$collectionOfNotAResource = [$nonResource1, $nonResource2];
176181

177182
$purgerProphecy = $this->prophesize(PurgerInterface::class);
178-
$purgerProphecy->purge([])->shouldNotBeCalled();
183+
$purgerProphecy->purge(['/dummies'])->shouldBeCalled();
179184

180185
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
181-
$iriConverterProphecy->getIriFromResource(Argument::type(ContainNonResource::class), UrlGeneratorInterface::ABS_PATH, new GetCollection())->willReturn('/dummies');
182-
$iriConverterProphecy->getIriFromResource($nonResource)->willThrow(new InvalidArgumentException())->shouldBeCalled();
186+
$iriConverterProphecy->getIriFromResource(Argument::type(ContainNonResource::class), UrlGeneratorInterface::ABS_PATH, new GetCollection())->willReturn('/dummies')->shouldBeCalled();
187+
$iriConverterProphecy->getIriFromResource($nonResource1)->willThrow(new InvalidArgumentException())->shouldBeCalled();
188+
$iriConverterProphecy->getIriFromResource($nonResource2)->willThrow(new InvalidArgumentException())->shouldBeCalled();
183189

184190
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
185191
$resourceClassResolverProphecy->isResourceClass(Argument::type('string'))->willReturn(true)->shouldBeCalled();
@@ -203,11 +209,59 @@ public function testNotAResourceClass(): void
203209

204210
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
205211
$propertyAccessorProphecy->isReadable(Argument::type(ContainNonResource::class), 'notAResource')->willReturn(true);
206-
$propertyAccessorProphecy->isReadable(Argument::type(ContainNonResource::class), 'collectionOfNotAResource')->willReturn(false);
207-
$propertyAccessorProphecy->getValue(Argument::type(ContainNonResource::class), 'notAResource')->shouldBeCalled()->willReturn($nonResource);
208-
$propertyAccessorProphecy->getValue(Argument::type(ContainNonResource::class), 'collectionOfNotAResource')->shouldNotBeCalled();
212+
$propertyAccessorProphecy->isReadable(Argument::type(ContainNonResource::class), 'collectionOfNotAResource')->willReturn(true);
213+
$propertyAccessorProphecy->getValue(Argument::type(ContainNonResource::class), 'notAResource')->shouldBeCalled()->willReturn($nonResource1);
214+
$propertyAccessorProphecy->getValue(Argument::type(ContainNonResource::class), 'collectionOfNotAResource')->shouldBeCalled()->willReturn($collectionOfNotAResource);
209215

210216
$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal());
211217
$listener->onFlush($eventArgs);
218+
$listener->postFlush();
219+
}
220+
221+
public function testAddTagsForCollection(): void
222+
{
223+
$dummy1 = new Dummy();
224+
$dummy1->setId(1);
225+
$dummy2 = new Dummy();
226+
$dummy2->setId(2);
227+
$collection = [$dummy1, $dummy2];
228+
229+
$purgerProphecy = $this->prophesize(PurgerInterface::class);
230+
$purgerProphecy->purge(['/dummies', '/dummies/1', '/dummies/2'])->shouldBeCalled();
231+
232+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
233+
$iriConverterProphecy->getIriFromResource(Argument::type(Dummy::class), UrlGeneratorInterface::ABS_PATH, new GetCollection())->willReturn('/dummies')->shouldBeCalled();
234+
$iriConverterProphecy->getIriFromResource($dummy1)->willReturn('/dummies/1')->shouldBeCalled();
235+
$iriConverterProphecy->getIriFromResource($dummy2)->willReturn('/dummies/2')->shouldBeCalled();
236+
237+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
238+
$resourceClassResolverProphecy->isResourceClass(Argument::type('string'))->willReturn(true)->shouldBeCalled();
239+
240+
$dummyWithCollection = new Dummy();
241+
$dummyWithCollection->setId(3);
242+
243+
$uowProphecy = $this->prophesize(UnitOfWork::class);
244+
$uowProphecy->getScheduledEntityInsertions()->willReturn([$dummyWithCollection])->shouldBeCalled();
245+
$uowProphecy->getScheduledEntityUpdates()->willReturn([])->shouldBeCalled();
246+
$uowProphecy->getScheduledEntityDeletions()->willReturn([])->shouldBeCalled();
247+
248+
$emProphecy = $this->prophesize(EntityManagerInterface::class);
249+
$emProphecy->getUnitOfWork()->willReturn($uowProphecy->reveal())->shouldBeCalled();
250+
251+
$dummyClassMetadata = new ClassMetadata(Dummy::class);
252+
// @phpstan-ignore-next-line
253+
$dummyClassMetadata->associationMappings = [
254+
'relatedDummies' => ['targetEntity' => Dummy::class],
255+
];
256+
$emProphecy->getClassMetadata(Dummy::class)->willReturn($dummyClassMetadata)->shouldBeCalled();
257+
$eventArgs = new OnFlushEventArgs($emProphecy->reveal());
258+
259+
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
260+
$propertyAccessorProphecy->isReadable(Argument::type(Dummy::class), 'relatedDummies')->willReturn(true);
261+
$propertyAccessorProphecy->getValue(Argument::type(Dummy::class), 'relatedDummies')->willReturn($collection)->shouldBeCalled();
262+
263+
$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal());
264+
$listener->onFlush($eventArgs);
265+
$listener->postFlush();
212266
}
213267
}

0 commit comments

Comments
 (0)