1 | <?php |
||
40 | final class ModelManagerTest extends TestCase |
||
41 | { |
||
42 | /** |
||
43 | * @var PropertyAccessor |
||
44 | */ |
||
45 | private $propertyAccessor; |
||
46 | |||
47 | /** |
||
48 | * @var Stub&ManagerRegistry |
||
49 | */ |
||
50 | private $registry; |
||
51 | |||
52 | protected function setUp(): void |
||
53 | { |
||
54 | parent::setUp(); |
||
55 | |||
56 | $this->registry = $this->createStub(ManagerRegistry::class); |
||
57 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @dataProvider getWrongDocuments |
||
62 | * |
||
63 | * @param mixed $document |
||
64 | */ |
||
65 | public function testNormalizedIdentifierException($document): void |
||
66 | { |
||
67 | $manager = new ModelManager($this->registry, $this->propertyAccessor); |
||
68 | |||
69 | $this->expectException(\RuntimeException::class); |
||
70 | |||
71 | $manager->getNormalizedIdentifier($document); |
||
72 | } |
||
73 | |||
74 | public function getWrongDocuments(): iterable |
||
75 | { |
||
76 | yield [0]; |
||
77 | yield [1]; |
||
78 | yield [false]; |
||
79 | yield [true]; |
||
80 | yield [[]]; |
||
81 | yield ['']; |
||
82 | yield ['sonata-project']; |
||
83 | } |
||
84 | |||
85 | public function testGetNormalizedIdentifierNull(): void |
||
86 | { |
||
87 | $manager = new ModelManager($this->registry, $this->propertyAccessor); |
||
88 | |||
89 | $this->assertNull($manager->getNormalizedIdentifier(null)); |
||
|
|||
90 | } |
||
91 | |||
92 | public function testSortParameters(): void |
||
93 | { |
||
94 | $manager = new ModelManager($this->registry, $this->propertyAccessor); |
||
95 | |||
96 | $datagrid1 = $this->createStub(Datagrid::class); |
||
97 | $datagrid2 = $this->createStub(Datagrid::class); |
||
98 | |||
99 | $field1 = new FieldDescription(); |
||
100 | $field1->setName('field1'); |
||
101 | |||
102 | $field2 = new FieldDescription(); |
||
103 | $field2->setName('field2'); |
||
104 | |||
105 | $field3 = new FieldDescription(); |
||
106 | $field3->setName('field3'); |
||
107 | $field3->setOption('sortable', 'field3sortBy'); |
||
108 | |||
109 | $datagrid1 |
||
110 | ->method('getValues') |
||
111 | ->willReturn([ |
||
112 | '_sort_by' => $field1, |
||
113 | '_sort_order' => 'ASC', |
||
114 | ]); |
||
115 | |||
116 | $datagrid2 |
||
117 | ->method('getValues') |
||
118 | ->willReturn([ |
||
119 | '_sort_by' => $field3, |
||
120 | '_sort_order' => 'ASC', |
||
121 | ]); |
||
122 | |||
123 | $parameters = $manager->getSortParameters($field1, $datagrid1); |
||
124 | |||
125 | $this->assertSame('DESC', $parameters['filter']['_sort_order']); |
||
126 | $this->assertSame('field1', $parameters['filter']['_sort_by']); |
||
127 | |||
128 | $parameters = $manager->getSortParameters($field2, $datagrid1); |
||
129 | |||
130 | $this->assertSame('ASC', $parameters['filter']['_sort_order']); |
||
131 | $this->assertSame('field2', $parameters['filter']['_sort_by']); |
||
132 | |||
133 | $parameters = $manager->getSortParameters($field3, $datagrid1); |
||
134 | |||
135 | $this->assertSame('ASC', $parameters['filter']['_sort_order']); |
||
136 | $this->assertSame('field3sortBy', $parameters['filter']['_sort_by']); |
||
137 | |||
138 | $parameters = $manager->getSortParameters($field3, $datagrid2); |
||
139 | |||
140 | $this->assertSame('DESC', $parameters['filter']['_sort_order']); |
||
141 | $this->assertSame('field3sortBy', $parameters['filter']['_sort_by']); |
||
142 | } |
||
143 | |||
144 | public function testGetParentMetadataForProperty(): void |
||
145 | { |
||
146 | $containerDocumentClass = ContainerDocument::class; |
||
147 | $associatedDocumentClass = AssociatedDocument::class; |
||
148 | $embeddedDocumentClass = EmbeddedDocument::class; |
||
149 | |||
150 | $dm = $this->createStub(DocumentManager::class); |
||
151 | |||
152 | $modelManager = new ModelManager($this->registry, $this->propertyAccessor); |
||
153 | |||
154 | $this->registry |
||
155 | ->method('getManagerForClass') |
||
156 | ->willReturn($dm); |
||
157 | |||
158 | $metadataFactory = $this->createStub(ClassMetadataFactory::class); |
||
159 | |||
160 | $dm |
||
161 | ->method('getMetadataFactory') |
||
162 | ->willReturn($metadataFactory); |
||
163 | |||
164 | $containerDocumentMetadata = $this->getMetadataForContainerDocument(); |
||
165 | $associatedDocumentMetadata = $this->getMetadataForAssociatedDocument(); |
||
166 | $embeddedDocumentMetadata = $this->getMetadataForEmbeddedDocument(); |
||
167 | |||
168 | $metadataFactory->method('getMetadataFor') |
||
169 | ->willReturnMap( |
||
170 | [ |
||
171 | [$containerDocumentClass, $containerDocumentMetadata], |
||
172 | [$embeddedDocumentClass, $embeddedDocumentMetadata], |
||
173 | [$associatedDocumentClass, $associatedDocumentMetadata], |
||
174 | ] |
||
175 | ); |
||
176 | |||
177 | /** @var ClassMetadata $metadata */ |
||
178 | [$metadata, $lastPropertyName] = $modelManager |
||
179 | ->getParentMetadataForProperty($containerDocumentClass, 'plainField'); |
||
180 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'integer'); |
||
181 | |||
182 | [$metadata, $lastPropertyName] = $modelManager |
||
183 | ->getParentMetadataForProperty($containerDocumentClass, 'associatedDocument.plainField'); |
||
184 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'string'); |
||
185 | |||
186 | [$metadata, $lastPropertyName] = $modelManager |
||
187 | ->getParentMetadataForProperty($containerDocumentClass, 'embeddedDocument.plainField'); |
||
188 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
189 | |||
190 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
191 | } |
||
192 | |||
193 | public function testModelReverseTransformWithSetter(): void |
||
194 | { |
||
195 | $class = SimpleDocument::class; |
||
196 | |||
197 | $manager = $this->createModelManagerForClass($class); |
||
198 | $object = $manager->modelReverseTransform( |
||
199 | $class, |
||
200 | [ |
||
201 | 'schmeckles' => 42, |
||
202 | 'multi_word_property' => 'hello', |
||
203 | 'schwifty' => true, |
||
204 | ] |
||
205 | ); |
||
206 | $this->assertInstanceOf($class, $object); |
||
207 | $this->assertSame(42, $object->getSchmeckles()); |
||
208 | $this->assertSame('hello', $object->getMultiWordProperty()); |
||
209 | $this->assertTrue($object->schwifty); |
||
210 | } |
||
211 | |||
212 | public function testModelReverseTransformFailsWithPrivateSetter(): void |
||
213 | { |
||
214 | $class = SimpleDocumentWithPrivateSetter::class; |
||
215 | $manager = $this->createModelManagerForClass($class); |
||
216 | |||
217 | $this->expectException(NoSuchPropertyException::class); |
||
218 | |||
219 | $manager->modelReverseTransform($class, ['schmeckles' => 42]); |
||
220 | } |
||
221 | |||
222 | public function testModelReverseTransformFailsWithPrivateProperties(): void |
||
223 | { |
||
224 | $class = SimpleDocument::class; |
||
225 | $manager = $this->createModelManagerForClass($class); |
||
226 | |||
227 | $this->expectException(NoSuchPropertyException::class); |
||
228 | |||
229 | $manager->modelReverseTransform($class, ['plumbus' => 42]); |
||
230 | } |
||
231 | |||
232 | public function testModelReverseTransformFailsWithPrivateProperties2(): void |
||
233 | { |
||
234 | $class = SimpleDocument::class; |
||
235 | $manager = $this->createModelManagerForClass($class); |
||
236 | |||
237 | $this->expectException(NoSuchPropertyException::class); |
||
238 | |||
239 | $manager->modelReverseTransform($class, ['plumbus' => 42]); |
||
240 | } |
||
241 | |||
242 | public function testCollections(): void |
||
243 | { |
||
244 | $model = new ModelManager($this->registry, $this->propertyAccessor); |
||
245 | |||
246 | $collection = $model->getModelCollectionInstance('whyDoWeEvenHaveThisParameter'); |
||
247 | $this->assertInstanceOf(ArrayCollection::class, $collection); |
||
248 | |||
249 | $item1 = new \stdClass(); |
||
250 | $item2 = new \stdClass(); |
||
251 | $model->collectionAddElement($collection, $item1); |
||
252 | $model->collectionAddElement($collection, $item2); |
||
253 | |||
254 | $this->assertTrue($model->collectionHasElement($collection, $item1)); |
||
255 | |||
256 | $model->collectionRemoveElement($collection, $item1); |
||
257 | |||
258 | $this->assertFalse($model->collectionHasElement($collection, $item1)); |
||
259 | |||
260 | $model->collectionClear($collection); |
||
261 | |||
262 | $this->assertTrue($collection->isEmpty()); |
||
263 | } |
||
264 | |||
265 | public function testModelTransform(): void |
||
266 | { |
||
267 | $model = new ModelManager($this->registry, $this->propertyAccessor); |
||
268 | |||
269 | $instance = new \stdClass(); |
||
270 | $result = $model->modelTransform('thisIsNotUsed', $instance); |
||
271 | |||
272 | $this->assertSame($instance, $result); |
||
273 | } |
||
274 | |||
275 | public function testGetPaginationParameters(): void |
||
276 | { |
||
277 | $datagrid = $this->createMock(DatagridInterface::class); |
||
278 | $fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
279 | |||
280 | $datagrid->expects($this->once()) |
||
281 | ->method('getValues') |
||
282 | ->willReturn(['_sort_by' => $fieldDescription]); |
||
283 | |||
284 | $fieldDescription->expects($this->once()) |
||
285 | ->method('getName') |
||
286 | ->willReturn($name = 'test'); |
||
287 | |||
288 | $model = new ModelManager($this->registry, $this->propertyAccessor); |
||
289 | |||
290 | $result = $model->getPaginationParameters($datagrid, $page = 5); |
||
291 | |||
292 | $this->assertSame($page, $result['filter']['_page']); |
||
293 | $this->assertSame($name, $result['filter']['_sort_by']); |
||
294 | } |
||
295 | |||
296 | public function testGetModelInstanceException(): void |
||
297 | { |
||
298 | $model = new ModelManager($this->registry, $this->propertyAccessor); |
||
299 | |||
300 | $this->expectException(\InvalidArgumentException::class); |
||
301 | |||
302 | $model->getModelInstance(AbstractDocument::class); |
||
303 | } |
||
304 | |||
305 | public function testGetModelInstanceForProtectedDocument(): void |
||
306 | { |
||
307 | $model = new ModelManager($this->registry, $this->propertyAccessor); |
||
308 | |||
309 | $this->assertInstanceOf(ProtectedDocument::class, $model->getModelInstance(ProtectedDocument::class)); |
||
310 | } |
||
311 | |||
312 | public function testFindBadId(): void |
||
313 | { |
||
314 | $model = new ModelManager($this->registry, $this->propertyAccessor); |
||
315 | |||
316 | $this->assertNull($model->find('notImportant', null)); |
||
317 | } |
||
318 | |||
319 | public function testGetUrlSafeIdentifierException(): void |
||
320 | { |
||
321 | $model = new ModelManager($this->registry, $this->propertyAccessor); |
||
322 | |||
323 | $this->expectException(\RuntimeException::class); |
||
324 | |||
325 | $model->getNormalizedIdentifier(new \stdClass()); |
||
326 | } |
||
327 | |||
328 | public function testGetUrlSafeIdentifierNull(): void |
||
329 | { |
||
330 | $model = new ModelManager($this->registry, $this->propertyAccessor); |
||
331 | |||
332 | $this->assertNull($model->getNormalizedIdentifier(null)); |
||
333 | } |
||
334 | |||
335 | private function createModelManagerForClass(string $class): ModelManager |
||
336 | { |
||
337 | $metadataFactory = $this->createMock(ClassMetadataFactory::class); |
||
338 | $modelManager = $this->createMock(ObjectManager::class); |
||
339 | $registry = $this->createMock(ManagerRegistry::class); |
||
340 | |||
341 | $classMetadata = new ClassMetadata($class); |
||
342 | $classMetadata->reflClass = new \ReflectionClass($class); |
||
343 | |||
344 | $modelManager->expects($this->once()) |
||
345 | ->method('getMetadataFactory') |
||
346 | ->willReturn($metadataFactory); |
||
347 | $metadataFactory->expects($this->once()) |
||
348 | ->method('getMetadataFor') |
||
349 | ->with($class) |
||
350 | ->willReturn($classMetadata); |
||
351 | $registry->expects($this->once()) |
||
352 | ->method('getManagerForClass') |
||
353 | ->with($class) |
||
354 | ->willReturn($modelManager); |
||
355 | |||
356 | return new ModelManager($registry, $this->propertyAccessor); |
||
357 | } |
||
358 | |||
359 | private function getMetadataForEmbeddedDocument(): ClassMetadata |
||
373 | |||
374 | private function getMetadataForAssociatedDocument(): ClassMetadata |
||
397 | |||
398 | private function getMetadataForContainerDocument(): ClassMetadata |
||
430 | } |
||
431 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: