|
18 | 18 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
|
19 | 19 | use Doctrine\Common\Persistence\ManagerRegistry;
|
20 | 20 | use Doctrine\Common\Persistence\ObjectManager;
|
| 21 | +use Doctrine\ORM\Mapping\ClassMetadataInfo; |
21 | 22 | use PHPUnit\Framework\TestCase;
|
| 23 | +use Prophecy\Prediction\CallPrediction; |
| 24 | +use Prophecy\Prediction\NoCallsPrediction; |
22 | 25 |
|
23 | 26 | /**
|
24 | 27 | * @author Baptiste Meyer <[email protected]>
|
@@ -69,6 +72,7 @@ public function testPersistIfEntityAlreadyManaged()
|
69 | 72 | $objectManagerProphecy->persist($dummy)->shouldNotBeCalled();
|
70 | 73 | $objectManagerProphecy->flush()->shouldBeCalled();
|
71 | 74 | $objectManagerProphecy->refresh($dummy)->shouldBeCalled();
|
| 75 | + $objectManagerProphecy->getClassMetadata(Dummy::class)->willReturn(null)->shouldBeCalled(); |
72 | 76 |
|
73 | 77 | $managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
|
74 | 78 | $managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn($objectManagerProphecy->reveal())->shouldBeCalled();
|
@@ -109,4 +113,36 @@ public function testRemoveWithNullManager()
|
109 | 113 |
|
110 | 114 | (new DataPersister($managerRegistryProphecy->reveal()))->remove(new Dummy());
|
111 | 115 | }
|
| 116 | + |
| 117 | + public function getTrackingPolicyParameters() |
| 118 | + { |
| 119 | + return [ |
| 120 | + 'deferred explicit' => [true, true], |
| 121 | + 'deferred implicit' => [false, false], |
| 122 | + ]; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @dataProvider getTrackingPolicyParameters |
| 127 | + */ |
| 128 | + public function testTrackingPolicy($deferredExplicit, $persisted) |
| 129 | + { |
| 130 | + $dummy = new Dummy(); |
| 131 | + |
| 132 | + $classMetadataInfo = $this->prophesize(ClassMetadataInfo::class); |
| 133 | + $classMetadataInfo->isChangeTrackingDeferredExplicit()->willReturn($deferredExplicit)->shouldBeCalled(); |
| 134 | + |
| 135 | + $objectManagerProphecy = $this->prophesize(ObjectManager::class); |
| 136 | + $objectManagerProphecy->getClassMetadata(Dummy::class)->willReturn($classMetadataInfo)->shouldBeCalled(); |
| 137 | + $objectManagerProphecy->contains($dummy)->willReturn(true); |
| 138 | + $objectManagerProphecy->persist($dummy)->should($persisted ? new CallPrediction() : new NoCallsPrediction()); |
| 139 | + $objectManagerProphecy->flush()->shouldBeCalled(); |
| 140 | + $objectManagerProphecy->refresh($dummy)->shouldBeCalled(); |
| 141 | + |
| 142 | + $managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); |
| 143 | + $managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn($objectManagerProphecy)->shouldBeCalled(); |
| 144 | + |
| 145 | + $result = (new DataPersister($managerRegistryProphecy->reveal()))->persist($dummy); |
| 146 | + $this->assertSame($dummy, $result); |
| 147 | + } |
112 | 148 | }
|
0 commit comments