1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\Tests\ORM\Cache\Persister\Collection; |
||
6 | |||
7 | use Doctrine\Common\Collections\ArrayCollection; |
||
8 | use Doctrine\ORM\Cache\Persister\CachedPersister; |
||
9 | use Doctrine\ORM\Cache\Persister\Collection\AbstractCollectionPersister; |
||
10 | use Doctrine\ORM\Cache\Persister\Collection\CachedCollectionPersister; |
||
11 | use Doctrine\ORM\Cache\Region; |
||
12 | use Doctrine\ORM\EntityManagerInterface; |
||
13 | use Doctrine\ORM\Mapping\AssociationMetadata; |
||
14 | use Doctrine\ORM\PersistentCollection; |
||
15 | use Doctrine\ORM\Persisters\Collection\CollectionPersister; |
||
16 | use Doctrine\Tests\OrmTestCase; |
||
17 | use Doctrine\Tests\Models\Cache\State; |
||
18 | |||
19 | /** |
||
20 | * @group DDC-2183 |
||
21 | */ |
||
22 | abstract class AbstractCollectionPersisterTest extends OrmTestCase |
||
23 | { |
||
24 | /** |
||
25 | * @var Region |
||
26 | */ |
||
27 | protected $region; |
||
28 | |||
29 | /** |
||
30 | * @var CollectionPersister |
||
31 | */ |
||
32 | protected $collectionPersister; |
||
33 | |||
34 | /** |
||
35 | * @var EntityManagerInterface |
||
36 | */ |
||
37 | protected $em; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $regionMockMethods = [ |
||
43 | 'getName', |
||
44 | 'contains', |
||
45 | 'get', |
||
46 | 'getMultiple', |
||
47 | 'put', |
||
48 | 'evict', |
||
49 | 'evictAll' |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $collectionPersisterMockMethods = [ |
||
56 | 'delete', |
||
57 | 'update', |
||
58 | 'count', |
||
59 | 'slice', |
||
60 | 'contains', |
||
61 | 'containsKey', |
||
62 | 'removeElement', |
||
63 | 'removeKey', |
||
64 | 'get', |
||
65 | 'getMultiple', |
||
66 | 'loadCriteria' |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * @param EntityManagerInterface $em |
||
71 | * @param CollectionPersister $persister |
||
72 | * @param Region $region |
||
73 | * @param AssociationMetadata $association |
||
74 | * |
||
75 | * @return AbstractCollectionPersister |
||
76 | */ |
||
77 | abstract protected function createPersister( |
||
78 | EntityManagerInterface $em, |
||
79 | CollectionPersister $persister, |
||
80 | Region $region, |
||
81 | AssociationMetadata $association |
||
82 | ); |
||
83 | |||
84 | protected function setUp() |
||
85 | { |
||
86 | $this->getSharedSecondLevelCacheDriverImpl()->flushAll(); |
||
87 | $this->enableSecondLevelCache(); |
||
88 | parent::setUp(); |
||
89 | |||
90 | $this->em = $this->getTestEntityManager(); |
||
91 | $this->region = $this->createRegion(); |
||
92 | $this->collectionPersister = $this->getMockBuilder(CollectionPersister::class) |
||
1 ignored issue
–
show
|
|||
93 | ->setMethods($this->collectionPersisterMockMethods) |
||
94 | ->getMock(); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return \Doctrine\ORM\Cache\Region |
||
99 | */ |
||
100 | protected function createRegion() |
||
101 | { |
||
102 | return $this->getMockBuilder(Region::class) |
||
1 ignored issue
–
show
|
|||
103 | ->setMethods($this->regionMockMethods) |
||
104 | ->getMock(); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return \Doctrine\ORM\PersistentCollection |
||
109 | */ |
||
110 | protected function createCollection($owner, $assoc = null, $class = null, $elements = null) |
||
111 | { |
||
112 | $em = $this->em; |
||
113 | $class = $class ?: $this->em->getClassMetadata(State::class); |
||
114 | $assoc = $assoc ?: $class->getProperty('cities'); |
||
115 | $coll = new PersistentCollection($em, $class, $elements ?: new ArrayCollection); |
||
116 | |||
117 | $coll->setOwner($owner, $assoc); |
||
118 | $coll->setInitialized(true); |
||
119 | |||
120 | return $coll; |
||
121 | } |
||
122 | |||
123 | protected function createPersisterDefault() |
||
124 | { |
||
125 | $assoc = $this->em->getClassMetadata(State::class)->getProperty('cities'); |
||
126 | |||
127 | return $this->createPersister($this->em, $this->collectionPersister, $this->region, $assoc); |
||
128 | } |
||
129 | |||
130 | public function testImplementsEntityPersister() |
||
131 | { |
||
132 | $persister = $this->createPersisterDefault(); |
||
133 | |||
134 | self::assertInstanceOf(CollectionPersister::class, $persister); |
||
135 | self::assertInstanceOf(CachedPersister::class, $persister); |
||
136 | self::assertInstanceOf(CachedCollectionPersister::class, $persister); |
||
137 | } |
||
138 | |||
139 | public function testInvokeDelete() |
||
140 | { |
||
141 | $entity = new State("Foo"); |
||
142 | $persister = $this->createPersisterDefault(); |
||
143 | $collection = $this->createCollection($entity); |
||
144 | |||
145 | $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']); |
||
146 | |||
147 | $this->collectionPersister->expects($this->once()) |
||
148 | ->method('delete') |
||
149 | ->with($this->equalTo($collection)); |
||
150 | |||
151 | self::assertNull($persister->delete($collection)); |
||
152 | } |
||
153 | |||
154 | public function testInvokeUpdate() |
||
155 | { |
||
156 | $entity = new State("Foo"); |
||
157 | $persister = $this->createPersisterDefault(); |
||
158 | $collection = $this->createCollection($entity); |
||
159 | |||
160 | $collection->setDirty(true); |
||
161 | |||
162 | $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']); |
||
163 | |||
164 | $this->collectionPersister->expects($this->once()) |
||
165 | ->method('update') |
||
166 | ->with($this->equalTo($collection)); |
||
167 | |||
168 | self::assertNull($persister->update($collection)); |
||
169 | } |
||
170 | |||
171 | public function testInvokeCount() |
||
172 | { |
||
173 | $entity = new State("Foo"); |
||
174 | $persister = $this->createPersisterDefault(); |
||
175 | $collection = $this->createCollection($entity); |
||
176 | |||
177 | $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']); |
||
178 | |||
179 | $this->collectionPersister->expects($this->once()) |
||
180 | ->method('count') |
||
181 | ->with($this->equalTo($collection)) |
||
182 | ->will($this->returnValue(0)); |
||
183 | |||
184 | self::assertEquals(0, $persister->count($collection)); |
||
185 | } |
||
186 | |||
187 | public function testInvokeSlice() |
||
188 | { |
||
189 | $entity = new State("Foo"); |
||
190 | $persister = $this->createPersisterDefault(); |
||
191 | $collection = $this->createCollection($entity); |
||
192 | $slice = $this->createCollection($entity); |
||
193 | |||
194 | $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']); |
||
195 | |||
196 | $this->collectionPersister->expects($this->once()) |
||
197 | ->method('slice') |
||
198 | ->with($this->equalTo($collection), $this->equalTo(1), $this->equalTo(2)) |
||
199 | ->will($this->returnValue($slice)); |
||
200 | |||
201 | self::assertEquals($slice, $persister->slice($collection, 1, 2)); |
||
202 | } |
||
203 | |||
204 | public function testInvokeContains() |
||
205 | { |
||
206 | $entity = new State("Foo"); |
||
207 | $element = new State("Bar"); |
||
208 | $persister = $this->createPersisterDefault(); |
||
209 | $collection = $this->createCollection($entity); |
||
210 | |||
211 | $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']); |
||
212 | |||
213 | $this->collectionPersister->expects($this->once()) |
||
214 | ->method('contains') |
||
215 | ->with($this->equalTo($collection), $this->equalTo($element)) |
||
216 | ->will($this->returnValue(false)); |
||
217 | |||
218 | self::assertFalse($persister->contains($collection, $element)); |
||
219 | } |
||
220 | |||
221 | public function testInvokeContainsKey() |
||
222 | { |
||
223 | $entity = new State("Foo"); |
||
224 | $persister = $this->createPersisterDefault(); |
||
225 | $collection = $this->createCollection($entity); |
||
226 | |||
227 | $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']); |
||
228 | |||
229 | $this->collectionPersister->expects($this->once()) |
||
230 | ->method('containsKey') |
||
231 | ->with($this->equalTo($collection), $this->equalTo(0)) |
||
232 | ->will($this->returnValue(false)); |
||
233 | |||
234 | self::assertFalse($persister->containsKey($collection, 0)); |
||
235 | } |
||
236 | |||
237 | public function testInvokeRemoveElement() |
||
238 | { |
||
239 | $entity = new State("Foo"); |
||
240 | $element = new State("Bar"); |
||
241 | $persister = $this->createPersisterDefault(); |
||
242 | $collection = $this->createCollection($entity); |
||
243 | |||
244 | $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']); |
||
245 | |||
246 | $this->collectionPersister->expects($this->once()) |
||
247 | ->method('removeElement') |
||
248 | ->with($this->equalTo($collection), $this->equalTo($element)) |
||
249 | ->will($this->returnValue(false)); |
||
250 | |||
251 | self::assertFalse($persister->removeElement($collection, $element)); |
||
252 | } |
||
253 | |||
254 | public function testInvokeGet() |
||
255 | { |
||
256 | $entity = new State("Foo"); |
||
257 | $element = new State("Bar"); |
||
258 | $persister = $this->createPersisterDefault(); |
||
259 | $collection = $this->createCollection($entity); |
||
260 | |||
261 | $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']); |
||
262 | |||
263 | $this->collectionPersister->expects($this->once()) |
||
264 | ->method('get') |
||
265 | ->with($this->equalTo($collection), $this->equalTo(0)) |
||
266 | ->will($this->returnValue($element)); |
||
267 | |||
268 | self::assertEquals($element, $persister->get($collection, 0)); |
||
269 | } |
||
270 | } |
||
271 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..