1 | <?php |
||
36 | class HydratorFunctionalTest extends TestCase |
||
37 | { |
||
38 | /** |
||
39 | * @dataProvider getHydratorClasses |
||
40 | */ |
||
41 | public function testHydrator(object $instance) : void |
||
42 | { |
||
43 | $reflection = new ReflectionClass($instance); |
||
44 | $initialData = []; |
||
45 | $newData = []; |
||
46 | |||
47 | $this->recursiveFindInitialData($reflection, $instance, $initialData, $newData); |
||
48 | |||
49 | $generatedClass = $this->generateHydrator($instance); |
||
50 | |||
51 | // Hydration and extraction don't guarantee ordering. |
||
52 | ksort($initialData); |
||
53 | ksort($newData); |
||
54 | $extracted = $generatedClass->extract($instance); |
||
55 | ksort($extracted); |
||
56 | |||
57 | self::assertSame($initialData, $extracted); |
||
58 | self::assertSame($instance, $generatedClass->hydrate($newData, $instance)); |
||
59 | |||
60 | // Same as upper applies |
||
61 | $inspectionData = []; |
||
62 | $this->recursiveFindInspectionData($reflection, $instance, $inspectionData); |
||
63 | ksort($inspectionData); |
||
64 | $extracted = $generatedClass->extract($instance); |
||
65 | ksort($extracted); |
||
66 | |||
67 | self::assertSame($inspectionData, $newData); |
||
68 | self::assertSame($inspectionData, $extracted); |
||
69 | } |
||
70 | |||
71 | public function testHydratingNull() : void |
||
72 | { |
||
73 | $instance = new ClassWithPrivateProperties(); |
||
74 | |||
75 | self::assertSame('property0', $instance->getProperty0()); |
||
76 | |||
77 | $this->generateHydrator($instance)->hydrate(['property0' => null], $instance); |
||
|
|||
78 | |||
79 | self::assertNull($instance->getProperty0()); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Ensures that the hydrator will not attempt to read unitialized PHP >= 7.4 |
||
84 | * typed property, which would cause "Uncaught Error: Typed property Foo::$a |
||
85 | * must not be accessed before initialization" PHP engine errors. |
||
86 | * |
||
87 | * @requires PHP >= 7.4 |
||
88 | */ |
||
89 | public function testHydratorWillNotRaisedUnitiliazedTypedPropertyAccessError() : void |
||
90 | { |
||
91 | $instance = new ClassWithTypedProperties(); |
||
92 | $hydrator = $this->generateHydrator($instance); |
||
93 | |||
94 | $hydrator->hydrate(['property2' => 3], $instance); |
||
95 | |||
96 | self::assertSame([ |
||
97 | 'property0' => 1, // 'property0' has a default value, it should keep it. |
||
98 | 'property1' => 2, // 'property1' has a default value, it should keep it. |
||
99 | 'property2' => 3, |
||
100 | 'property3' => null, // 'property3' is not required, it should remain null. |
||
101 | 'property4' => null, // 'property4' default value is null, it should remain null. |
||
102 | 'untyped0' => null, // 'untyped0' is null by default |
||
103 | 'untyped1' => null, // 'untyped1' is null by default, |
||
104 | 'property5' => 'test' |
||
105 | ], $hydrator->extract($instance)); |
||
106 | } |
||
107 | |||
108 | public function testHydratorWithMappedFromAnnotation() : void |
||
109 | { |
||
110 | $instance = new ClassWithMappedProperties(); |
||
111 | $hydrator = $this->generateHydrator($instance); |
||
112 | |||
113 | $hydratedInstance = $hydrator->hydrate([ |
||
114 | 'test0' => 9, |
||
115 | 'test1' => 8, |
||
116 | 'test2' => 7, |
||
117 | 'test3' => 6, |
||
118 | ], $instance); |
||
119 | |||
120 | self::assertSame(9, $hydratedInstance->property0); |
||
121 | self::assertSame(8, $hydratedInstance->property1); |
||
122 | self::assertSame(7, $hydratedInstance->property2); |
||
123 | self::assertSame(6, $hydratedInstance->property3); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @requires PHP >= 7.4 |
||
128 | */ |
||
129 | public function testHydratorWillSetAllTypedProperties() : void |
||
130 | { |
||
131 | $instance = new ClassWithTypedProperties(); |
||
132 | $hydrator = $this->generateHydrator($instance); |
||
133 | |||
134 | $reference = [ |
||
135 | 'property0' => 11, |
||
136 | 'property1' => null, // Ensure explicit set null works as expected. |
||
137 | 'property2' => 13, |
||
138 | 'property3' => null, // Different use case (unrequired value with no default value). |
||
139 | 'property4' => 19, |
||
140 | 'untyped0' => null, // 'untyped0' is null by default |
||
141 | 'untyped1' => null, // 'untyped1' is null by default |
||
142 | 'property5' => 'test' |
||
143 | ]; |
||
144 | |||
145 | $hydrator->hydrate($reference, $instance); |
||
146 | |||
147 | self::assertSame($reference, $hydrator->extract($instance)); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @return mixed[] |
||
152 | */ |
||
153 | public function getHydratorClasses() : array |
||
169 | |||
170 | /** |
||
171 | * Recursively populate the $initialData and $newData array browsing the |
||
172 | * full class hierarchy tree |
||
173 | * |
||
174 | * Private properties from parent class that are hidden by children will be |
||
175 | * dropped from the populated arrays |
||
176 | * |
||
177 | * @param mixed[] $initialData |
||
178 | * @param mixed[] $newData |
||
179 | */ |
||
180 | private function recursiveFindInitialData( |
||
203 | |||
204 | /** |
||
205 | * Recursively populate the $inspectedData array browsing the full class |
||
206 | * hierarchy tree |
||
207 | * |
||
208 | * Private properties from parent class that are hidden by children will be |
||
209 | * dropped from the populated arrays |
||
210 | * |
||
211 | * @param mixed[] $inspectionData |
||
212 | */ |
||
213 | private function recursiveFindInspectionData( |
||
234 | |||
235 | /** |
||
236 | * Generates a hydrator for the given class name, and retrieves its class name |
||
237 | */ |
||
238 | private function generateHydrator(object $instance) : HydratorInterface |
||
264 | } |
||
265 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: