Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
])
->tag('doctrine.event_listener', ['event' => 'preUpdate'])
->tag('doctrine.event_listener', ['event' => 'onFlush'])
->tag('doctrine.event_listener', ['event' => 'postFlush']);
->tag('doctrine.event_listener', ['event' => 'postFlush'])
->tag('kernel.reset', ['method' => 'reset', 'on_invalid' => 'ignore']);
};
13 changes: 11 additions & 2 deletions src/Symfony/Doctrine/EventListener/PurgeHttpCacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* Purges responses containing modified entities from the proxy cache.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class PurgeHttpCacheListener
final class PurgeHttpCacheListener implements ResetInterface
{
use ClassInfoTrait;
private readonly PropertyAccessorInterface $propertyAccessor;
Expand Down Expand Up @@ -122,9 +123,17 @@ public function postFlush(): void
return;
}

$this->purger->purge(array_values($this->tags));
try {
$this->purger->purge(array_values($this->tags));
} finally {
$this->reset();
}
}

public function reset(): void
{
$this->tags = [];
$this->scheduledInsertions = [];
}

private function gatherResourceAndItemTags(object $entity, bool $purgeItem): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,16 @@ public function testMercurePublisherIsReset(): void
$this->assertSame([['method' => 'reset', 'on_invalid' => 'ignore']], $this->container->getDefinition('api_platform.doctrine.orm.listener.mercure.publish')->getTag('kernel.reset'));
}

public function testHttpCachePurgerIsReset(): void
{
$config = self::DEFAULT_CONFIG;
$config['api_platform']['doctrine']['enabled'] = true;
$config['api_platform']['http_cache']['invalidation']['enabled'] = true;
(new ApiPlatformExtension())->load($config, $this->container);

$this->assertSame([['method' => 'reset', 'on_invalid' => 'ignore']], $this->container->getDefinition('api_platform.doctrine.listener.http_cache.purge')->getTag('kernel.reset'));
}

public function testItRegistersMetadataConfiguration(): void
{
$config = self::DEFAULT_CONFIG;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,51 @@ public function testOnFlush(): void
$iriConverterProphecy->getIriFromResource(Argument::type(DummyNoGetOperation::class), UrlGeneratorInterface::ABS_PATH, new GetCollection())->shouldHaveBeenCalled();
}

public function testResetDropsTheTagsCollectedByAFailedFlush(): void
{
$failed = new Dummy();
$failed->setId(1);
$purged = new Dummy();
$purged->setId(2);

$purgerProphecy = $this->prophesize(PurgerInterface::class);
$purgerProphecy->purge(['/dummies', '/dummies/2'])->shouldBeCalledOnce();

$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $this->createIriConverter(), $this->createResourceClassResolver(), $this->createStub(PropertyAccessorInterface::class));

// the commit of the first flush fails: Doctrine dispatches onFlush, but never postFlush
$listener->onFlush($this->createOnFlushEventArgs([$failed]));
// between two requests (or two messages), the services resetter resets the listener
$listener->reset();

$listener->onFlush($this->createOnFlushEventArgs([$purged]));
$listener->postFlush();
}

public function testTagsAreClearedWhenThePurgeFails(): void
{
$first = new Dummy();
$first->setId(1);
$second = new Dummy();
$second->setId(2);

$purgerProphecy = $this->prophesize(PurgerInterface::class);
$purgerProphecy->purge(['/dummies', '/dummies/1'])->willThrow(new \RuntimeException('The cache server is down.'))->shouldBeCalledOnce();
$purgerProphecy->purge(['/dummies', '/dummies/2'])->shouldBeCalledOnce();

$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $this->createIriConverter(), $this->createResourceClassResolver(), $this->createStub(PropertyAccessorInterface::class));

$listener->onFlush($this->createOnFlushEventArgs([$first]));
try {
$listener->postFlush();
$this->fail('The purge failure must be thrown.');
} catch (\RuntimeException) {
}

$listener->onFlush($this->createOnFlushEventArgs([$second]));
$listener->postFlush();
}

public function testPreUpdate(): void
{
$oldRelatedDummy = new RelatedDummy();
Expand Down Expand Up @@ -428,4 +473,35 @@ static function (object $resource, int $referenceType = UrlGeneratorInterface::A
$listener->preUpdate($eventArgs);
$listener->postFlush();
}

private function createIriConverter(): IriConverterInterface
{
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromResource(Argument::type(Dummy::class), UrlGeneratorInterface::ABS_PATH, new GetCollection())->willReturn('/dummies');
$iriConverterProphecy->getIriFromResource(Argument::type(Dummy::class))->will(static fn (array $args): string => '/dummies/'.$args[0]->getId());

return $iriConverterProphecy->reveal();
}

private function createResourceClassResolver(): ResourceClassResolverInterface
{
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->isResourceClass(Argument::type('string'))->willReturn(true);

return $resourceClassResolverProphecy->reveal();
}

private function createOnFlushEventArgs(array $updates): OnFlushEventArgs
{
$uowProphecy = $this->prophesize(UnitOfWork::class);
$uowProphecy->getScheduledEntityInsertions()->willReturn([]);
$uowProphecy->getScheduledEntityUpdates()->willReturn($updates);
$uowProphecy->getScheduledEntityDeletions()->willReturn([]);

$emProphecy = $this->prophesize(EntityManagerInterface::class);
$emProphecy->getUnitOfWork()->willReturn($uowProphecy->reveal());
$emProphecy->getClassMetadata(Dummy::class)->willReturn(new ClassMetadata(Dummy::class));

return new OnFlushEventArgs($emProphecy->reveal());
}
}
Loading