|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Symfony\Tests\Doctrine\EventListener; |
| 15 | + |
| 16 | +use ApiPlatform\HttpCache\PurgerInterface; |
| 17 | +use ApiPlatform\Metadata\ApiResource; |
| 18 | +use ApiPlatform\Metadata\Get; |
| 19 | +use ApiPlatform\Metadata\GetCollection; |
| 20 | +use ApiPlatform\Metadata\IriConverterInterface; |
| 21 | +use ApiPlatform\Metadata\Operation; |
| 22 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 23 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 24 | +use ApiPlatform\Metadata\ResourceClassResolverInterface; |
| 25 | +use ApiPlatform\Metadata\UrlGeneratorInterface; |
| 26 | +use ApiPlatform\Symfony\Doctrine\EventListener\PurgeHttpCacheListener; |
| 27 | +use ApiPlatform\Symfony\Tests\Fixtures\TestBundle\Entity\Dummy; |
| 28 | +use Doctrine\ORM\EntityManagerInterface; |
| 29 | +use Doctrine\ORM\Event\OnFlushEventArgs; |
| 30 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 31 | +use Doctrine\ORM\UnitOfWork; |
| 32 | +use PHPUnit\Framework\TestCase; |
| 33 | + |
| 34 | +/** |
| 35 | + * Non-regression tests for the multi-operation purge fix. |
| 36 | + * |
| 37 | + * Uses native PHPUnit mocks (no Prophecy) to keep expectations close to the |
| 38 | + * actual call shape. |
| 39 | + */ |
| 40 | +final class PurgeHttpCacheListenerMultipleOperationsTest extends TestCase |
| 41 | +{ |
| 42 | + /** |
| 43 | + * (b) When several #[GetCollection] are declared on a resource, every |
| 44 | + * collection URI must be added to the purge tags. |
| 45 | + */ |
| 46 | + public function testPurgesAllCollectionUrisOnInsert(): void |
| 47 | + { |
| 48 | + $entity = new Dummy(); |
| 49 | + |
| 50 | + $getCollection1 = new GetCollection(uriTemplate: '/dummies'); |
| 51 | + $getCollection2 = new GetCollection(uriTemplate: '/dummies/featured'); |
| 52 | + |
| 53 | + $purger = $this->createMock(PurgerInterface::class); |
| 54 | + $purger->expects(self::once())->method('purge') |
| 55 | + ->with(self::callback(function (array $iris) { |
| 56 | + self::assertContains('/dummies', $iris); |
| 57 | + self::assertContains('/dummies/featured', $iris); |
| 58 | + |
| 59 | + return true; |
| 60 | + })); |
| 61 | + |
| 62 | + $iriConverter = $this->createStub(IriConverterInterface::class); |
| 63 | + $iriConverter->method('getIriFromResource') |
| 64 | + ->willReturnCallback(static function (object|string $resource, int $referenceType, ?Operation $operation): string { |
| 65 | + self::assertSame(UrlGeneratorInterface::ABS_PATH, $referenceType); |
| 66 | + self::assertInstanceOf(GetCollection::class, $operation); |
| 67 | + |
| 68 | + return $operation->getUriTemplate(); |
| 69 | + }); |
| 70 | + |
| 71 | + $listener = new PurgeHttpCacheListener( |
| 72 | + $purger, |
| 73 | + $iriConverter, |
| 74 | + $this->mockResourceClassResolver(), |
| 75 | + null, |
| 76 | + null, |
| 77 | + null, |
| 78 | + $this->mockMetadataFactory([$getCollection1, $getCollection2]), |
| 79 | + ); |
| 80 | + |
| 81 | + $listener->onFlush($this->onFlushArgs([$entity], [], [])); |
| 82 | + $listener->postFlush(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * (b) When several #[Get] are declared on a resource, every item URI must |
| 87 | + * be added to the purge tags on update. |
| 88 | + */ |
| 89 | + public function testPurgesAllItemUrisOnUpdate(): void |
| 90 | + { |
| 91 | + $entity = new Dummy(); |
| 92 | + $entity->setId(7); |
| 93 | + |
| 94 | + $get1 = new Get(uriTemplate: '/dummies/{id}'); |
| 95 | + $get2 = new Get(uriTemplate: '/dummies/{id}/details'); |
| 96 | + |
| 97 | + $purger = $this->createMock(PurgerInterface::class); |
| 98 | + $purger->expects(self::once())->method('purge') |
| 99 | + ->with(self::callback(function (array $iris) { |
| 100 | + self::assertContains('/dummies/7', $iris); |
| 101 | + self::assertContains('/dummies/7/details', $iris); |
| 102 | + |
| 103 | + return true; |
| 104 | + })); |
| 105 | + |
| 106 | + $iriConverter = $this->createStub(IriConverterInterface::class); |
| 107 | + $iriConverter->method('getIriFromResource') |
| 108 | + ->willReturnCallback(static function (object|string $resource, int $referenceType, ?Operation $operation): string { |
| 109 | + self::assertSame(UrlGeneratorInterface::ABS_PATH, $referenceType); |
| 110 | + self::assertNotNull($operation); |
| 111 | + |
| 112 | + return str_replace('{id}', '7', (string) $operation->getUriTemplate()); |
| 113 | + }); |
| 114 | + |
| 115 | + $listener = new PurgeHttpCacheListener( |
| 116 | + $purger, |
| 117 | + $iriConverter, |
| 118 | + $this->mockResourceClassResolver(), |
| 119 | + null, |
| 120 | + null, |
| 121 | + null, |
| 122 | + $this->mockMetadataFactory([new GetCollection(uriTemplate: '/dummies'), $get1, $get2]), |
| 123 | + ); |
| 124 | + |
| 125 | + $listener->onFlush($this->onFlushArgs([], [$entity], [])); |
| 126 | + $listener->postFlush(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * (a) BC regression: when the optional ResourceMetadataCollectionFactory |
| 131 | + * is not injected (legacy wiring or manual instantiation), the listener |
| 132 | + * must still emit item tags alongside the collection tag — the previous |
| 133 | + * iteration of the fix lost item tags in the null-factory fallback. |
| 134 | + */ |
| 135 | + public function testBcFallbackStillEmitsItemTagsOnUpdate(): void |
| 136 | + { |
| 137 | + $entity = new Dummy(); |
| 138 | + $entity->setId(11); |
| 139 | + |
| 140 | + $purger = $this->createMock(PurgerInterface::class); |
| 141 | + $purger->expects(self::once())->method('purge') |
| 142 | + ->with(self::callback(function (array $iris) { |
| 143 | + self::assertContains('/dummies', $iris, 'collection IRI must be present in BC fallback'); |
| 144 | + self::assertContains('/dummies/11', $iris, 'item IRI must still be emitted when no metadata factory is wired'); |
| 145 | + |
| 146 | + return true; |
| 147 | + })); |
| 148 | + |
| 149 | + $iriConverter = $this->createStub(IriConverterInterface::class); |
| 150 | + $iriConverter->method('getIriFromResource') |
| 151 | + ->willReturnCallback(static function (object|string $resource, int $referenceType = UrlGeneratorInterface::ABS_PATH, ?Operation $operation = null): string { |
| 152 | + if ($operation instanceof GetCollection) { |
| 153 | + return '/dummies'; |
| 154 | + } |
| 155 | + |
| 156 | + return '/dummies/11'; |
| 157 | + }); |
| 158 | + |
| 159 | + $listener = new PurgeHttpCacheListener( |
| 160 | + $purger, |
| 161 | + $iriConverter, |
| 162 | + $this->mockResourceClassResolver(), |
| 163 | + null, |
| 164 | + null, |
| 165 | + null, |
| 166 | + null, // no metadata factory → BC path |
| 167 | + ); |
| 168 | + |
| 169 | + $listener->onFlush($this->onFlushArgs([], [$entity], [])); |
| 170 | + $listener->postFlush(); |
| 171 | + } |
| 172 | + |
| 173 | + private function mockResourceClassResolver(): ResourceClassResolverInterface |
| 174 | + { |
| 175 | + $resolver = $this->createStub(ResourceClassResolverInterface::class); |
| 176 | + $resolver->method('isResourceClass')->willReturn(true); |
| 177 | + $resolver->method('getResourceClass')->willReturn(Dummy::class); |
| 178 | + |
| 179 | + return $resolver; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @param Operation[] $operations |
| 184 | + */ |
| 185 | + private function mockMetadataFactory(array $operations): ResourceMetadataCollectionFactoryInterface |
| 186 | + { |
| 187 | + $factory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class); |
| 188 | + $factory->method('create')->willReturn( |
| 189 | + new ResourceMetadataCollection(Dummy::class, [new ApiResource(operations: $operations)]) |
| 190 | + ); |
| 191 | + |
| 192 | + return $factory; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @param array<int, object> $insertions |
| 197 | + * @param array<int, object> $updates |
| 198 | + * @param array<int, object> $deletions |
| 199 | + */ |
| 200 | + private function onFlushArgs(array $insertions, array $updates, array $deletions): OnFlushEventArgs |
| 201 | + { |
| 202 | + $uow = $this->createStub(UnitOfWork::class); |
| 203 | + $uow->method('getScheduledEntityInsertions')->willReturn($insertions); |
| 204 | + $uow->method('getScheduledEntityUpdates')->willReturn($updates); |
| 205 | + $uow->method('getScheduledEntityDeletions')->willReturn($deletions); |
| 206 | + |
| 207 | + $classMetadata = new ClassMetadata(Dummy::class); |
| 208 | + // @phpstan-ignore-next-line |
| 209 | + $classMetadata->associationMappings = []; |
| 210 | + |
| 211 | + $em = $this->createStub(EntityManagerInterface::class); |
| 212 | + $em->method('getUnitOfWork')->willReturn($uow); |
| 213 | + $em->method('getClassMetadata')->willReturn($classMetadata); |
| 214 | + |
| 215 | + return new OnFlushEventArgs($em); |
| 216 | + } |
| 217 | +} |
0 commit comments