|
16 | 16 | use ApiPlatform\Core\Api\IriConverterInterface;
|
17 | 17 | use ApiPlatform\Core\Api\ResourceClassResolverInterface;
|
18 | 18 | use ApiPlatform\Core\Bridge\Doctrine\EventListener\PurgeHttpCacheListener;
|
| 19 | +use ApiPlatform\Core\Exception\InvalidArgumentException; |
19 | 20 | use ApiPlatform\Core\HttpCache\PurgerInterface;
|
20 | 21 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
|
| 22 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyNoGetOperation; |
21 | 23 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
|
22 | 24 | use Doctrine\ORM\EntityManagerInterface;
|
23 | 25 | use Doctrine\ORM\Event\OnFlushEventArgs;
|
@@ -47,27 +49,34 @@ public function testOnFlush()
|
47 | 49 | $toDelete2 = new Dummy();
|
48 | 50 | $toDelete2->setId(4);
|
49 | 51 |
|
| 52 | + $toDeleteNoPurge = new DummyNoGetOperation(); |
| 53 | + $toDeleteNoPurge->setId(5); |
| 54 | + |
50 | 55 | $purgerProphecy = $this->prophesize(PurgerInterface::class);
|
51 | 56 | $purgerProphecy->purge(['/dummies' => '/dummies', '/dummies/1' => '/dummies/1', '/dummies/2' => '/dummies/2', '/dummies/3' => '/dummies/3', '/dummies/4' => '/dummies/4'])->shouldBeCalled();
|
52 | 57 |
|
53 | 58 | $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
|
54 | 59 | $iriConverterProphecy->getIriFromResourceClass(Dummy::class)->willReturn('/dummies')->shouldBeCalled();
|
| 60 | + $iriConverterProphecy->getIriFromResourceClass(DummyNoGetOperation::class)->willThrow(new InvalidArgumentException())->shouldBeCalled(); |
55 | 61 | $iriConverterProphecy->getIriFromItem($toUpdate1)->willReturn('/dummies/1')->shouldBeCalled();
|
56 | 62 | $iriConverterProphecy->getIriFromItem($toUpdate2)->willReturn('/dummies/2')->shouldBeCalled();
|
57 | 63 | $iriConverterProphecy->getIriFromItem($toDelete1)->willReturn('/dummies/3')->shouldBeCalled();
|
58 | 64 | $iriConverterProphecy->getIriFromItem($toDelete2)->willReturn('/dummies/4')->shouldBeCalled();
|
| 65 | + $iriConverterProphecy->getIriFromItem($toDeleteNoPurge)->shouldNotBeCalled(); |
59 | 66 |
|
60 | 67 | $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
|
61 | 68 | $resourceClassResolverProphecy->getResourceClass(Argument::type(Dummy::class))->willReturn(Dummy::class)->shouldBeCalled();
|
| 69 | + $resourceClassResolverProphecy->getResourceClass(Argument::type(DummyNoGetOperation::class))->willReturn(DummyNoGetOperation::class)->shouldBeCalled(); |
62 | 70 |
|
63 | 71 | $uowProphecy = $this->prophesize(UnitOfWork::class);
|
64 | 72 | $uowProphecy->getScheduledEntityInsertions()->willReturn([$toInsert1, $toInsert2])->shouldBeCalled();
|
65 | 73 | $uowProphecy->getScheduledEntityUpdates()->willReturn([$toUpdate1, $toUpdate2])->shouldBeCalled();
|
66 |
| - $uowProphecy->getScheduledEntityDeletions()->willReturn([$toDelete1, $toDelete2])->shouldBeCalled(); |
| 74 | + $uowProphecy->getScheduledEntityDeletions()->willReturn([$toDelete1, $toDelete2, $toDeleteNoPurge])->shouldBeCalled(); |
67 | 75 |
|
68 | 76 | $emProphecy = $this->prophesize(EntityManagerInterface::class);
|
69 | 77 | $emProphecy->getUnitOfWork()->willReturn($uowProphecy->reveal())->shouldBeCalled();
|
70 | 78 | $emProphecy->getClassMetadata(Dummy::class)->willReturn(new ClassMetadata(Dummy::class))->shouldBeCalled();
|
| 79 | + $emProphecy->getClassMetadata(DummyNoGetOperation::class)->willReturn(new ClassMetadata(DummyNoGetOperation::class))->shouldBeCalled(); |
71 | 80 | $eventArgs = new OnFlushEventArgs($emProphecy->reveal());
|
72 | 81 |
|
73 | 82 | $listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal());
|
@@ -111,4 +120,32 @@ public function testPreUpdate()
|
111 | 120 | $listener->preUpdate($eventArgs);
|
112 | 121 | $listener->postFlush();
|
113 | 122 | }
|
| 123 | + |
| 124 | + public function testNothingToPurge() |
| 125 | + { |
| 126 | + $dummyNoGetOperation = new DummyNoGetOperation(); |
| 127 | + $dummyNoGetOperation->setId(1); |
| 128 | + |
| 129 | + $purgerProphecy = $this->prophesize(PurgerInterface::class); |
| 130 | + $purgerProphecy->purge([])->shouldNotBeCalled(); |
| 131 | + |
| 132 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 133 | + $iriConverterProphecy->getIriFromResourceClass(DummyNoGetOperation::class)->willThrow(new InvalidArgumentException())->shouldBeCalled(); |
| 134 | + $iriConverterProphecy->getIriFromItem($dummyNoGetOperation)->shouldNotBeCalled(); |
| 135 | + |
| 136 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 137 | + $resourceClassResolverProphecy->getResourceClass(Argument::type(DummyNoGetOperation::class))->willReturn(DummyNoGetOperation::class)->shouldBeCalled(); |
| 138 | + |
| 139 | + $emProphecy = $this->prophesize(EntityManagerInterface::class); |
| 140 | + |
| 141 | + $classMetadata = new ClassMetadata(DummyNoGetOperation::class); |
| 142 | + $emProphecy->getClassMetadata(DummyNoGetOperation::class)->willReturn($classMetadata)->shouldBeCalled(); |
| 143 | + |
| 144 | + $changeSet = ['lorem' => 'ipsum']; |
| 145 | + $eventArgs = new PreUpdateEventArgs($dummyNoGetOperation, $emProphecy->reveal(), $changeSet); |
| 146 | + |
| 147 | + $listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal()); |
| 148 | + $listener->preUpdate($eventArgs); |
| 149 | + $listener->postFlush(); |
| 150 | + } |
114 | 151 | }
|
0 commit comments