1 | <?php |
||
49 | class ModelManagerTest extends TestCase |
||
50 | { |
||
51 | public static function setUpBeforeClass() |
||
57 | |||
58 | public function testSortParameters() |
||
113 | |||
114 | public function getVersionDataProvider() |
||
121 | |||
122 | /** |
||
123 | * @dataProvider getVersionDataProvider |
||
124 | */ |
||
125 | public function testGetVersion($isVersioned) |
||
148 | |||
149 | public function lockDataProvider() |
||
157 | |||
158 | /** |
||
159 | * @dataProvider lockDataProvider |
||
160 | */ |
||
161 | public function testLock($isVersioned, $expectsException) |
||
195 | |||
196 | public function testGetParentMetadataForProperty() |
||
197 | { |
||
198 | if (version_compare(Version::VERSION, '2.5') < 0) { |
||
199 | $this->markTestSkipped('Test for embeddables needs to run on Doctrine >= 2.5'); |
||
200 | |||
201 | return; |
||
202 | } |
||
203 | |||
204 | $containerEntityClass = ContainerEntity::class; |
||
205 | $associatedEntityClass = AssociatedEntity::class; |
||
206 | $embeddedEntityClass = EmbeddedEntity::class; |
||
207 | $modelManagerClass = ModelManager::class; |
||
208 | |||
209 | $em = $this->createMock(EntityManager::class); |
||
210 | |||
211 | /** @var \PHPUnit_Framework_MockObject_MockObject|ModelManager $modelManager */ |
||
212 | $modelManager = $this->getMockBuilder($modelManagerClass) |
||
213 | ->disableOriginalConstructor() |
||
214 | ->setMethods(['getMetadata', 'getEntityManager']) |
||
215 | ->getMock(); |
||
216 | |||
217 | $modelManager->expects($this->any()) |
||
|
|||
218 | ->method('getEntityManager') |
||
219 | ->will($this->returnValue($em)); |
||
220 | |||
221 | $containerEntityMetadata = $this->getMetadataForContainerEntity(); |
||
222 | $associatedEntityMetadata = $this->getMetadataForAssociatedEntity(); |
||
223 | $embeddedEntityMetadata = $this->getMetadataForEmbeddedEntity(); |
||
224 | |||
225 | $modelManager->expects($this->any())->method('getMetadata') |
||
226 | ->will( |
||
227 | $this->returnValueMap( |
||
228 | [ |
||
229 | [$containerEntityClass, $containerEntityMetadata], |
||
230 | [$embeddedEntityClass, $embeddedEntityMetadata], |
||
231 | [$associatedEntityClass, $associatedEntityMetadata], |
||
232 | ] |
||
233 | ) |
||
234 | ); |
||
235 | |||
236 | /** @var ClassMetadata $metadata */ |
||
237 | list($metadata, $lastPropertyName) = $modelManager |
||
238 | ->getParentMetadataForProperty($containerEntityClass, 'plainField'); |
||
239 | $this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'integer'); |
||
240 | |||
241 | list($metadata, $lastPropertyName) = $modelManager |
||
242 | ->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.plainField'); |
||
243 | $this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'string'); |
||
244 | |||
245 | list($metadata, $lastPropertyName) = $modelManager |
||
246 | ->getParentMetadataForProperty($containerEntityClass, 'embeddedEntity.plainField'); |
||
247 | $this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
248 | |||
249 | list($metadata, $lastPropertyName) = $modelManager |
||
250 | ->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.embeddedEntity.plainField'); |
||
251 | $this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
252 | } |
||
253 | |||
254 | public function getMetadataForEmbeddedEntity() |
||
268 | |||
269 | public function getMetadataForAssociatedEntity() |
||
292 | |||
293 | public function getMetadataForContainerEntity() |
||
324 | |||
325 | public function testNonIntegerIdentifierType() |
||
368 | |||
369 | public function testAssociationIdentifierType() |
||
411 | |||
412 | /** |
||
413 | * [sortBy, sortOrder, isAddOrderBy]. |
||
414 | * |
||
415 | * @return array |
||
416 | */ |
||
417 | public function getSortableInDataSourceIteratorDataProvider() |
||
426 | |||
427 | /** |
||
428 | * @dataProvider getSortableInDataSourceIteratorDataProvider |
||
429 | * |
||
430 | * @param string|null $sortBy |
||
431 | * @param string|null $sortOrder |
||
432 | * @param bool $isAddOrderBy |
||
433 | */ |
||
434 | public function testSortableInDataSourceIterator($sortBy, $sortOrder, $isAddOrderBy) |
||
492 | |||
493 | public function testModelReverseTransform() |
||
527 | |||
528 | public function testCollections() |
||
529 | { |
||
530 | $registry = $this->createMock(RegistryInterface::class); |
||
531 | $model = new ModelManager($registry); |
||
532 | |||
533 | $collection = $model->getModelCollectionInstance('whyDoWeEvenHaveThisParameter'); |
||
534 | $this->assertInstanceOf(ArrayCollection::class, $collection); |
||
535 | |||
536 | $item1 = 'item1'; |
||
537 | $item2 = 'item2'; |
||
538 | $model->collectionAddElement($collection, $item1); |
||
539 | $model->collectionAddElement($collection, $item2); |
||
540 | |||
541 | $this->assertTrue($model->collectionHasElement($collection, $item1)); |
||
542 | |||
543 | $model->collectionRemoveElement($collection, $item1); |
||
544 | |||
545 | $this->assertFalse($model->collectionHasElement($collection, $item1)); |
||
546 | |||
547 | $model->collectionClear($collection); |
||
548 | |||
549 | $this->assertTrue($collection->isEmpty()); |
||
550 | } |
||
551 | |||
552 | public function testModelTransform() |
||
553 | { |
||
554 | $registry = $this->createMock(RegistryInterface::class); |
||
555 | $model = new ModelManager($registry); |
||
556 | |||
557 | $result = $model->modelTransform('thisIsNotUsed', 'doWeNeedThisMethod'); |
||
558 | |||
559 | $this->assertSame('doWeNeedThisMethod', $result); |
||
560 | } |
||
561 | |||
562 | public function testGetPaginationParameters() |
||
563 | { |
||
564 | $datagrid = $this->createMock(DatagridInterface::class); |
||
565 | $filter = $this->createMock(FilterInterface::class); |
||
566 | $registry = $this->createMock(RegistryInterface::class); |
||
567 | |||
568 | $datagrid->expects($this->once()) |
||
569 | ->method('getValues') |
||
570 | ->willReturn(['_sort_by' => $filter]); |
||
571 | |||
572 | $filter->expects($this->once()) |
||
573 | ->method('getName') |
||
574 | ->willReturn($name = 'test'); |
||
575 | |||
576 | $model = new ModelManager($registry); |
||
577 | |||
578 | $result = $model->getPaginationParameters($datagrid, $page = 5); |
||
579 | |||
580 | $this->assertSame($page, $result['filter']['_page']); |
||
581 | $this->assertSame($name, $result['filter']['_sort_by']); |
||
582 | } |
||
583 | |||
584 | public function testGetModelInstanceException() |
||
585 | { |
||
586 | $registry = $this->createMock(RegistryInterface::class); |
||
587 | |||
588 | $model = new ModelManager($registry); |
||
589 | |||
590 | $this->expectException(\RuntimeException::class); |
||
591 | |||
592 | $model->getModelInstance(AbstractEntity::class); |
||
593 | } |
||
594 | |||
595 | public function testGetEntityManagerException() |
||
596 | { |
||
597 | $registry = $this->createMock(RegistryInterface::class); |
||
598 | |||
599 | $model = new ModelManager($registry); |
||
600 | |||
601 | $this->expectException(\RuntimeException::class); |
||
602 | |||
603 | $model->getEntityManager(VersionedEntity::class); |
||
604 | } |
||
605 | |||
606 | public function testGetNewFieldDescriptionInstanceException() |
||
607 | { |
||
608 | $registry = $this->createMock(RegistryInterface::class); |
||
609 | |||
610 | $model = new ModelManager($registry); |
||
611 | |||
612 | $this->expectException(\RuntimeException::class); |
||
613 | |||
614 | $model->getNewFieldDescriptionInstance(VersionedEntity::class, [], []); |
||
615 | } |
||
616 | |||
617 | /** |
||
618 | * @dataProvider createUpdateRemoveData |
||
619 | */ |
||
620 | public function testCreate($exception) |
||
621 | { |
||
622 | $registry = $this->createMock(RegistryInterface::class); |
||
623 | |||
624 | $entityManger = $this->createMock(EntityManager::class); |
||
625 | |||
626 | $registry->expects($this->once()) |
||
627 | ->method('getManagerForClass') |
||
628 | ->willReturn($entityManger); |
||
629 | |||
630 | $entityManger->expects($this->once()) |
||
631 | ->method('persist'); |
||
632 | |||
633 | $entityManger->expects($this->once()) |
||
634 | ->method('flush') |
||
635 | ->willThrowException($exception); |
||
636 | |||
637 | $model = new ModelManager($registry); |
||
638 | |||
639 | $this->expectException(ModelManagerException::class); |
||
640 | |||
641 | $model->create(new VersionedEntity()); |
||
642 | } |
||
643 | |||
644 | public function createUpdateRemoveData() |
||
645 | { |
||
646 | return [ |
||
647 | 'PDOException' => [ |
||
648 | new \PDOException(), |
||
649 | ], |
||
650 | 'DBALException' => [ |
||
651 | new DBALException(), |
||
652 | ], |
||
653 | ]; |
||
654 | } |
||
655 | |||
656 | /** |
||
657 | * @dataProvider createUpdateRemoveData |
||
658 | */ |
||
659 | public function testUpdate($exception) |
||
660 | { |
||
661 | $registry = $this->createMock(RegistryInterface::class); |
||
662 | |||
663 | $entityManger = $this->createMock(EntityManager::class); |
||
664 | |||
665 | $registry->expects($this->once()) |
||
666 | ->method('getManagerForClass') |
||
667 | ->willReturn($entityManger); |
||
668 | |||
669 | $entityManger->expects($this->once()) |
||
670 | ->method('persist'); |
||
671 | |||
672 | $entityManger->expects($this->once()) |
||
673 | ->method('flush') |
||
674 | ->willThrowException($exception); |
||
675 | |||
676 | $model = new ModelManager($registry); |
||
677 | |||
678 | $this->expectException(ModelManagerException::class); |
||
679 | |||
680 | $model->update(new VersionedEntity()); |
||
681 | } |
||
682 | |||
683 | /** |
||
684 | * @dataProvider createUpdateRemoveData |
||
685 | */ |
||
686 | public function testRemove($exception) |
||
687 | { |
||
688 | $registry = $this->createMock(RegistryInterface::class); |
||
689 | |||
690 | $entityManger = $this->createMock(EntityManager::class); |
||
691 | |||
692 | $registry->expects($this->once()) |
||
693 | ->method('getManagerForClass') |
||
694 | ->willReturn($entityManger); |
||
695 | |||
696 | $entityManger->expects($this->once()) |
||
697 | ->method('remove'); |
||
698 | |||
699 | $entityManger->expects($this->once()) |
||
700 | ->method('flush') |
||
701 | ->willThrowException($exception); |
||
702 | |||
703 | $model = new ModelManager($registry); |
||
704 | |||
705 | $this->expectException(ModelManagerException::class); |
||
706 | |||
707 | $model->delete(new VersionedEntity()); |
||
708 | } |
||
709 | |||
710 | public function testFindBadId() |
||
711 | { |
||
712 | $registry = $this->createMock(RegistryInterface::class); |
||
713 | |||
714 | $model = new ModelManager($registry); |
||
715 | |||
716 | $this->assertNull($model->find('notImportant', null)); |
||
717 | } |
||
718 | |||
719 | public function testGetUrlsafeIdentifierException() |
||
720 | { |
||
721 | $registry = $this->createMock(RegistryInterface::class); |
||
722 | |||
723 | $model = new ModelManager($registry); |
||
724 | |||
725 | $this->expectException(\RuntimeException::class); |
||
726 | |||
727 | $model->getNormalizedIdentifier('test'); |
||
728 | } |
||
729 | |||
730 | public function testGetUrlsafeIdentifierNull() |
||
731 | { |
||
732 | $registry = $this->createMock(RegistryInterface::class); |
||
733 | |||
734 | $model = new ModelManager($registry); |
||
735 | |||
736 | $this->assertNull($model->getNormalizedIdentifier(null)); |
||
737 | } |
||
738 | |||
739 | private function getMetadata($class, $isVersioned) |
||
753 | } |
||
754 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.