Complex classes like DoctrineObjectTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DoctrineObjectTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class DoctrineObjectTest extends BaseTestCase |
||
16 | { |
||
17 | /** |
||
18 | * @var DoctrineObjectHydrator |
||
19 | */ |
||
20 | protected $hydratorByValue; |
||
21 | |||
22 | /** |
||
23 | * @var DoctrineObjectHydrator |
||
24 | */ |
||
25 | protected $hydratorByReference; |
||
26 | |||
27 | /** |
||
28 | * @var \Doctrine\Common\Persistence\Mapping\ClassMetadata|\PHPUnit_Framework_MockObject_MockObject |
||
29 | */ |
||
30 | protected $metadata; |
||
31 | |||
32 | /** |
||
33 | * @var \Doctrine\Common\Persistence\ObjectManager|\PHPUnit_Framework_MockObject_MockObject |
||
34 | */ |
||
35 | protected $objectManager; |
||
36 | |||
37 | /** |
||
38 | * setUp |
||
39 | */ |
||
40 | public function setUp() |
||
41 | { |
||
42 | parent::setUp(); |
||
43 | |||
44 | $this->metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
||
45 | $this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
||
46 | |||
47 | $this->objectManager->expects($this->any()) |
||
48 | ->method('getClassMetadata') |
||
49 | ->will($this->returnValue($this->metadata)); |
||
50 | } |
||
51 | |||
52 | public function configureObjectManagerForSimpleEntity() |
||
119 | |||
120 | public function configureObjectManagerForNamingStrategyEntity() |
||
121 | { |
||
122 | $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\NamingStrategyEntity'); |
||
123 | |||
124 | $this |
||
125 | ->metadata |
||
126 | ->expects($this->any()) |
||
127 | ->method('getName') |
||
128 | ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\NamingStrategyEntity')); |
||
129 | $this |
||
130 | ->metadata |
||
131 | ->expects($this->any()) |
||
132 | ->method('getAssociationNames') |
||
133 | ->will($this->returnValue(array())); |
||
134 | |||
135 | $this |
||
136 | ->metadata |
||
137 | ->expects($this->any()) |
||
138 | ->method('getFieldNames') |
||
139 | ->will($this->returnValue(array('camelCase'))); |
||
140 | |||
141 | $this |
||
142 | ->metadata |
||
143 | ->expects($this->any()) |
||
144 | ->method('getTypeOfField') |
||
145 | ->with($this->equalTo('camelCase')) |
||
146 | ->will($this->returnValue('string')); |
||
147 | |||
148 | $this |
||
149 | ->metadata |
||
150 | ->expects($this->any()) |
||
151 | ->method('hasAssociation') |
||
152 | ->will($this->returnValue(false)); |
||
153 | |||
154 | $this |
||
155 | ->metadata |
||
156 | ->expects($this->any()) |
||
157 | ->method('getIdentifierFieldNames') |
||
158 | ->will($this->returnValue(array('camelCase'))); |
||
159 | |||
160 | $this |
||
161 | ->metadata |
||
162 | ->expects($this->any()) |
||
163 | ->method('getReflectionClass') |
||
164 | ->will($this->returnValue($refl)); |
||
165 | |||
166 | $this->hydratorByValue = new DoctrineObjectHydrator( |
||
167 | $this->objectManager, |
||
168 | true |
||
169 | ); |
||
170 | $this->hydratorByReference = new DoctrineObjectHydrator( |
||
171 | $this->objectManager, |
||
172 | false |
||
173 | ); |
||
174 | } |
||
175 | |||
176 | public function configureObjectManagerForSimpleIsEntity() |
||
177 | { |
||
178 | $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleIsEntity'); |
||
179 | |||
180 | $this |
||
181 | ->metadata |
||
182 | ->expects($this->any()) |
||
183 | ->method('getName') |
||
184 | ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleIsEntity')); |
||
185 | $this |
||
186 | ->metadata |
||
187 | ->expects($this->any()) |
||
188 | ->method('getAssociationNames') |
||
189 | ->will($this->returnValue(array())); |
||
190 | |||
191 | $this |
||
192 | ->metadata |
||
193 | ->expects($this->any()) |
||
194 | ->method('getFieldNames') |
||
195 | ->will($this->returnValue(array('id', 'done'))); |
||
196 | |||
197 | $this |
||
198 | ->metadata |
||
199 | ->expects($this->any()) |
||
200 | ->method('getTypeOfField') |
||
201 | ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('done'))) |
||
202 | ->will( |
||
203 | $this->returnCallback( |
||
204 | function ($arg) { |
||
205 | if ('id' === $arg) { |
||
206 | return 'integer'; |
||
207 | } elseif ('done' === $arg) { |
||
208 | return 'boolean'; |
||
209 | } |
||
210 | |||
211 | throw new \InvalidArgumentException(); |
||
212 | } |
||
213 | ) |
||
214 | ); |
||
215 | |||
216 | $this |
||
217 | ->metadata |
||
218 | ->expects($this->any()) |
||
219 | ->method('hasAssociation') |
||
220 | ->will($this->returnValue(false)); |
||
221 | |||
222 | $this |
||
223 | ->metadata |
||
224 | ->expects($this->any()) |
||
225 | ->method('getIdentifierFieldNames') |
||
226 | ->will($this->returnValue(array('id'))); |
||
227 | |||
228 | $this |
||
229 | ->metadata |
||
230 | ->expects($this->any()) |
||
231 | ->method('getReflectionClass') |
||
232 | ->will($this->returnValue($refl)); |
||
233 | |||
234 | $this->hydratorByValue = new DoctrineObjectHydrator( |
||
235 | $this->objectManager, |
||
236 | true |
||
237 | ); |
||
238 | $this->hydratorByReference = new DoctrineObjectHydrator( |
||
239 | $this->objectManager, |
||
240 | false |
||
241 | ); |
||
242 | } |
||
243 | |||
244 | public function configureObjectManagerForSimpleEntityWithIsBoolean() |
||
245 | { |
||
246 | $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithIsBoolean'); |
||
247 | |||
248 | $this |
||
249 | ->metadata |
||
250 | ->expects($this->any()) |
||
251 | ->method('getName') |
||
252 | ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithIsBoolean')); |
||
253 | $this |
||
254 | ->metadata |
||
255 | ->expects($this->any()) |
||
256 | ->method('getAssociationNames') |
||
257 | ->will($this->returnValue(array())); |
||
258 | |||
259 | $this |
||
260 | ->metadata |
||
261 | ->expects($this->any()) |
||
262 | ->method('getFieldNames') |
||
263 | ->will($this->returnValue(array('id', 'isActive'))); |
||
264 | |||
265 | $this |
||
266 | ->metadata |
||
267 | ->expects($this->any()) |
||
268 | ->method('getTypeOfField') |
||
269 | ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('isActive'))) |
||
270 | ->will( |
||
271 | $this->returnCallback( |
||
272 | function ($arg) { |
||
273 | if ('id' === $arg) { |
||
274 | return 'integer'; |
||
275 | } elseif ('isActive' === $arg) { |
||
276 | return 'boolean'; |
||
277 | } |
||
278 | |||
279 | throw new \InvalidArgumentException(); |
||
280 | } |
||
281 | ) |
||
282 | ); |
||
283 | |||
284 | $this |
||
285 | ->metadata |
||
286 | ->expects($this->any()) |
||
287 | ->method('hasAssociation') |
||
288 | ->will($this->returnValue(false)); |
||
289 | |||
290 | $this |
||
291 | ->metadata |
||
292 | ->expects($this->any()) |
||
293 | ->method('getIdentifierFieldNames') |
||
294 | ->will($this->returnValue(array('id'))); |
||
295 | |||
296 | $this |
||
297 | ->metadata |
||
298 | ->expects($this->any()) |
||
299 | ->method('getReflectionClass') |
||
300 | ->will($this->returnValue($refl)); |
||
301 | |||
302 | $this->hydratorByValue = new DoctrineObjectHydrator( |
||
303 | $this->objectManager, |
||
304 | true |
||
305 | ); |
||
306 | $this->hydratorByReference = new DoctrineObjectHydrator( |
||
307 | $this->objectManager, |
||
308 | false |
||
309 | ); |
||
310 | } |
||
311 | |||
312 | public function configureObjectManagerForSimpleEntityWithStringId() |
||
367 | |||
368 | public function configureObjectManagerForSimpleEntityWithDateTime() |
||
430 | |||
431 | public function configureObjectManagerForOneToOneEntity() |
||
520 | |||
521 | public function configureObjectManagerForOneToOneEntityNotNullable() |
||
624 | |||
625 | public function configureObjectManagerForOneToManyEntity() |
||
626 | { |
||
627 | $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity'); |
||
628 | |||
629 | $this |
||
630 | ->metadata |
||
631 | ->expects($this->any()) |
||
632 | ->method('getFieldNames') |
||
633 | ->will($this->returnValue(array('id'))); |
||
634 | |||
635 | $this |
||
636 | ->metadata |
||
637 | ->expects($this->any()) |
||
638 | ->method('getAssociationNames') |
||
639 | ->will($this->returnValue(array('entities'))); |
||
640 | |||
641 | $this |
||
642 | ->metadata |
||
643 | ->expects($this->any()) |
||
644 | ->method('getTypeOfField') |
||
645 | ->with( |
||
646 | $this->logicalOr( |
||
647 | $this->equalTo('id'), |
||
648 | $this->equalTo('entities'), |
||
649 | $this->equalTo('field') |
||
650 | ) |
||
651 | ) |
||
652 | ->will( |
||
653 | $this->returnCallback( |
||
654 | function ($arg) { |
||
655 | if ($arg === 'id') { |
||
656 | return 'integer'; |
||
657 | } elseif ($arg === 'field') { |
||
658 | return 'string'; |
||
659 | } elseif ($arg === 'entities') { |
||
660 | return 'Doctrine\Common\Collections\ArrayCollection'; |
||
661 | } |
||
662 | |||
663 | throw new \InvalidArgumentException(); |
||
664 | } |
||
665 | ) |
||
666 | ); |
||
667 | |||
668 | $this |
||
669 | ->metadata |
||
670 | ->expects($this->any()) |
||
671 | ->method('hasAssociation') |
||
672 | ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('entities'), $this->equalTo('field'))) |
||
673 | ->will( |
||
674 | $this->returnCallback( |
||
675 | function ($arg) { |
||
676 | if ($arg === 'id') { |
||
677 | return false; |
||
678 | } elseif ($arg === 'field') { |
||
679 | return false; |
||
680 | } elseif ($arg === 'entities') { |
||
681 | return true; |
||
682 | } |
||
683 | |||
684 | throw new \InvalidArgumentException(); |
||
685 | } |
||
686 | ) |
||
687 | ); |
||
688 | |||
689 | $this |
||
690 | ->metadata |
||
691 | ->expects($this->any()) |
||
692 | ->method('isSingleValuedAssociation') |
||
693 | ->with('entities') |
||
694 | ->will($this->returnValue(false)); |
||
695 | |||
696 | $this |
||
697 | ->metadata |
||
698 | ->expects($this->any()) |
||
699 | ->method('isCollectionValuedAssociation') |
||
700 | ->with('entities') |
||
701 | ->will($this->returnValue(true)); |
||
702 | |||
703 | $this |
||
704 | ->metadata |
||
705 | ->expects($this->any()) |
||
706 | ->method('getAssociationTargetClass') |
||
707 | ->with('entities') |
||
708 | ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity')); |
||
709 | |||
710 | $this |
||
711 | ->metadata |
||
712 | ->expects($this->any()) |
||
713 | ->method('getReflectionClass') |
||
714 | ->will($this->returnValue($refl)); |
||
715 | |||
716 | $this->metadata |
||
717 | ->expects($this->any()) |
||
718 | ->method('getIdentifier') |
||
719 | ->will($this->returnValue(array("id"))); |
||
720 | |||
721 | $this->hydratorByValue = new DoctrineObjectHydrator( |
||
722 | $this->objectManager, |
||
723 | true |
||
724 | ); |
||
725 | $this->hydratorByReference = new DoctrineObjectHydrator( |
||
726 | $this->objectManager, |
||
727 | false |
||
728 | ); |
||
729 | } |
||
730 | |||
731 | public function configureObjectManagerForOneToManyArrayEntity() |
||
732 | { |
||
733 | $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyArrayEntity'); |
||
734 | |||
735 | $this |
||
736 | ->metadata |
||
737 | ->expects($this->any()) |
||
738 | ->method('getFieldNames') |
||
739 | ->will($this->returnValue(array('id'))); |
||
740 | |||
741 | $this |
||
742 | ->metadata |
||
743 | ->expects($this->any()) |
||
744 | ->method('getAssociationNames') |
||
745 | ->will($this->returnValue(array('entities'))); |
||
746 | |||
747 | $this |
||
748 | ->metadata |
||
749 | ->expects($this->any()) |
||
750 | ->method('getTypeOfField') |
||
751 | ->with( |
||
752 | $this->logicalOr( |
||
753 | $this->equalTo('id'), |
||
754 | $this->equalTo('entities'), |
||
755 | $this->equalTo('field') |
||
756 | ) |
||
757 | ) |
||
758 | ->will( |
||
759 | $this->returnCallback( |
||
760 | function ($arg) { |
||
761 | if ($arg === 'id') { |
||
762 | return 'integer'; |
||
763 | } elseif ($arg === 'field') { |
||
764 | return 'string'; |
||
765 | } elseif ($arg === 'entities') { |
||
766 | return 'Doctrine\Common\Collections\ArrayCollection'; |
||
767 | } |
||
768 | |||
769 | throw new \InvalidArgumentException(); |
||
770 | } |
||
771 | ) |
||
772 | ); |
||
773 | |||
774 | $this |
||
775 | ->metadata |
||
776 | ->expects($this->any()) |
||
777 | ->method('hasAssociation') |
||
778 | ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('entities'))) |
||
779 | ->will( |
||
780 | $this->returnCallback( |
||
781 | function ($arg) { |
||
782 | if ($arg === 'id') { |
||
783 | return false; |
||
784 | } elseif ($arg === 'field') { |
||
785 | return 'string'; |
||
786 | } elseif ($arg === 'entities') { |
||
787 | return true; |
||
788 | } |
||
789 | |||
790 | throw new \InvalidArgumentException(); |
||
791 | } |
||
792 | ) |
||
793 | ); |
||
794 | |||
795 | $this |
||
796 | ->metadata |
||
797 | ->expects($this->any()) |
||
798 | ->method('isSingleValuedAssociation') |
||
799 | ->with('entities') |
||
800 | ->will($this->returnValue(false)); |
||
801 | |||
802 | $this |
||
803 | ->metadata |
||
804 | ->expects($this->any()) |
||
805 | ->method('isCollectionValuedAssociation') |
||
806 | ->with('entities') |
||
807 | ->will($this->returnValue(true)); |
||
808 | |||
809 | $this |
||
810 | ->metadata |
||
811 | ->expects($this->any()) |
||
812 | ->method('getAssociationTargetClass') |
||
813 | ->with('entities') |
||
814 | ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity')); |
||
815 | |||
816 | $this |
||
817 | ->metadata |
||
818 | ->expects($this->any()) |
||
819 | ->method('getReflectionClass') |
||
820 | ->will($this->returnValue($refl)); |
||
821 | |||
822 | $this->metadata |
||
823 | ->expects($this->any()) |
||
824 | ->method('getIdentifier') |
||
825 | ->will($this->returnValue(array("id"))); |
||
826 | |||
827 | $this->hydratorByValue = new DoctrineObjectHydrator( |
||
828 | $this->objectManager, |
||
829 | true |
||
830 | ); |
||
831 | $this->hydratorByReference = new DoctrineObjectHydrator( |
||
832 | $this->objectManager, |
||
833 | false |
||
834 | ); |
||
835 | } |
||
836 | |||
837 | public function testObjectIsPassedForContextToStrategies() |
||
853 | |||
854 | public function testCanExtractSimpleEntityByValue() |
||
866 | |||
867 | public function testCanExtractSimpleEntityByReference() |
||
879 | |||
880 | public function testCanHydrateSimpleEntityByValue() |
||
892 | |||
893 | /** |
||
894 | * When using hydration by value, it will use the public API of the entity to set values (setters) |
||
895 | * |
||
896 | * @covers \DoctrineModule\Stdlib\Hydrator\DoctrineObject::hydrateByValue |
||
897 | */ |
||
898 | public function testCanHydrateSimpleEntityWithStringIdByValue() |
||
910 | |||
911 | public function testCanHydrateSimpleEntityByReference() |
||
923 | |||
924 | /** |
||
925 | * When using hydration by reference, it won't use the public API of the entity to set values (getters) |
||
926 | * |
||
927 | * @covers \DoctrineModule\Stdlib\Hydrator\DoctrineObject::hydrateByReference |
||
928 | */ |
||
929 | public function testCanHydrateSimpleEntityWithStringIdByReference() |
||
941 | |||
942 | public function testReuseExistingEntityIfDataArrayContainsIdentifier() |
||
966 | |||
967 | /** |
||
968 | * Test for https://github.com/doctrine/DoctrineModule/issues/456 |
||
969 | */ |
||
970 | public function testReuseExistingEntityIfDataArrayContainsIdentifierWithZeroIdentifier() |
||
971 | { |
||
972 | // When using hydration by reference, it won't use the public API of the entity to set values (setters) |
||
973 | $entity = new Asset\SimpleEntity(); |
||
974 | |||
975 | $this->configureObjectManagerForSimpleEntity(); |
||
976 | $data = array('id' => 0); |
||
977 | |||
978 | $entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
||
979 | $entityInDatabaseWithIdOfOne->setId(0); |
||
980 | $entityInDatabaseWithIdOfOne->setField('bar', false); |
||
981 | |||
982 | $this |
||
983 | ->objectManager |
||
984 | ->expects($this->once()) |
||
985 | ->method('find') |
||
986 | ->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', array('id' => 0)) |
||
987 | ->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
||
988 | |||
989 | $entity = $this->hydratorByValue->hydrate($data, $entity); |
||
990 | |||
991 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
||
992 | $this->assertEquals('bar', $entity->getField(false)); |
||
993 | } |
||
994 | |||
995 | public function testExtractOneToOneAssociationByValue() |
||
1015 | |||
1016 | public function testExtractOneToOneAssociationByReference() |
||
1036 | |||
1037 | public function testHydrateOneToOneAssociationByValue() |
||
1055 | |||
1056 | public function testHydrateOneToOneAssociationByReference() |
||
1074 | |||
1075 | public function testHydrateOneToOneAssociationByValueUsingIdentifierForRelation() |
||
1101 | |||
1102 | public function testHydrateOneToOneAssociationByReferenceUsingIdentifierForRelation() |
||
1128 | |||
1129 | public function testHydrateOneToOneAssociationByValueUsingIdentifierArrayForRelation() |
||
1155 | |||
1156 | public function testHydrateOneToOneAssociationByValueUsingFullArrayForRelation() |
||
1194 | |||
1195 | public function testHydrateOneToOneAssociationByReferenceUsingIdentifierArrayForRelation() |
||
1221 | |||
1222 | public function testCanHydrateOneToOneAssociationByValueWithNullableRelation() |
||
1236 | |||
1237 | public function testCanHydrateOneToOneAssociationByReferenceWithNullableRelation() |
||
1251 | |||
1252 | public function testExtractOneToManyAssociationByValue() |
||
1281 | |||
1282 | /** |
||
1283 | * @depends testExtractOneToManyAssociationByValue |
||
1284 | */ |
||
1285 | public function testExtractOneToManyByValueWithArray() |
||
1314 | |||
1315 | public function testExtractOneToManyAssociationByReference() |
||
1344 | |||
1345 | /** |
||
1346 | * @depends testExtractOneToManyAssociationByReference |
||
1347 | */ |
||
1348 | public function testExtractOneToManyArrayByReference() |
||
1377 | |||
1378 | public function testHydrateOneToManyAssociationByValue() |
||
1416 | |||
1417 | /** |
||
1418 | * @depends testHydrateOneToManyAssociationByValue |
||
1419 | */ |
||
1420 | public function testHydrateOneToManyArrayByValue() |
||
1458 | |||
1459 | public function testHydrateOneToManyAssociationByReference() |
||
1497 | |||
1498 | /** |
||
1499 | * @depends testHydrateOneToManyAssociationByReference |
||
1500 | */ |
||
1501 | public function testHydrateOneToManyArrayByReference() |
||
1539 | |||
1540 | public function testHydrateOneToManyAssociationByValueUsingIdentifiersForRelations() |
||
1541 | { |
||
1542 | // When using hydration by value, it will use the public API of the entity to set values (setters) |
||
1543 | $entity = new Asset\OneToManyEntity(); |
||
1544 | $this->configureObjectManagerForOneToManyEntity(); |
||
1545 | |||
1546 | $data = array( |
||
1547 | 'entities' => array( |
||
1548 | 2, 3 |
||
1549 | ) |
||
1550 | ); |
||
1551 | |||
1552 | $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
||
1553 | $entityInDatabaseWithIdOfTwo->setId(2); |
||
1554 | $entityInDatabaseWithIdOfTwo->setField('foo', false); |
||
1555 | |||
1556 | $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
||
1557 | $entityInDatabaseWithIdOfThree->setId(3); |
||
1558 | $entityInDatabaseWithIdOfThree->setField('bar', false); |
||
1559 | |||
1560 | $this |
||
1561 | ->objectManager |
||
1562 | ->expects($this->exactly(2)) |
||
1563 | ->method('find') |
||
1564 | ->with( |
||
1565 | 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
||
1566 | $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
||
1567 | ) |
||
1568 | ->will( |
||
1569 | $this->returnCallback( |
||
1570 | function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
||
1571 | if ($arg['id'] === 2) { |
||
1572 | return $entityInDatabaseWithIdOfTwo; |
||
1573 | } elseif ($arg['id'] === 3) { |
||
1574 | return $entityInDatabaseWithIdOfThree; |
||
1575 | } |
||
1576 | |||
1577 | throw new \InvalidArgumentException(); |
||
1578 | } |
||
1579 | ) |
||
1580 | ); |
||
1581 | |||
1582 | $entity = $this->hydratorByValue->hydrate($data, $entity); |
||
1583 | |||
1584 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
||
1585 | |||
1586 | /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
||
1587 | $entities = $entity->getEntities(false); |
||
1588 | |||
1589 | foreach ($entities as $en) { |
||
1590 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
||
1591 | $this->assertInternalType('integer', $en->getId()); |
||
1592 | $this->assertContains('Modified from addEntities adder', $en->getField(false)); |
||
1593 | } |
||
1594 | |||
1595 | $this->assertEquals(2, $entities[0]->getId()); |
||
1596 | $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
||
1597 | |||
1598 | $this->assertEquals(3, $entities[1]->getId()); |
||
1599 | $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
||
1600 | } |
||
1601 | |||
1602 | public function testHydrateOneToManyAssociationByValueUsingIdentifiersArrayForRelations() |
||
1664 | |||
1665 | public function testHydrateOneToManyAssociationByReferenceUsingIdentifiersArrayForRelations() |
||
1666 | { |
||
1667 | |||
1668 | // When using hydration by value, it will use the public API of the entity to set values (setters) |
||
1669 | $entity = new Asset\OneToManyEntity(); |
||
1670 | $this->configureObjectManagerForOneToManyEntity(); |
||
1671 | |||
1672 | $data = array( |
||
1673 | 'entities' => array( |
||
1674 | array('id' => 2), |
||
1675 | array('id' => 3) |
||
1676 | ) |
||
1677 | ); |
||
1678 | |||
1679 | $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
||
1680 | $entityInDatabaseWithIdOfTwo->setId(2); |
||
1681 | $entityInDatabaseWithIdOfTwo->setField('foo', false); |
||
1682 | |||
1683 | $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
||
1684 | $entityInDatabaseWithIdOfThree->setId(3); |
||
1685 | $entityInDatabaseWithIdOfThree->setField('bar', false); |
||
1686 | |||
1687 | $this |
||
1688 | ->objectManager |
||
1689 | ->expects($this->exactly(2)) |
||
1690 | ->method('find') |
||
1691 | ->with( |
||
1692 | 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
||
1693 | $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
||
1694 | ) |
||
1695 | ->will( |
||
1696 | $this->returnCallback( |
||
1697 | function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
||
1698 | if ($arg['id'] === 2) { |
||
1699 | return $entityInDatabaseWithIdOfTwo; |
||
1700 | } elseif ($arg['id'] === 3) { |
||
1701 | return $entityInDatabaseWithIdOfThree; |
||
1702 | } |
||
1703 | |||
1704 | throw new \InvalidArgumentException(); |
||
1705 | } |
||
1706 | ) |
||
1707 | ); |
||
1708 | |||
1709 | $entity = $this->hydratorByReference->hydrate($data, $entity); |
||
1710 | |||
1711 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
||
1712 | |||
1713 | $entities = $entity->getEntities(false); |
||
1714 | |||
1715 | foreach ($entities as $en) { |
||
1716 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
||
1717 | $this->assertInternalType('integer', $en->getId()); |
||
1718 | $this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
||
1719 | } |
||
1720 | |||
1721 | $this->assertEquals(2, $entities[0]->getId()); |
||
1722 | $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
||
1723 | |||
1724 | $this->assertEquals(3, $entities[1]->getId()); |
||
1725 | $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
||
1726 | } |
||
1727 | |||
1728 | public function testHydrateOneToManyAssociationByReferenceUsingIdentifiersForRelations() |
||
1729 | { |
||
1730 | // When using hydration by reference, it won't use the public API of the entity to set values (setters) |
||
1731 | $entity = new Asset\OneToManyEntity(); |
||
1732 | $this->configureObjectManagerForOneToManyEntity(); |
||
1733 | |||
1734 | $data = array( |
||
1735 | 'entities' => array( |
||
1736 | 2, 3 |
||
1737 | ) |
||
1738 | ); |
||
1739 | |||
1740 | $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
||
1741 | $entityInDatabaseWithIdOfTwo->setId(2); |
||
1742 | $entityInDatabaseWithIdOfTwo->setField('foo', false); |
||
1743 | |||
1744 | $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
||
1745 | $entityInDatabaseWithIdOfThree->setId(3); |
||
1746 | $entityInDatabaseWithIdOfThree->setField('bar', false); |
||
1747 | |||
1748 | $this |
||
1749 | ->objectManager |
||
1750 | ->expects($this->any()) |
||
1751 | ->method('find') |
||
1752 | ->with( |
||
1753 | 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
||
1754 | $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
||
1755 | ) |
||
1756 | ->will( |
||
1757 | $this->returnCallback( |
||
1758 | function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
||
1759 | if ($arg['id'] === 2) { |
||
1760 | return $entityInDatabaseWithIdOfTwo; |
||
1761 | } elseif ($arg['id'] === 3) { |
||
1762 | return $entityInDatabaseWithIdOfThree; |
||
1763 | } |
||
1764 | |||
1765 | throw new \InvalidArgumentException(); |
||
1766 | } |
||
1767 | ) |
||
1768 | ); |
||
1769 | |||
1770 | $entity = $this->hydratorByReference->hydrate($data, $entity); |
||
1771 | |||
1772 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
||
1773 | |||
1774 | $entities = $entity->getEntities(false); |
||
1775 | |||
1776 | foreach ($entities as $en) { |
||
1777 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
||
1778 | $this->assertInternalType('integer', $en->getId()); |
||
1779 | $this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
||
1780 | } |
||
1781 | |||
1782 | $this->assertEquals(2, $entities[0]->getId()); |
||
1783 | $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
||
1784 | |||
1785 | $this->assertEquals(3, $entities[1]->getId()); |
||
1786 | $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
||
1787 | } |
||
1788 | |||
1789 | public function testHydrateOneToManyAssociationByValueUsingDisallowRemoveStrategy() |
||
1790 | { |
||
1791 | // When using hydration by value, it will use the public API of the entity to set values (setters) |
||
1792 | $toMany1 = new Asset\SimpleEntity(); |
||
1793 | $toMany1->setId(2); |
||
1794 | $toMany1->setField('foo', false); |
||
1795 | |||
1796 | $toMany2 = new Asset\SimpleEntity(); |
||
1797 | $toMany2->setId(3); |
||
1798 | $toMany2->setField('bar', false); |
||
1799 | |||
1800 | $toMany3 = new Asset\SimpleEntity(); |
||
1801 | $toMany3->setId(8); |
||
1802 | $toMany3->setField('baz', false); |
||
1803 | |||
1804 | $entity = new Asset\OneToManyEntity(); |
||
1805 | $this->configureObjectManagerForOneToManyEntity(); |
||
1806 | |||
1807 | // Initially add two elements |
||
1808 | $entity->addEntities(new ArrayCollection(array($toMany1, $toMany2))); |
||
1809 | |||
1810 | // The hydrated collection contains two other elements, one of them is new, and one of them is missing |
||
1811 | // in the new strategy |
||
1812 | $data = array( |
||
1813 | 'entities' => array( |
||
1814 | $toMany2, $toMany3 |
||
1815 | ) |
||
1816 | ); |
||
1817 | |||
1818 | // Use a DisallowRemove strategy |
||
1819 | $this->hydratorByValue->addStrategy('entities', new Strategy\DisallowRemoveByValue()); |
||
1820 | $entity = $this->hydratorByValue->hydrate($data, $entity); |
||
1821 | |||
1822 | $entities = $entity->getEntities(false); |
||
1823 | |||
1824 | // DisallowStrategy should not remove existing entities in Collection even if it's not in the new collection |
||
1825 | $this->assertEquals(3, count($entities)); |
||
1826 | |||
1827 | foreach ($entities as $en) { |
||
1828 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
||
1829 | $this->assertInternalType('integer', $en->getId()); |
||
1830 | } |
||
1831 | |||
1832 | $this->assertEquals(2, $entities[0]->getId()); |
||
1833 | $this->assertSame($toMany1, $entities[0]); |
||
1834 | |||
1835 | $this->assertEquals(3, $entities[1]->getId()); |
||
1836 | $this->assertSame($toMany2, $entities[1]); |
||
1837 | |||
1838 | $this->assertEquals(8, $entities[2]->getId()); |
||
1839 | $this->assertSame($toMany3, $entities[2]); |
||
1840 | } |
||
1841 | |||
1842 | public function testHydrateOneToManyAssociationByReferenceUsingDisallowRemoveStrategy() |
||
1843 | { |
||
1844 | // When using hydration by reference, it won't use the public API of the entity to set values (setters) |
||
1845 | $toMany1 = new Asset\SimpleEntity(); |
||
1846 | $toMany1->setId(2); |
||
1847 | $toMany1->setField('foo', false); |
||
1848 | |||
1849 | $toMany2 = new Asset\SimpleEntity(); |
||
1850 | $toMany2->setId(3); |
||
1851 | $toMany2->setField('bar', false); |
||
1852 | |||
1853 | $toMany3 = new Asset\SimpleEntity(); |
||
1854 | $toMany3->setId(8); |
||
1855 | $toMany3->setField('baz', false); |
||
1856 | |||
1857 | $entity = new Asset\OneToManyEntity(); |
||
1858 | $this->configureObjectManagerForOneToManyEntity(); |
||
1859 | |||
1860 | // Initially add two elements |
||
1861 | $entity->addEntities(new ArrayCollection(array($toMany1, $toMany2))); |
||
1862 | |||
1863 | // The hydrated collection contains two other elements, one of them is new, and one of them is missing |
||
1864 | // in the new strategy |
||
1865 | $data = array( |
||
1866 | 'entities' => array( |
||
1867 | $toMany2, $toMany3 |
||
1868 | ) |
||
1869 | ); |
||
1870 | |||
1871 | // Use a DisallowRemove strategy |
||
1872 | $this->hydratorByReference->addStrategy('entities', new Strategy\DisallowRemoveByReference()); |
||
1873 | $entity = $this->hydratorByReference->hydrate($data, $entity); |
||
1874 | |||
1875 | $entities = $entity->getEntities(false); |
||
1876 | |||
1877 | // DisallowStrategy should not remove existing entities in Collection even if it's not in the new collection |
||
1878 | $this->assertEquals(3, count($entities)); |
||
1879 | |||
1880 | foreach ($entities as $en) { |
||
1881 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
||
1882 | $this->assertInternalType('integer', $en->getId()); |
||
1883 | |||
1884 | // Only the third element is new so the adder has not been called on it |
||
1885 | if ($en === $toMany3) { |
||
1886 | $this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
||
1887 | } |
||
1888 | } |
||
1889 | |||
1890 | $this->assertEquals(2, $entities[0]->getId()); |
||
1891 | $this->assertSame($toMany1, $entities[0]); |
||
1892 | |||
1893 | $this->assertEquals(3, $entities[1]->getId()); |
||
1894 | $this->assertSame($toMany2, $entities[1]); |
||
1895 | |||
1896 | $this->assertEquals(8, $entities[2]->getId()); |
||
1897 | $this->assertSame($toMany3, $entities[2]); |
||
1898 | } |
||
1899 | |||
1900 | public function testHydrateOneToManyAssociationByValueWithArrayCausingDataModifications() |
||
1901 | { |
||
1902 | // When using hydration by value, it will use the public API of the entity to set values (setters) |
||
1903 | $data = array( |
||
1904 | 'entities' => array( |
||
1905 | array('id' => 2, 'field' => 'Modified By Hydrate'), |
||
1906 | array('id' => 3, 'field' => 'Modified By Hydrate') |
||
1907 | ) |
||
1908 | ); |
||
1909 | |||
1910 | $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
||
1911 | $entityInDatabaseWithIdOfTwo->setId(2); |
||
1912 | $entityInDatabaseWithIdOfTwo->setField('foo', false); |
||
1913 | |||
1914 | $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
||
1915 | $entityInDatabaseWithIdOfThree->setId(3); |
||
1916 | $entityInDatabaseWithIdOfThree->setField('bar', false); |
||
1917 | |||
1918 | $entity = new Asset\OneToManyEntityWithEntities( |
||
1919 | new ArrayCollection(array( |
||
1920 | $entityInDatabaseWithIdOfTwo, |
||
1921 | $entityInDatabaseWithIdOfThree |
||
1922 | )) |
||
1923 | ); |
||
1924 | $this->configureObjectManagerForOneToManyEntity(); |
||
1925 | |||
1926 | $this |
||
1927 | ->objectManager |
||
1928 | ->expects($this->exactly(2)) |
||
1929 | ->method('find') |
||
1930 | ->with( |
||
1931 | 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
||
1932 | $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
||
1933 | ) |
||
1934 | ->will( |
||
1935 | $this->returnCallback( |
||
1936 | function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
||
1937 | if ($arg['id'] === 2) { |
||
1938 | return $entityInDatabaseWithIdOfTwo; |
||
1939 | } elseif ($arg['id'] === 3) { |
||
1940 | return $entityInDatabaseWithIdOfThree; |
||
1941 | } |
||
1942 | |||
1943 | throw new \InvalidArgumentException(); |
||
1944 | } |
||
1945 | ) |
||
1946 | ); |
||
1947 | |||
1948 | $entity = $this->hydratorByValue->hydrate($data, $entity); |
||
1949 | |||
1950 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
||
1951 | |||
1952 | /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
||
1953 | $entities = $entity->getEntities(false); |
||
1954 | |||
1955 | foreach ($entities as $en) { |
||
1956 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
||
1957 | $this->assertInternalType('integer', $en->getId()); |
||
1958 | $this->assertInternalType('string', $en->getField()); |
||
1959 | $this->assertContains('Modified By Hydrate', $en->getField(false)); |
||
1960 | } |
||
1961 | |||
1962 | $this->assertEquals(2, $entities[0]->getId()); |
||
1963 | $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
||
1964 | |||
1965 | $this->assertEquals(3, $entities[1]->getId()); |
||
1966 | $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
||
1967 | } |
||
1968 | |||
1969 | |||
1970 | public function testHydrateOneToManyAssociationByValueWithTraversableCausingDataModifications() |
||
1971 | { |
||
1972 | // When using hydration by value, it will use the public API of the entity to set values (setters) |
||
1973 | $data = array( |
||
1974 | 'entities' => new ArrayCollection( |
||
1975 | array( |
||
1976 | array('id' => 2, 'field' => 'Modified By Hydrate'), |
||
1977 | array('id' => 3, 'field' => 'Modified By Hydrate') |
||
1978 | ) |
||
1979 | ) |
||
1980 | ); |
||
1981 | |||
1982 | $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
||
1983 | $entityInDatabaseWithIdOfTwo->setId(2); |
||
1984 | $entityInDatabaseWithIdOfTwo->setField('foo', false); |
||
1985 | |||
1986 | $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
||
1987 | $entityInDatabaseWithIdOfThree->setId(3); |
||
1988 | $entityInDatabaseWithIdOfThree->setField('bar', false); |
||
1989 | |||
1990 | $entity = new Asset\OneToManyEntityWithEntities( |
||
1991 | new ArrayCollection(array( |
||
1992 | $entityInDatabaseWithIdOfTwo, |
||
1993 | $entityInDatabaseWithIdOfThree |
||
1994 | )) |
||
1995 | ); |
||
1996 | $this->configureObjectManagerForOneToManyEntity(); |
||
1997 | |||
1998 | $this |
||
1999 | ->objectManager |
||
2000 | ->expects($this->exactly(2)) |
||
2001 | ->method('find') |
||
2002 | ->with( |
||
2003 | 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
||
2004 | $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
||
2005 | ) |
||
2006 | ->will( |
||
2007 | $this->returnCallback( |
||
2008 | function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
||
2009 | if ($arg['id'] === 2) { |
||
2010 | return $entityInDatabaseWithIdOfTwo; |
||
2011 | } elseif ($arg['id'] === 3) { |
||
2012 | return $entityInDatabaseWithIdOfThree; |
||
2013 | } |
||
2014 | |||
2015 | throw new \InvalidArgumentException(); |
||
2016 | } |
||
2017 | ) |
||
2018 | ); |
||
2019 | |||
2020 | $entity = $this->hydratorByValue->hydrate($data, $entity); |
||
2021 | |||
2022 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
||
2023 | |||
2024 | /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
||
2025 | $entities = $entity->getEntities(false); |
||
2026 | |||
2027 | foreach ($entities as $en) { |
||
2028 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
||
2029 | $this->assertInternalType('integer', $en->getId()); |
||
2030 | $this->assertInternalType('string', $en->getField()); |
||
2031 | $this->assertContains('Modified By Hydrate', $en->getField(false)); |
||
2032 | } |
||
2033 | |||
2034 | $this->assertEquals(2, $entities[0]->getId()); |
||
2035 | $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
||
2036 | |||
2037 | $this->assertEquals(3, $entities[1]->getId()); |
||
2038 | $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
||
2039 | } |
||
2040 | |||
2041 | public function testHydrateOneToManyAssociationByValueWithStdClass() |
||
2042 | { |
||
2043 | // When using hydration by value, it will use the public API of the entity to set values (setters) |
||
2044 | $stdClass1 = new \StdClass(); |
||
2045 | $stdClass1->id = 2; |
||
2046 | |||
2047 | $stdClass2 = new \StdClass(); |
||
2048 | $stdClass2->id = 3; |
||
2049 | |||
2050 | $data = array('entities' => array($stdClass1, $stdClass2)); |
||
2051 | |||
2052 | $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
||
2053 | $entityInDatabaseWithIdOfTwo->setId(2); |
||
2054 | $entityInDatabaseWithIdOfTwo->setField('foo', false); |
||
2055 | |||
2056 | $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
||
2057 | $entityInDatabaseWithIdOfThree->setId(3); |
||
2058 | $entityInDatabaseWithIdOfThree->setField('bar', false); |
||
2059 | |||
2060 | $entity = new Asset\OneToManyEntityWithEntities( |
||
2061 | new ArrayCollection(array( |
||
2062 | $entityInDatabaseWithIdOfTwo, |
||
2063 | $entityInDatabaseWithIdOfThree |
||
2064 | )) |
||
2065 | ); |
||
2066 | $this->configureObjectManagerForOneToManyEntity(); |
||
2067 | |||
2068 | $this |
||
2069 | ->objectManager |
||
2070 | ->expects($this->exactly(2)) |
||
2071 | ->method('find') |
||
2072 | ->with( |
||
2073 | 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
||
2074 | $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
||
2075 | ) |
||
2076 | ->will( |
||
2077 | $this->returnCallback( |
||
2078 | function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
||
2079 | if ($arg['id'] === 2) { |
||
2080 | return $entityInDatabaseWithIdOfTwo; |
||
2081 | } elseif ($arg['id'] === 3) { |
||
2082 | return $entityInDatabaseWithIdOfThree; |
||
2083 | } |
||
2084 | |||
2085 | throw new \InvalidArgumentException(); |
||
2086 | } |
||
2087 | ) |
||
2088 | ); |
||
2089 | |||
2090 | $entity = $this->hydratorByValue->hydrate($data, $entity); |
||
2091 | |||
2092 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
||
2093 | |||
2094 | /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
||
2095 | $entities = $entity->getEntities(false); |
||
2096 | |||
2097 | foreach ($entities as $en) { |
||
2098 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
||
2099 | $this->assertInternalType('integer', $en->getId()); |
||
2100 | } |
||
2101 | |||
2102 | $this->assertEquals(2, $entities[0]->getId()); |
||
2103 | $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
||
2104 | |||
2105 | $this->assertEquals(3, $entities[1]->getId()); |
||
2106 | $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
||
2107 | } |
||
2108 | |||
2109 | public function testHydrateOneToManyAssociationByReferenceWithArrayCausingDataModifications() |
||
2110 | { |
||
2111 | // When using hydration by value, it will use the public API of the entity to set values (setters) |
||
2112 | $data = array( |
||
2113 | 'entities' => array( |
||
2114 | array('id' => 2, 'field' => 'Modified By Hydrate'), |
||
2115 | array('id' => 3, 'field' => 'Modified By Hydrate') |
||
2116 | ) |
||
2117 | ); |
||
2118 | |||
2119 | $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
||
2120 | $entityInDatabaseWithIdOfTwo->setId(2); |
||
2121 | $entityInDatabaseWithIdOfTwo->setField('Unmodified Value', false); |
||
2122 | |||
2123 | $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
||
2124 | $entityInDatabaseWithIdOfThree->setId(3); |
||
2125 | $entityInDatabaseWithIdOfThree->setField('Unmodified Value', false); |
||
2126 | |||
2127 | $entity = new Asset\OneToManyEntityWithEntities( |
||
2128 | new ArrayCollection(array( |
||
2129 | $entityInDatabaseWithIdOfTwo, |
||
2130 | $entityInDatabaseWithIdOfThree |
||
2131 | )) |
||
2132 | ); |
||
2133 | |||
2134 | $reflSteps = array( |
||
2135 | new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities'), |
||
2136 | new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'), |
||
2137 | new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'), |
||
2138 | new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities'), |
||
2139 | ); |
||
2140 | $this |
||
2141 | ->metadata |
||
2142 | ->expects($this->any()) |
||
2143 | ->method('getReflectionClass') |
||
2144 | ->will($this->returnCallback( |
||
2145 | function () use (&$reflSteps) { |
||
2146 | $refl = array_shift($reflSteps); |
||
2147 | return $refl; |
||
2148 | } |
||
2149 | )); |
||
2150 | |||
2151 | $this->configureObjectManagerForOneToManyEntity(); |
||
2152 | |||
2153 | $this |
||
2154 | ->objectManager |
||
2155 | ->expects($this->exactly(2)) |
||
2156 | ->method('find') |
||
2157 | ->with( |
||
2158 | 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
||
2159 | $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
||
2160 | ) |
||
2161 | ->will( |
||
2162 | $this->returnCallback( |
||
2163 | function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
||
2164 | if ($arg['id'] === 2) { |
||
2165 | return $entityInDatabaseWithIdOfTwo; |
||
2166 | } elseif ($arg['id'] === 3) { |
||
2167 | return $entityInDatabaseWithIdOfThree; |
||
2168 | } |
||
2169 | |||
2170 | throw new \InvalidArgumentException(); |
||
2171 | } |
||
2172 | ) |
||
2173 | ); |
||
2174 | |||
2175 | $entity = $this->hydratorByReference->hydrate($data, $entity); |
||
2176 | |||
2177 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
||
2178 | |||
2179 | /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
||
2180 | $entities = $entity->getEntities(false); |
||
2181 | |||
2182 | foreach ($entities as $en) { |
||
2183 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
||
2184 | $this->assertInternalType('integer', $en->getId()); |
||
2185 | $this->assertInternalType('string', $en->getField()); |
||
2186 | $this->assertContains('Modified By Hydrate', $en->getField(false)); |
||
2187 | } |
||
2188 | |||
2189 | $this->assertEquals(2, $entities[0]->getId()); |
||
2190 | $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
||
2191 | |||
2192 | $this->assertEquals(3, $entities[1]->getId()); |
||
2193 | $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
||
2194 | } |
||
2195 | |||
2196 | public function testAssertCollectionsAreNotSwappedDuringHydration() |
||
2197 | { |
||
2198 | // When using hydration by value, it will use the public API of the entity to set values (setters) |
||
2199 | $entity = new Asset\OneToManyEntity(); |
||
2200 | $this->configureObjectManagerForOneToManyEntity(); |
||
2201 | |||
2202 | $toMany1 = new Asset\SimpleEntity(); |
||
2203 | $toMany1->setId(2); |
||
2204 | $toMany1->setField('foo', false); |
||
2205 | |||
2206 | $toMany2 = new Asset\SimpleEntity(); |
||
2207 | $toMany2->setId(3); |
||
2208 | $toMany2->setField('bar', false); |
||
2209 | |||
2210 | $data = array( |
||
2211 | 'entities' => array( |
||
2212 | $toMany1, $toMany2 |
||
2213 | ) |
||
2214 | ); |
||
2215 | |||
2216 | // Set the initial collection |
||
2217 | $entity->addEntities(new ArrayCollection(array($toMany1, $toMany2))); |
||
2218 | $initialCollection = $entity->getEntities(false); |
||
2219 | |||
2220 | $entity = $this->hydratorByValue->hydrate($data, $entity); |
||
2221 | |||
2222 | $modifiedCollection = $entity->getEntities(false); |
||
2223 | $this->assertSame($initialCollection, $modifiedCollection); |
||
2224 | } |
||
2225 | |||
2226 | public function testAssertCollectionsAreNotSwappedDuringHydrationUsingIdentifiersForRelations() |
||
2227 | { |
||
2228 | // When using hydration by value, it will use the public API of the entity to set values (setters) |
||
2229 | $entity = new Asset\OneToManyEntity(); |
||
2230 | $this->configureObjectManagerForOneToManyEntity(); |
||
2231 | |||
2232 | $data = array( |
||
2233 | 'entities' => array( |
||
2234 | 2, 3 |
||
2235 | ) |
||
2236 | ); |
||
2237 | |||
2238 | $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
||
2239 | $entityInDatabaseWithIdOfTwo->setId(2); |
||
2240 | $entityInDatabaseWithIdOfTwo->setField('foo', false); |
||
2241 | |||
2242 | $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
||
2243 | $entityInDatabaseWithIdOfThree->setId(3); |
||
2244 | $entityInDatabaseWithIdOfThree->setField('bar', false); |
||
2245 | |||
2246 | // Set the initial collection |
||
2247 | $entity->addEntities(new ArrayCollection(array($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree))); |
||
2248 | $initialCollection = $entity->getEntities(false); |
||
2249 | |||
2250 | $this |
||
2251 | ->objectManager |
||
2252 | ->expects($this->any()) |
||
2253 | ->method('find') |
||
2254 | ->with( |
||
2255 | 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
||
2256 | $this->logicalOr($this->equalTo(array('id' =>2)), $this->equalTo(array('id' => 3))) |
||
2257 | ) |
||
2258 | ->will( |
||
2259 | $this->returnCallback( |
||
2260 | function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
||
2261 | if ($arg['id'] === 2) { |
||
2262 | return $entityInDatabaseWithIdOfTwo; |
||
2263 | } |
||
2264 | |||
2265 | return $entityInDatabaseWithIdOfThree; |
||
2266 | } |
||
2267 | ) |
||
2268 | ); |
||
2269 | |||
2270 | $entity = $this->hydratorByValue->hydrate($data, $entity); |
||
2271 | |||
2272 | $modifiedCollection = $entity->getEntities(false); |
||
2273 | $this->assertSame($initialCollection, $modifiedCollection); |
||
2274 | } |
||
2275 | |||
2276 | public function testCanLookupsForEmptyIdentifiers() |
||
2277 | { |
||
2278 | // When using hydration by reference, it won't use the public API of the entity to set values (setters) |
||
2279 | $entity = new Asset\OneToManyEntity(); |
||
2280 | $this->configureObjectManagerForOneToManyEntity(); |
||
2281 | |||
2282 | $data = array( |
||
2283 | 'entities' => array( |
||
2284 | '' |
||
2285 | ) |
||
2286 | ); |
||
2287 | |||
2288 | $entityInDatabaseWithEmptyId = new Asset\SimpleEntity(); |
||
2289 | $entityInDatabaseWithEmptyId->setId(''); |
||
2290 | $entityInDatabaseWithEmptyId->setField('baz', false); |
||
2291 | |||
2292 | $this |
||
2293 | ->objectManager |
||
2294 | ->expects($this->any()) |
||
2295 | ->method('find') |
||
2296 | ->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', '') |
||
2297 | ->will($this->returnValue($entityInDatabaseWithEmptyId)); |
||
2298 | |||
2299 | $entity = $this->hydratorByValue->hydrate($data, $entity); |
||
2300 | |||
2301 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
||
2302 | |||
2303 | $entities = $entity->getEntities(false); |
||
2304 | $entity = $entities[0]; |
||
2305 | |||
2306 | $this->assertEquals(1, count($entities)); |
||
2307 | |||
2308 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
||
2309 | $this->assertSame($entityInDatabaseWithEmptyId, $entity); |
||
2310 | } |
||
2311 | |||
2312 | public function testHandleDateTimeConversionUsingByValue() |
||
2326 | |||
2327 | public function testEmptyStringIsNotConvertedToDateTime() |
||
2338 | |||
2339 | public function testAssertNullValueHydratedForOneToOneWithOptionalMethodSignature() |
||
2352 | |||
2353 | public function testAssertNullValueNotUsedAsIdentifierForOneToOneWithNonOptionalMethodSignature() |
||
2354 | { |
||
2355 | $entity = new Asset\OneToOneEntityNotNullable(); |
||
2356 | |||
2357 | $entity->setToOne(new Asset\SimpleEntity()); |
||
2358 | $this->configureObjectManagerForOneToOneEntityNotNullable(); |
||
2359 | $this->objectManager->expects($this->never())->method('find'); |
||
2360 | |||
2361 | $data = array('toOne' => null); |
||
2362 | |||
2363 | $object = $this->hydratorByValue->hydrate($data, $entity); |
||
2364 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $object->getToOne(false)); |
||
2365 | } |
||
2366 | |||
2367 | public function testUsesStrategyOnSimpleFieldsWhenHydratingByValue() |
||
2368 | { |
||
2369 | // When using hydration by value, it will use the public API of the entity to set values (setters) |
||
2370 | $entity = new Asset\SimpleEntity(); |
||
2371 | $this->configureObjectManagerForSimpleEntity(); |
||
2372 | $data = array('field' => 'foo'); |
||
2373 | |||
2374 | $this->hydratorByValue->addStrategy('field', new Asset\SimpleStrategy()); |
||
2375 | $entity = $this->hydratorByValue->hydrate($data, $entity); |
||
2376 | |||
2377 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
||
2378 | $this->assertEquals('From setter: modified while hydrating', $entity->getField(false)); |
||
2379 | } |
||
2380 | |||
2381 | public function testUsesStrategyOnSimpleFieldsWhenHydratingByReference() |
||
2394 | |||
2395 | public function testUsesStrategyOnSimpleFieldsWhenExtractingByValue() |
||
2408 | |||
2409 | public function testUsesStrategyOnSimpleFieldsWhenExtractingByReference() |
||
2422 | |||
2423 | public function testCanExtractIsserByValue() |
||
2424 | { |
||
2425 | $entity = new Asset\SimpleIsEntity(); |
||
2426 | $entity->setId(2); |
||
2427 | $entity->setDone(true); |
||
2428 | |||
2429 | $this->configureObjectManagerForSimpleIsEntity(); |
||
2430 | |||
2431 | $data = $this->hydratorByValue->extract($entity); |
||
2432 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleIsEntity', $entity); |
||
2433 | $this->assertEquals(array('id' => 2, 'done' => true), $data); |
||
2434 | } |
||
2435 | |||
2436 | public function testCanExtractIsserThatStartsWithIsByValue() |
||
2437 | { |
||
2438 | $entity = new Asset\SimpleEntityWithIsBoolean(); |
||
2439 | $entity->setId(2); |
||
2440 | $entity->setIsActive(true); |
||
2441 | |||
2442 | $this->configureObjectManagerForSimpleEntityWithIsBoolean(); |
||
2443 | |||
2444 | $data = $this->hydratorByValue->extract($entity); |
||
2445 | $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithIsBoolean', $entity); |
||
2446 | $this->assertEquals(array('id' => 2, 'isActive' => true), $data); |
||
2447 | } |
||
2448 | |||
2449 | public function testExtractWithPropertyNameFilterByValue() |
||
2465 | |||
2466 | public function testExtractWithPropertyNameFilterByReference() |
||
2482 | |||
2483 | public function testExtractByReferenceUsesNamingStrategy() |
||
2484 | { |
||
2485 | $this->configureObjectManagerForNamingStrategyEntity(); |
||
2486 | $name = 'Foo'; |
||
2487 | $this->hydratorByReference->setNamingStrategy(new UnderscoreNamingStrategy()); |
||
2488 | $data = $this->hydratorByReference->extract(new NamingStrategyEntity($name)); |
||
2489 | $this->assertEquals($name, $data['camel_case']); |
||
2490 | } |
||
2491 | |||
2492 | public function testExtractByValueUsesNamingStrategy() |
||
2493 | { |
||
2494 | $this->configureObjectManagerForNamingStrategyEntity(); |
||
2495 | $name = 'Bar'; |
||
2496 | $this->hydratorByValue->setNamingStrategy(new UnderscoreNamingStrategy()); |
||
2497 | $data = $this->hydratorByValue->extract(new NamingStrategyEntity($name)); |
||
2498 | $this->assertEquals($name, $data['camel_case']); |
||
2499 | } |
||
2500 | |||
2501 | public function testHydrateByReferenceUsesNamingStrategy() |
||
2502 | { |
||
2503 | $this->configureObjectManagerForNamingStrategyEntity(); |
||
2504 | $name = 'Baz'; |
||
2505 | $this->hydratorByReference->setNamingStrategy(new UnderscoreNamingStrategy()); |
||
2506 | $entity = $this->hydratorByReference->hydrate(array('camel_case' => $name), new NamingStrategyEntity()); |
||
2507 | $this->assertEquals($name, $entity->getCamelCase()); |
||
2509 | |||
2510 | public function testHydrateByValueUsesNamingStrategy() |
||
2518 | |||
2519 | public function configureObjectManagerForSimplePrivateEntity() |
||
2574 | |||
2575 | public function testCannotHydratePrivateByValue() |
||
2585 | } |
||
2586 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: