diff --git a/src/Symfony/Bundle/Resources/config/doctrine_orm_http_cache_purger.php b/src/Symfony/Bundle/Resources/config/doctrine_orm_http_cache_purger.php index bbb221b549b..a01a9cc7028 100644 --- a/src/Symfony/Bundle/Resources/config/doctrine_orm_http_cache_purger.php +++ b/src/Symfony/Bundle/Resources/config/doctrine_orm_http_cache_purger.php @@ -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']); }; diff --git a/src/Symfony/Doctrine/EventListener/PurgeHttpCacheListener.php b/src/Symfony/Doctrine/EventListener/PurgeHttpCacheListener.php index b29c52f0a1f..82d04d20394 100644 --- a/src/Symfony/Doctrine/EventListener/PurgeHttpCacheListener.php +++ b/src/Symfony/Doctrine/EventListener/PurgeHttpCacheListener.php @@ -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 */ -final class PurgeHttpCacheListener +final class PurgeHttpCacheListener implements ResetInterface { use ClassInfoTrait; private readonly PropertyAccessorInterface $propertyAccessor; @@ -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 diff --git a/src/Symfony/Tests/Bundle/DependencyInjection/ApiPlatformExtensionTest.php b/src/Symfony/Tests/Bundle/DependencyInjection/ApiPlatformExtensionTest.php index f04f7ffc510..cadea68ef69 100644 --- a/src/Symfony/Tests/Bundle/DependencyInjection/ApiPlatformExtensionTest.php +++ b/src/Symfony/Tests/Bundle/DependencyInjection/ApiPlatformExtensionTest.php @@ -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; diff --git a/src/Symfony/Tests/Doctrine/EventListener/PurgeHttpCacheListenerTest.php b/src/Symfony/Tests/Doctrine/EventListener/PurgeHttpCacheListenerTest.php index a78eda245f9..7109fb4649e 100644 --- a/src/Symfony/Tests/Doctrine/EventListener/PurgeHttpCacheListenerTest.php +++ b/src/Symfony/Tests/Doctrine/EventListener/PurgeHttpCacheListenerTest.php @@ -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(); @@ -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()); + } }