|
22 | 22 | use ApiPlatform\Exception\ItemNotFoundException;
|
23 | 23 | use ApiPlatform\HttpCache\PurgerInterface;
|
24 | 24 | use ApiPlatform\Metadata\GetCollection;
|
| 25 | +use ApiPlatform\Tests\Fixtures\NotAResource; |
| 26 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ContainNonResource; |
25 | 27 | use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
|
26 | 28 | use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyNoGetOperation;
|
27 | 29 | use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
|
@@ -74,6 +76,7 @@ public function testOnFlush()
|
74 | 76 | $iriConverterProphecy->getIriFromResource(Argument::any())->willThrow(new ItemNotFoundException());
|
75 | 77 |
|
76 | 78 | $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
|
| 79 | + $resourceClassResolverProphecy->isResourceClass(Argument::type('string'))->willReturn(true)->shouldBeCalled(); |
77 | 80 | $resourceClassResolverProphecy->getResourceClass(Argument::type(Dummy::class))->willReturn(Dummy::class)->shouldBeCalled();
|
78 | 81 | $resourceClassResolverProphecy->getResourceClass(Argument::type(DummyNoGetOperation::class))->willReturn(DummyNoGetOperation::class)->shouldBeCalled();
|
79 | 82 |
|
@@ -125,6 +128,7 @@ public function testPreUpdate()
|
125 | 128 | $iriConverterProphecy->getIriFromResource($newRelatedDummy)->willReturn('/related_dummies/new')->shouldBeCalled();
|
126 | 129 |
|
127 | 130 | $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
|
| 131 | + $resourceClassResolverProphecy->isResourceClass(Argument::type('string'))->willReturn(true)->shouldBeCalled(); |
128 | 132 | $resourceClassResolverProphecy->getResourceClass(Argument::type(Dummy::class))->willReturn(Dummy::class)->shouldBeCalled();
|
129 | 133 |
|
130 | 134 | $emProphecy = $this->prophesize(EntityManagerInterface::class);
|
@@ -168,4 +172,45 @@ public function testNothingToPurge()
|
168 | 172 | $listener->preUpdate($eventArgs);
|
169 | 173 | $listener->postFlush();
|
170 | 174 | }
|
| 175 | + |
| 176 | + public function testNotAResourceClass() |
| 177 | + { |
| 178 | + $containNonResource = new ContainNonResource(); |
| 179 | + $nonResource = new NotAResource('foo', 'bar'); |
| 180 | + |
| 181 | + $purgerProphecy = $this->prophesize(PurgerInterface::class); |
| 182 | + $purgerProphecy->purge([])->shouldNotBeCalled(); |
| 183 | + |
| 184 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 185 | + $iriConverterProphecy->getIriFromResource(ContainNonResource::class, UrlGeneratorInterface::ABS_PATH, Argument::any())->willReturn('/dummies/1'); |
| 186 | + $iriConverterProphecy->getIriFromResource($nonResource)->shouldNotBeCalled(); |
| 187 | + |
| 188 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 189 | + $resourceClassResolverProphecy->getResourceClass(Argument::type(ContainNonResource::class))->willReturn(ContainNonResource::class)->shouldBeCalled(); |
| 190 | + $resourceClassResolverProphecy->isResourceClass(NotAResource::class)->willReturn(false)->shouldBeCalled(); |
| 191 | + |
| 192 | + $emProphecy = $this->prophesize(EntityManagerInterface::class); |
| 193 | + |
| 194 | + $uowProphecy = $this->prophesize(UnitOfWork::class); |
| 195 | + $uowProphecy->getScheduledEntityInsertions()->willReturn([$containNonResource])->shouldBeCalled(); |
| 196 | + $uowProphecy->getScheduledEntityUpdates()->willReturn([])->shouldBeCalled(); |
| 197 | + $uowProphecy->getScheduledEntityDeletions()->willReturn([])->shouldBeCalled(); |
| 198 | + |
| 199 | + $emProphecy = $this->prophesize(EntityManagerInterface::class); |
| 200 | + $emProphecy->getUnitOfWork()->willReturn($uowProphecy->reveal())->shouldBeCalled(); |
| 201 | + |
| 202 | + $dummyClassMetadata = new ClassMetadata(ContainNonResource::class); |
| 203 | + $dummyClassMetadata->associationMappings = [ |
| 204 | + 'notAResource' => [], |
| 205 | + ]; |
| 206 | + $emProphecy->getClassMetadata(ContainNonResource::class)->willReturn($dummyClassMetadata); |
| 207 | + $eventArgs = new OnFlushEventArgs($emProphecy->reveal()); |
| 208 | + |
| 209 | + $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class); |
| 210 | + $propertyAccessorProphecy->isReadable(Argument::type(ContainNonResource::class), 'notAResource')->willReturn(true); |
| 211 | + $propertyAccessorProphecy->getValue(Argument::type(ContainNonResource::class), 'notAResource')->shouldBeCalled()->willReturn($nonResource); |
| 212 | + |
| 213 | + $listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal()); |
| 214 | + $listener->onFlush($eventArgs); |
| 215 | + } |
171 | 216 | }
|
0 commit comments