1 | <?php |
||
52 | |||
53 | class ModelManagerTest extends TestCase |
||
54 | { |
||
55 | public static function setUpBeforeClass(): void |
||
56 | { |
||
57 | if (!Type::hasType('uuid')) { |
||
58 | Type::addType('uuid', UuidType::class); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | public function testSortParameters(): void |
||
63 | { |
||
64 | $registry = $this->createMock(RegistryInterface::class); |
||
65 | |||
66 | $manager = new ModelManager($registry); |
||
67 | |||
68 | $datagrid1 = $this->createMock(Datagrid::class); |
||
69 | $datagrid2 = $this->createMock(Datagrid::class); |
||
70 | |||
71 | $field1 = new FieldDescription(); |
||
72 | $field1->setName('field1'); |
||
73 | |||
74 | $field2 = new FieldDescription(); |
||
75 | $field2->setName('field2'); |
||
76 | |||
77 | $field3 = new FieldDescription(); |
||
78 | $field3->setName('field3'); |
||
79 | $field3->setOption('sortable', 'field3sortBy'); |
||
80 | |||
81 | $datagrid1 |
||
82 | ->expects($this->any()) |
||
83 | ->method('getValues') |
||
84 | ->will($this->returnValue([ |
||
85 | '_sort_by' => $field1, |
||
86 | '_sort_order' => 'ASC', |
||
87 | ])); |
||
88 | |||
89 | $datagrid2 |
||
90 | ->expects($this->any()) |
||
91 | ->method('getValues') |
||
92 | ->will($this->returnValue([ |
||
93 | '_sort_by' => $field3, |
||
94 | '_sort_order' => 'ASC', |
||
95 | ])); |
||
96 | |||
97 | $parameters = $manager->getSortParameters($field1, $datagrid1); |
||
98 | |||
99 | $this->assertSame('DESC', $parameters['filter']['_sort_order']); |
||
100 | $this->assertSame('field1', $parameters['filter']['_sort_by']); |
||
101 | |||
102 | $parameters = $manager->getSortParameters($field2, $datagrid1); |
||
103 | |||
104 | $this->assertSame('ASC', $parameters['filter']['_sort_order']); |
||
105 | $this->assertSame('field2', $parameters['filter']['_sort_by']); |
||
106 | |||
107 | $parameters = $manager->getSortParameters($field3, $datagrid1); |
||
108 | |||
109 | $this->assertSame('ASC', $parameters['filter']['_sort_order']); |
||
110 | $this->assertSame('field3sortBy', $parameters['filter']['_sort_by']); |
||
111 | |||
112 | $parameters = $manager->getSortParameters($field3, $datagrid2); |
||
113 | |||
114 | $this->assertSame('DESC', $parameters['filter']['_sort_order']); |
||
115 | $this->assertSame('field3sortBy', $parameters['filter']['_sort_by']); |
||
116 | } |
||
117 | |||
118 | public function getVersionDataProvider(): array |
||
119 | { |
||
120 | return [ |
||
121 | [true], |
||
122 | [false], |
||
123 | ]; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @dataProvider getVersionDataProvider |
||
128 | */ |
||
129 | public function testGetVersion($isVersioned): void |
||
130 | { |
||
131 | $object = new VersionedEntity(); |
||
132 | |||
133 | $modelManager = $this->getMockBuilder(ModelManager::class) |
||
134 | ->disableOriginalConstructor() |
||
135 | ->setMethods(['getMetadata']) |
||
136 | ->getMock(); |
||
137 | |||
138 | $metadata = $this->getMetadata(\get_class($object), $isVersioned); |
||
139 | |||
140 | $modelManager->expects($this->any()) |
||
141 | ->method('getMetadata') |
||
142 | ->will($this->returnValue($metadata)); |
||
143 | |||
144 | if ($isVersioned) { |
||
145 | $object->version = 123; |
||
146 | |||
147 | $this->assertNotNull($modelManager->getLockVersion($object)); |
||
148 | } else { |
||
149 | $this->assertNull($modelManager->getLockVersion($object)); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | public function lockDataProvider(): array |
||
154 | { |
||
155 | return [ |
||
156 | [true, false], |
||
157 | [true, true], |
||
158 | [false, false], |
||
159 | ]; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @dataProvider lockDataProvider |
||
164 | */ |
||
165 | public function testLock($isVersioned, $expectsException): void |
||
166 | { |
||
167 | $object = new VersionedEntity(); |
||
168 | |||
169 | $em = $this->getMockBuilder(EntityManager::class) |
||
170 | ->disableOriginalConstructor() |
||
171 | ->setMethods(['lock']) |
||
172 | ->getMock(); |
||
173 | |||
174 | $modelManager = $this->getMockBuilder(ModelManager::class) |
||
175 | ->disableOriginalConstructor() |
||
176 | ->setMethods(['getMetadata', 'getEntityManager']) |
||
177 | ->getMock(); |
||
178 | |||
179 | $modelManager->expects($this->any()) |
||
180 | ->method('getEntityManager') |
||
181 | ->will($this->returnValue($em)); |
||
182 | |||
183 | $metadata = $this->getMetadata(\get_class($object), $isVersioned); |
||
184 | |||
185 | $modelManager->expects($this->any()) |
||
186 | ->method('getMetadata') |
||
187 | ->will($this->returnValue($metadata)); |
||
188 | |||
189 | $em->expects($isVersioned ? $this->once() : $this->never()) |
||
190 | ->method('lock'); |
||
191 | |||
192 | if ($expectsException) { |
||
193 | $em->expects($this->once()) |
||
194 | ->method('lock') |
||
195 | ->will($this->throwException(OptimisticLockException::lockFailed($object))); |
||
196 | |||
197 | $this->expectException(LockException::class); |
||
198 | } |
||
199 | |||
200 | $modelManager->lock($object, 123); |
||
201 | } |
||
202 | |||
203 | public function testGetParentMetadataForProperty(): void |
||
204 | { |
||
205 | if (version_compare(Version::VERSION, '2.5') < 0) { |
||
206 | $this->markTestSkipped('Test for embeddables needs to run on Doctrine >= 2.5'); |
||
207 | |||
208 | return; |
||
209 | } |
||
210 | |||
211 | $containerEntityClass = ContainerEntity::class; |
||
212 | $associatedEntityClass = AssociatedEntity::class; |
||
213 | $embeddedEntityClass = EmbeddedEntity::class; |
||
214 | $modelManagerClass = ModelManager::class; |
||
215 | |||
216 | $em = $this->createMock(EntityManager::class); |
||
217 | |||
218 | /** @var \PHPUnit_Framework_MockObject_MockObject|ModelManager $modelManager */ |
||
219 | $modelManager = $this->getMockBuilder($modelManagerClass) |
||
220 | ->disableOriginalConstructor() |
||
221 | ->setMethods(['getMetadata', 'getEntityManager']) |
||
222 | ->getMock(); |
||
223 | |||
224 | $modelManager->expects($this->any()) |
||
225 | ->method('getEntityManager') |
||
226 | ->will($this->returnValue($em)); |
||
227 | |||
228 | $containerEntityMetadata = $this->getMetadataForContainerEntity(); |
||
229 | $associatedEntityMetadata = $this->getMetadataForAssociatedEntity(); |
||
230 | $embeddedEntityMetadata = $this->getMetadataForEmbeddedEntity(); |
||
231 | |||
232 | $modelManager->expects($this->any())->method('getMetadata') |
||
233 | ->will( |
||
234 | $this->returnValueMap( |
||
235 | [ |
||
236 | [$containerEntityClass, $containerEntityMetadata], |
||
237 | [$embeddedEntityClass, $embeddedEntityMetadata], |
||
238 | [$associatedEntityClass, $associatedEntityMetadata], |
||
239 | ] |
||
240 | ) |
||
241 | ); |
||
242 | |||
243 | /** @var ClassMetadata $metadata */ |
||
244 | list($metadata, $lastPropertyName) = $modelManager |
||
245 | ->getParentMetadataForProperty($containerEntityClass, 'plainField'); |
||
246 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'integer'); |
||
247 | |||
248 | list($metadata, $lastPropertyName) = $modelManager |
||
249 | ->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.plainField'); |
||
250 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'string'); |
||
251 | |||
252 | list($metadata, $lastPropertyName) = $modelManager |
||
253 | ->getParentMetadataForProperty($containerEntityClass, 'embeddedEntity.plainField'); |
||
254 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
255 | |||
256 | list($metadata, $lastPropertyName) = $modelManager |
||
257 | ->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.embeddedEntity.plainField'); |
||
258 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
259 | |||
260 | list($metadata, $lastPropertyName) = $modelManager |
||
261 | ->getParentMetadataForProperty( |
||
262 | $containerEntityClass, |
||
263 | 'associatedEntity.embeddedEntity.subEmbeddedEntity.plainField' |
||
264 | ); |
||
265 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
266 | } |
||
267 | |||
268 | public function getMetadataForEmbeddedEntity() |
||
269 | { |
||
270 | $metadata = new ClassMetadata(EmbeddedEntity::class); |
||
271 | |||
272 | $metadata->fieldMappings = [ |
||
273 | 'plainField' => [ |
||
274 | 'fieldName' => 'plainField', |
||
275 | 'columnName' => 'plainField', |
||
276 | 'type' => 'boolean', |
||
277 | ], |
||
278 | ]; |
||
279 | |||
280 | return $metadata; |
||
281 | } |
||
282 | |||
283 | public function getMetadataForSubEmbeddedEntity() |
||
284 | { |
||
285 | $metadata = new ClassMetadata(SubEmbeddedEntity::class); |
||
286 | |||
287 | $metadata->fieldMappings = [ |
||
288 | 'plainField' => [ |
||
289 | 'fieldName' => 'plainField', |
||
290 | 'columnName' => 'plainField', |
||
291 | 'type' => 'boolean', |
||
292 | ], |
||
293 | ]; |
||
294 | |||
295 | return $metadata; |
||
296 | } |
||
297 | |||
298 | public function getMetadataForAssociatedEntity() |
||
299 | { |
||
300 | $embeddedEntityClass = EmbeddedEntity::class; |
||
301 | $subEmbeddedEntityClass = SubEmbeddedEntity::class; |
||
302 | |||
303 | $metadata = new ClassMetadata(AssociatedEntity::class); |
||
304 | |||
305 | $metadata->fieldMappings = [ |
||
306 | 'plainField' => [ |
||
307 | 'fieldName' => 'plainField', |
||
308 | 'columnName' => 'plainField', |
||
309 | 'type' => 'string', |
||
310 | ], |
||
311 | ]; |
||
312 | |||
313 | $metadata->embeddedClasses['embeddedEntity'] = [ |
||
314 | 'class' => $embeddedEntityClass, |
||
315 | 'columnPrefix' => 'embedded_entity_', |
||
316 | ]; |
||
317 | $metadata->embeddedClasses['embeddedEntity.subEmbeddedEntity'] = [ |
||
318 | 'class' => $subEmbeddedEntityClass, |
||
319 | 'columnPrefix' => 'embedded_entity_sub_embedded_entity_', |
||
320 | 'declaredField' => 'embeddedEntity', |
||
321 | 'originalField' => 'subEmbeddedEntity', |
||
322 | ]; |
||
323 | |||
324 | $metadata->inlineEmbeddable('embeddedEntity', $this->getMetadataForEmbeddedEntity()); |
||
325 | $metadata->inlineEmbeddable('embeddedEntity.subEmbeddedEntity', $this->getMetadataForSubEmbeddedEntity()); |
||
326 | |||
327 | return $metadata; |
||
328 | } |
||
329 | |||
330 | public function getMetadataForContainerEntity() |
||
331 | { |
||
332 | $containerEntityClass = ContainerEntity::class; |
||
333 | $associatedEntityClass = AssociatedEntity::class; |
||
334 | $embeddedEntityClass = EmbeddedEntity::class; |
||
335 | $subEmbeddedEntityClass = SubEmbeddedEntity::class; |
||
336 | |||
337 | $metadata = new ClassMetadata($containerEntityClass); |
||
338 | |||
339 | $metadata->fieldMappings = [ |
||
340 | 'plainField' => [ |
||
341 | 'fieldName' => 'plainField', |
||
342 | 'columnName' => 'plainField', |
||
343 | 'type' => 'integer', |
||
344 | ], |
||
345 | ]; |
||
346 | |||
347 | $metadata->associationMappings['associatedEntity'] = [ |
||
348 | 'fieldName' => 'associatedEntity', |
||
349 | 'targetEntity' => $associatedEntityClass, |
||
350 | 'sourceEntity' => $containerEntityClass, |
||
351 | ]; |
||
352 | |||
353 | $metadata->embeddedClasses['embeddedEntity'] = [ |
||
354 | 'class' => $embeddedEntityClass, |
||
355 | 'columnPrefix' => 'embeddedEntity', |
||
356 | ]; |
||
357 | $metadata->embeddedClasses['embeddedEntity.subEmbeddedEntity'] = [ |
||
358 | 'class' => $subEmbeddedEntityClass, |
||
359 | 'columnPrefix' => 'embedded_entity_sub_embedded_entity_', |
||
360 | 'declaredField' => 'embeddedEntity', |
||
361 | 'originalField' => 'subEmbeddedEntity', |
||
362 | ]; |
||
363 | |||
364 | $metadata->inlineEmbeddable('embeddedEntity', $this->getMetadataForEmbeddedEntity()); |
||
365 | $metadata->inlineEmbeddable('embeddedEntity.subEmbeddedEntity', $this->getMetadataForSubEmbeddedEntity()); |
||
366 | |||
367 | return $metadata; |
||
368 | } |
||
369 | |||
370 | public function testNonIntegerIdentifierType(): void |
||
371 | { |
||
372 | $uuid = new NonIntegerIdentifierTestClass('efbcfc4b-8c43-4d42-aa4c-d707e55151ac'); |
||
373 | $entity = new UuidEntity($uuid); |
||
374 | |||
375 | $meta = $this->createMock(ClassMetadata::class); |
||
376 | $meta->expects($this->any()) |
||
377 | ->method('getIdentifierValues') |
||
378 | ->willReturn([$entity->getId()]); |
||
379 | $meta->expects($this->any()) |
||
380 | ->method('getTypeOfField') |
||
381 | ->willReturn(UuidType::NAME); |
||
382 | |||
383 | $mf = $this->createMock(ClassMetadataFactory::class); |
||
384 | $mf->expects($this->any()) |
||
385 | ->method('getMetadataFor') |
||
386 | ->willReturn($meta); |
||
387 | |||
388 | $platform = $this->createMock(PostgreSqlPlatform::class); |
||
389 | |||
390 | $conn = $this->createMock(Connection::class); |
||
391 | $conn->expects($this->any()) |
||
392 | ->method('getDatabasePlatform') |
||
393 | ->willReturn($platform); |
||
394 | |||
395 | $em = $this->createMock(EntityManager::class); |
||
396 | $em->expects($this->any()) |
||
397 | ->method('getMetadataFactory') |
||
398 | ->willReturn($mf); |
||
399 | $em->expects($this->any()) |
||
400 | ->method('getConnection') |
||
401 | ->willReturn($conn); |
||
402 | |||
403 | $registry = $this->createMock(RegistryInterface::class); |
||
404 | $registry->expects($this->any()) |
||
405 | ->method('getManagerForClass') |
||
406 | ->willReturn($em); |
||
407 | |||
408 | $manager = new ModelManager($registry); |
||
409 | $result = $manager->getIdentifierValues($entity); |
||
410 | |||
411 | $this->assertSame($entity->getId()->toString(), $result[0]); |
||
412 | } |
||
413 | |||
414 | public function testAssociationIdentifierType(): void |
||
415 | { |
||
416 | $entity = new ContainerEntity(new AssociatedEntity(42, new EmbeddedEntity()), new EmbeddedEntity()); |
||
417 | |||
418 | $meta = $this->createMock(ClassMetadata::class); |
||
419 | $meta->expects($this->any()) |
||
420 | ->method('getIdentifierValues') |
||
421 | ->willReturn([$entity->getAssociatedEntity()->getPlainField()]); |
||
422 | $meta->expects($this->any()) |
||
423 | ->method('getTypeOfField') |
||
424 | ->willReturn(null); |
||
425 | |||
426 | $mf = $this->createMock(ClassMetadataFactory::class); |
||
427 | $mf->expects($this->any()) |
||
428 | ->method('getMetadataFor') |
||
429 | ->willReturn($meta); |
||
430 | |||
431 | $platform = $this->createMock(PostgreSqlPlatform::class); |
||
432 | |||
433 | $conn = $this->createMock(Connection::class); |
||
434 | $conn->expects($this->any()) |
||
435 | ->method('getDatabasePlatform') |
||
436 | ->willReturn($platform); |
||
437 | |||
438 | $em = $this->createMock(EntityManager::class); |
||
439 | $em->expects($this->any()) |
||
440 | ->method('getMetadataFactory') |
||
441 | ->willReturn($mf); |
||
442 | $em->expects($this->any()) |
||
443 | ->method('getConnection') |
||
444 | ->willReturn($conn); |
||
445 | |||
446 | $registry = $this->createMock(RegistryInterface::class); |
||
447 | $registry->expects($this->any()) |
||
448 | ->method('getManagerForClass') |
||
449 | ->willReturn($em); |
||
450 | |||
451 | $manager = new ModelManager($registry); |
||
452 | $result = $manager->getIdentifierValues($entity); |
||
453 | |||
454 | $this->assertSame(42, $result[0]); |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * [sortBy, sortOrder, isAddOrderBy]. |
||
459 | * |
||
460 | * @return array |
||
461 | */ |
||
462 | public function getSortableInDataSourceIteratorDataProvider(): array |
||
463 | { |
||
464 | return [ |
||
465 | [null, null, false], |
||
466 | ['', 'ASC', false], |
||
467 | ['field', 'ASC', true], |
||
468 | ['field', null, true], |
||
469 | ]; |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * @dataProvider getSortableInDataSourceIteratorDataProvider |
||
474 | * |
||
475 | * @param string|null $sortBy |
||
476 | * @param string|null $sortOrder |
||
477 | * @param bool $isAddOrderBy |
||
478 | */ |
||
479 | public function testSortableInDataSourceIterator($sortBy, $sortOrder, $isAddOrderBy): void |
||
480 | { |
||
481 | $datagrid = $this->getMockForAbstractClass(DatagridInterface::class); |
||
482 | $configuration = $this->getMockBuilder(Configuration::class)->getMock(); |
||
483 | $configuration->expects($this->any()) |
||
484 | ->method('getDefaultQueryHints') |
||
485 | ->willReturn([]); |
||
486 | |||
487 | $em = $this->getMockBuilder(EntityManager::class) |
||
488 | ->disableOriginalConstructor() |
||
489 | ->getMock(); |
||
490 | |||
491 | $em->expects($this->any()) |
||
492 | ->method('getConfiguration') |
||
493 | ->willReturn($configuration); |
||
494 | |||
495 | $queryBuilder = $this->getMockBuilder(QueryBuilder::class) |
||
496 | ->setConstructorArgs([$em]) |
||
497 | ->getMock(); |
||
498 | $query = new Query($em); |
||
499 | |||
500 | $proxyQuery = $this->getMockBuilder(ProxyQuery::class) |
||
501 | ->setConstructorArgs([$queryBuilder]) |
||
502 | ->setMethods(['getSortBy', 'getSortOrder', 'getRootAliases']) |
||
503 | ->getMock(); |
||
504 | |||
505 | $proxyQuery->expects($this->any()) |
||
506 | ->method('getSortOrder') |
||
507 | ->willReturn($sortOrder); |
||
508 | |||
509 | $proxyQuery->expects($this->any()) |
||
510 | ->method('getSortBy') |
||
511 | ->willReturn($sortBy); |
||
512 | |||
513 | $queryBuilder->expects($isAddOrderBy ? $this->atLeastOnce() : $this->never()) |
||
514 | ->method('addOrderBy'); |
||
515 | |||
516 | $proxyQuery->expects($this->any()) |
||
517 | ->method('getRootAliases') |
||
518 | ->willReturn(['a']); |
||
519 | |||
520 | $queryBuilder->expects($this->any()) |
||
521 | ->method('getQuery') |
||
522 | ->willReturn($query); |
||
523 | |||
524 | $datagrid->expects($this->any()) |
||
525 | ->method('getQuery') |
||
526 | ->willReturn($proxyQuery); |
||
527 | |||
528 | $registry = $this->getMockBuilder(RegistryInterface::class)->getMock(); |
||
529 | $manager = new ModelManager($registry); |
||
530 | $manager->getDataSourceIterator($datagrid, []); |
||
531 | |||
532 | if ($isAddOrderBy) { |
||
533 | $this->assertArrayHasKey($key = 'doctrine.customTreeWalkers', $hints = $query->getHints()); |
||
534 | $this->assertContains(OrderByToSelectWalker::class, $hints[$key]); |
||
535 | } |
||
536 | } |
||
537 | |||
538 | public function testModelReverseTransform(): void |
||
539 | { |
||
540 | $class = SimpleEntity::class; |
||
541 | |||
542 | $metadataFactory = $this->createMock(ClassMetadataFactory::class); |
||
543 | $modelManager = $this->createMock(ObjectManager::class); |
||
544 | $registry = $this->createMock(RegistryInterface::class); |
||
545 | |||
546 | $classMetadata = new ClassMetadata($class); |
||
547 | $classMetadata->reflClass = new \ReflectionClass($class); |
||
548 | |||
549 | $modelManager->expects($this->once()) |
||
550 | ->method('getMetadataFactory') |
||
551 | ->willReturn($metadataFactory); |
||
552 | $metadataFactory->expects($this->once()) |
||
553 | ->method('getMetadataFor') |
||
554 | ->with($class) |
||
555 | ->willReturn($classMetadata); |
||
556 | $registry->expects($this->once()) |
||
557 | ->method('getManagerForClass') |
||
558 | ->with($class) |
||
559 | ->willReturn($modelManager); |
||
560 | |||
561 | $manager = new ModelManager($registry); |
||
562 | $this->assertInstanceOf($class, $object = $manager->modelReverseTransform( |
||
563 | $class, |
||
564 | [ |
||
565 | 'schmeckles' => 42, |
||
566 | 'multi_word_property' => 'hello', |
||
567 | ] |
||
568 | )); |
||
569 | $this->assertSame(42, $object->getSchmeckles()); |
||
570 | $this->assertSame('hello', $object->getMultiWordProperty()); |
||
571 | } |
||
572 | |||
573 | public function testCollections(): void |
||
574 | { |
||
575 | $registry = $this->createMock(RegistryInterface::class); |
||
576 | $model = new ModelManager($registry); |
||
577 | |||
578 | $collection = $model->getModelCollectionInstance('whyDoWeEvenHaveThisParameter'); |
||
579 | $this->assertInstanceOf(ArrayCollection::class, $collection); |
||
580 | |||
581 | $item1 = 'item1'; |
||
582 | $item2 = 'item2'; |
||
583 | $model->collectionAddElement($collection, $item1); |
||
584 | $model->collectionAddElement($collection, $item2); |
||
585 | |||
586 | $this->assertTrue($model->collectionHasElement($collection, $item1)); |
||
587 | |||
588 | $model->collectionRemoveElement($collection, $item1); |
||
589 | |||
590 | $this->assertFalse($model->collectionHasElement($collection, $item1)); |
||
591 | |||
592 | $model->collectionClear($collection); |
||
593 | |||
594 | $this->assertTrue($collection->isEmpty()); |
||
595 | } |
||
596 | |||
597 | public function testModelTransform(): void |
||
598 | { |
||
599 | $registry = $this->createMock(RegistryInterface::class); |
||
600 | $model = new ModelManager($registry); |
||
601 | |||
602 | $result = $model->modelTransform('thisIsNotUsed', 'doWeNeedThisMethod'); |
||
603 | |||
604 | $this->assertSame('doWeNeedThisMethod', $result); |
||
605 | } |
||
606 | |||
607 | public function testGetPaginationParameters(): void |
||
608 | { |
||
609 | $datagrid = $this->createMock(DatagridInterface::class); |
||
610 | $filter = $this->createMock(FilterInterface::class); |
||
611 | $registry = $this->createMock(RegistryInterface::class); |
||
612 | |||
613 | $datagrid->expects($this->once()) |
||
614 | ->method('getValues') |
||
615 | ->willReturn(['_sort_by' => $filter]); |
||
616 | |||
617 | $filter->expects($this->once()) |
||
618 | ->method('getName') |
||
619 | ->willReturn($name = 'test'); |
||
620 | |||
621 | $model = new ModelManager($registry); |
||
622 | |||
623 | $result = $model->getPaginationParameters($datagrid, $page = 5); |
||
624 | |||
625 | $this->assertSame($page, $result['filter']['_page']); |
||
626 | $this->assertSame($name, $result['filter']['_sort_by']); |
||
627 | } |
||
628 | |||
629 | public function testGetModelInstanceException(): void |
||
630 | { |
||
631 | $registry = $this->createMock(RegistryInterface::class); |
||
632 | |||
633 | $model = new ModelManager($registry); |
||
634 | |||
635 | $this->expectException(\RuntimeException::class); |
||
636 | |||
637 | $model->getModelInstance(AbstractEntity::class); |
||
638 | } |
||
639 | |||
640 | public function testGetEntityManagerException(): void |
||
641 | { |
||
642 | $registry = $this->createMock(RegistryInterface::class); |
||
643 | |||
644 | $model = new ModelManager($registry); |
||
645 | |||
646 | $this->expectException(\RuntimeException::class); |
||
647 | |||
648 | $model->getEntityManager(VersionedEntity::class); |
||
649 | } |
||
650 | |||
651 | public function testGetNewFieldDescriptionInstanceException(): void |
||
652 | { |
||
653 | $registry = $this->createMock(RegistryInterface::class); |
||
654 | |||
655 | $model = new ModelManager($registry); |
||
656 | |||
657 | $this->expectException(\RuntimeException::class); |
||
658 | |||
659 | $model->getNewFieldDescriptionInstance(VersionedEntity::class, [], []); |
||
660 | } |
||
661 | |||
662 | /** |
||
663 | * @dataProvider createUpdateRemoveData |
||
664 | */ |
||
665 | public function testCreate($exception): void |
||
666 | { |
||
667 | $registry = $this->createMock(RegistryInterface::class); |
||
668 | |||
669 | $entityManger = $this->createMock(EntityManager::class); |
||
670 | |||
671 | $registry->expects($this->once()) |
||
672 | ->method('getManagerForClass') |
||
673 | ->willReturn($entityManger); |
||
674 | |||
675 | $entityManger->expects($this->once()) |
||
676 | ->method('persist'); |
||
677 | |||
678 | $entityManger->expects($this->once()) |
||
679 | ->method('flush') |
||
680 | ->willThrowException($exception); |
||
681 | |||
682 | $model = new ModelManager($registry); |
||
683 | |||
684 | $this->expectException(ModelManagerException::class); |
||
685 | |||
686 | $model->create(new VersionedEntity()); |
||
687 | } |
||
688 | |||
689 | public function createUpdateRemoveData(): array |
||
690 | { |
||
691 | return [ |
||
692 | 'PDOException' => [ |
||
693 | new \PDOException(), |
||
694 | ], |
||
695 | 'DBALException' => [ |
||
696 | new DBALException(), |
||
697 | ], |
||
698 | ]; |
||
699 | } |
||
700 | |||
701 | /** |
||
702 | * @dataProvider createUpdateRemoveData |
||
703 | */ |
||
704 | public function testUpdate($exception): void |
||
705 | { |
||
706 | $registry = $this->createMock(RegistryInterface::class); |
||
707 | |||
708 | $entityManger = $this->createMock(EntityManager::class); |
||
709 | |||
710 | $registry->expects($this->once()) |
||
711 | ->method('getManagerForClass') |
||
712 | ->willReturn($entityManger); |
||
713 | |||
714 | $entityManger->expects($this->once()) |
||
715 | ->method('persist'); |
||
716 | |||
717 | $entityManger->expects($this->once()) |
||
718 | ->method('flush') |
||
719 | ->willThrowException($exception); |
||
720 | |||
721 | $model = new ModelManager($registry); |
||
722 | |||
723 | $this->expectException(ModelManagerException::class); |
||
724 | |||
725 | $model->update(new VersionedEntity()); |
||
726 | } |
||
727 | |||
728 | /** |
||
729 | * @dataProvider createUpdateRemoveData |
||
730 | */ |
||
731 | public function testRemove($exception): void |
||
732 | { |
||
733 | $registry = $this->createMock(RegistryInterface::class); |
||
734 | |||
735 | $entityManger = $this->createMock(EntityManager::class); |
||
736 | |||
737 | $registry->expects($this->once()) |
||
738 | ->method('getManagerForClass') |
||
739 | ->willReturn($entityManger); |
||
740 | |||
741 | $entityManger->expects($this->once()) |
||
742 | ->method('remove'); |
||
743 | |||
744 | $entityManger->expects($this->once()) |
||
745 | ->method('flush') |
||
746 | ->willThrowException($exception); |
||
747 | |||
748 | $model = new ModelManager($registry); |
||
749 | |||
750 | $this->expectException(ModelManagerException::class); |
||
751 | |||
752 | $model->delete(new VersionedEntity()); |
||
753 | } |
||
754 | |||
755 | public function testFindBadId(): void |
||
756 | { |
||
757 | $registry = $this->createMock(RegistryInterface::class); |
||
758 | |||
759 | $model = new ModelManager($registry); |
||
760 | |||
761 | $this->assertNull($model->find('notImportant', null)); |
||
762 | } |
||
763 | |||
764 | public function testGetUrlsafeIdentifierException(): void |
||
765 | { |
||
766 | $registry = $this->createMock(RegistryInterface::class); |
||
767 | |||
768 | $model = new ModelManager($registry); |
||
769 | |||
770 | $this->expectException(\RuntimeException::class); |
||
771 | |||
772 | $model->getNormalizedIdentifier('test'); |
||
773 | } |
||
774 | |||
775 | public function testGetUrlsafeIdentifierNull(): void |
||
776 | { |
||
777 | $registry = $this->createMock(RegistryInterface::class); |
||
778 | |||
779 | $model = new ModelManager($registry); |
||
780 | |||
781 | $this->assertNull($model->getNormalizedIdentifier(null)); |
||
782 | } |
||
783 | |||
784 | private function getMetadata($class, $isVersioned) |
||
785 | { |
||
786 | $metadata = new ClassMetadata($class); |
||
787 | |||
788 | $metadata->isVersioned = $isVersioned; |
||
789 | |||
790 | if ($isVersioned) { |
||
791 | $versionField = 'version'; |
||
792 | $metadata->versionField = $versionField; |
||
793 | $metadata->reflFields[$versionField] = new \ReflectionProperty($class, $versionField); |
||
794 | } |
||
795 | |||
796 | return $metadata; |
||
797 | } |
||
798 | } |
||
799 |