|
7 | 7 | use EventSauce\ObjectHydrator\Fixtures\ClassThatCastsListsToBasedOnDocComments; |
8 | 8 | use EventSauce\ObjectHydrator\Fixtures\ClassThatCastsListsToDifferentTypes; |
9 | 9 | use EventSauce\ObjectHydrator\Fixtures\ClassThatCastsListToScalarType; |
| 10 | +use EventSauce\ObjectHydrator\Fixtures\ClassThatHasMultipleCastersOnMapProperty; |
10 | 11 | use EventSauce\ObjectHydrator\Fixtures\ClassThatHasMultipleCastersOnSingleProperty; |
| 12 | +use EventSauce\ObjectHydrator\Fixtures\ClassThatSpecifiesArraysWithDocComments; |
| 13 | +use EventSauce\ObjectHydrator\Fixtures\ClassThatSpecifiesArrayWithIntegerKeys; |
11 | 14 | use EventSauce\ObjectHydrator\Fixtures\ClassWithCamelCaseProperty; |
12 | 15 | use EventSauce\ObjectHydrator\Fixtures\ClassWithPropertyCasting; |
13 | 16 | use EventSauce\ObjectHydrator\Fixtures\ClassWithStaticConstructor; |
|
22 | 25 |
|
23 | 26 | abstract class HydratingSerializedObjectsTestCase extends TestCase |
24 | 27 | { |
25 | | - abstract public function objectMapper(): ObjectMapper; |
| 28 | + abstract public function objectMapper(bool $serializeMapsAsObjects = false): ObjectMapper; |
26 | 29 |
|
27 | 30 | /** |
28 | 31 | * @test |
@@ -137,10 +140,204 @@ public function dataProvider(): iterable |
137 | 140 | } |
138 | 141 | } |
139 | 142 |
|
| 143 | + /** |
| 144 | + * @test |
| 145 | + * @dataProvider arrayDataProvider |
| 146 | + */ |
| 147 | + public function serializes_associative_arrays_as_objects_based_on_configuration( |
| 148 | + string $class, |
| 149 | + bool $serializeMapsAsObjects, |
| 150 | + array $input, |
| 151 | + array $types, |
| 152 | + ): void { |
| 153 | + $mapper = $this->objectMapper(serializeMapsAsObjects: $serializeMapsAsObjects); |
| 154 | + |
| 155 | + $object = $mapper->hydrateObject($class, $input); |
| 156 | + $payload = $mapper->serializeObject($object); |
| 157 | + |
| 158 | + self::assertInstanceOf($class, $object); |
| 159 | + self::assertEquals($input, $payload); |
| 160 | + self::assertExpectedTypes($types, $object); |
| 161 | + } |
| 162 | + |
| 163 | + public function arrayDataProvider(): iterable |
| 164 | + { |
| 165 | + yield 'associative arrays as objects when casting enabled' => [ |
| 166 | + ClassThatSpecifiesArraysWithDocComments::class, |
| 167 | + true, |
| 168 | + [ |
| 169 | + 'map_with_objects' => (object) [ |
| 170 | + 'frank' => ['snake_case' => 'Frank'], |
| 171 | + 'renske' => ['snake_case' => 'Renske'], |
| 172 | + ], |
| 173 | + 'map_with_scalars' => (object) ['one' => 1, 'two' => 2], |
| 174 | + 'map_with_associative_arrays' => (object) [ |
| 175 | + 'one' => ['key' => 'value'], |
| 176 | + 'two' => ['another_key' => 'another_value'], |
| 177 | + ], |
| 178 | + 'list_without_type_hint' => ['Frank', 'Renske'], |
| 179 | + 'list_with_type_hint' => ['Frank', 'Renske'], |
| 180 | + 'method_map_with_objects' => (object) [ |
| 181 | + 'frank' => ['snake_case' => 'Frank'], |
| 182 | + 'renske' => ['snake_case' => 'Renske'], |
| 183 | + ], |
| 184 | + 'method_map_with_scalars' => (object) ['one' => 1, 'two' => 2 ], |
| 185 | + 'method_map_with_associative_arrays' => (object) [ |
| 186 | + 'one' => ['key' => 'value'], |
| 187 | + 'two' => ['another_key' => 'another_value'], |
| 188 | + ], |
| 189 | + 'method_list_without_type_hint' => ['Frank', 'Renske'], |
| 190 | + 'method_list_with_type_hint' => ['Frank', 'Renske'], |
| 191 | + ], |
| 192 | + [ |
| 193 | + 'mapWithObjects' => ['type' => 'map', 'values' => ClassWithCamelCaseProperty::class], |
| 194 | + 'mapWithScalars' => ['type' => 'map', 'values' => 'integer'], |
| 195 | + 'mapWithAssociativeArrays' => ['type' => 'map', 'values' => 'array'], |
| 196 | + 'listWithoutTypeHint' => ['type' => 'list', 'values' => 'string'], |
| 197 | + 'listWithTypeHint' => ['type' => 'list', 'values' => 'string'], |
| 198 | + 'methodMapWithObjects' => ['type' => 'map', 'values' => ClassWithCamelCaseProperty::class], |
| 199 | + 'methodMapWithScalars' => ['type' => 'map', 'values' => 'integer'], |
| 200 | + 'methodMapWithAssociativeArrays' => ['type' => 'map', 'values' => 'array'], |
| 201 | + 'methodListWithoutTypeHint' => ['type' => 'list', 'values' => 'string'], |
| 202 | + 'methodListWithTypeHint' => ['type' => 'list', 'values' => 'string'], |
| 203 | + ] |
| 204 | + ]; |
| 205 | + |
| 206 | + yield 'associative arrays as arrays when casting disabled' => [ |
| 207 | + ClassThatSpecifiesArraysWithDocComments::class, |
| 208 | + false, |
| 209 | + [ |
| 210 | + 'map_with_objects' => [ |
| 211 | + 'frank' => ['snake_case' => 'Frank'], |
| 212 | + 'renske' => ['snake_case' => 'Renske'], |
| 213 | + ], |
| 214 | + 'map_with_scalars' => ['one' => 1, 'two' => 2], |
| 215 | + 'map_with_associative_arrays' => [ |
| 216 | + 'one' => ['key' => 'value'], |
| 217 | + 'two' => ['another_key' => 'another_value'], |
| 218 | + ], |
| 219 | + 'list_without_type_hint' => ['Frank', 'Renske'], |
| 220 | + 'list_with_type_hint' => ['Frank', 'Renske'], |
| 221 | + 'method_map_with_objects' => [ |
| 222 | + 'frank' => ['snake_case' => 'Frank'], |
| 223 | + 'renske' => ['snake_case' => 'Renske'], |
| 224 | + ], |
| 225 | + 'method_map_with_scalars' => ['one' => 1, 'two' => 2 ], |
| 226 | + 'method_map_with_associative_arrays' => [ |
| 227 | + 'one' => ['key' => 'value'], |
| 228 | + 'two' => ['another_key' => 'another_value'], |
| 229 | + ], |
| 230 | + 'method_list_without_type_hint' => ['Frank', 'Renske'], |
| 231 | + 'method_list_with_type_hint' => ['Frank', 'Renske'], |
| 232 | + ], |
| 233 | + [ |
| 234 | + 'mapWithObjects' => ['type' => 'map', 'values' => ClassWithCamelCaseProperty::class], |
| 235 | + 'mapWithScalars' => ['type' => 'map', 'values' => 'integer'], |
| 236 | + 'mapWithAssociativeArrays' => ['type' => 'map', 'values' => 'array'], |
| 237 | + 'listWithoutTypeHint' => ['type' => 'list', 'values' => 'string'], |
| 238 | + 'listWithTypeHint' => ['type' => 'list', 'values' => 'string'], |
| 239 | + 'methodMapWithObjects' => ['type' => 'map', 'values' => ClassWithCamelCaseProperty::class], |
| 240 | + 'methodMapWithScalars' => ['type' => 'map', 'values' => 'integer'], |
| 241 | + 'methodMapWithAssociativeArrays' => ['type' => 'map', 'values' => 'array'], |
| 242 | + 'methodListWithoutTypeHint' => ['type' => 'list', 'values' => 'string'], |
| 243 | + 'methodListWithTypeHint' => ['type' => 'list', 'values' => 'string'], |
| 244 | + ] |
| 245 | + ]; |
| 246 | + |
| 247 | + yield 'non-sequential lists serialized as objects when casting enabled' => [ |
| 248 | + ClassThatSpecifiesArrayWithIntegerKeys::class, |
| 249 | + true, |
| 250 | + [ |
| 251 | + 'array_with_integer_keys' => (object) [0 => 'zero', 2 => 'two'], |
| 252 | + ], |
| 253 | + [ |
| 254 | + 'arrayWithIntegerKeys' => ['type' => 'map', 'values' => 'string'], |
| 255 | + ], |
| 256 | + ]; |
| 257 | + |
| 258 | + yield 'non-sequential lists serialized as arrays when casting disabled' => [ |
| 259 | + ClassThatSpecifiesArrayWithIntegerKeys::class, |
| 260 | + false, |
| 261 | + [ |
| 262 | + 'array_with_integer_keys' => [0 => 'zero', 2 => 'two'], |
| 263 | + ], |
| 264 | + [ |
| 265 | + 'arrayWithIntegerKeys' => ['type' => 'map', 'values' => 'string'], |
| 266 | + ], |
| 267 | + ]; |
| 268 | + |
| 269 | + yield 'sequential arrays serialized as arrays when casting enabled' => [ |
| 270 | + ClassThatSpecifiesArrayWithIntegerKeys::class, |
| 271 | + true, |
| 272 | + [ |
| 273 | + 'array_with_integer_keys' => [0 => 'zero', 1 => 'one'], |
| 274 | + ], |
| 275 | + [ |
| 276 | + 'arrayWithIntegerKeys' => ['type' => 'list', 'values' => 'string'], |
| 277 | + ], |
| 278 | + ]; |
| 279 | + |
| 280 | + yield 'sequential arrays serialized as arrays when casting disabled' => [ |
| 281 | + ClassThatSpecifiesArrayWithIntegerKeys::class, |
| 282 | + false, |
| 283 | + [ |
| 284 | + 'array_with_integer_keys' => [0 => 'zero', 1 => 'one'], |
| 285 | + ], |
| 286 | + [ |
| 287 | + 'arrayWithIntegerKeys' => ['type' => 'list', 'values' => 'string'], |
| 288 | + ], |
| 289 | + ]; |
| 290 | + } |
| 291 | + |
| 292 | + /** |
| 293 | + * @test |
| 294 | + * @dataProvider associativeArraysWithPropertySerializersDataProvider |
| 295 | + */ |
| 296 | + public function serializes_associative_arrays_with_property_serializers_as_objects( |
| 297 | + bool $serializeMapsAsObjects, |
| 298 | + ClassThatHasMultipleCastersOnMapProperty $expectedObject, |
| 299 | + array $input, |
| 300 | + ): void { |
| 301 | + $mapper = $this->objectMapper($serializeMapsAsObjects); |
| 302 | + |
| 303 | + $object = $mapper->hydrateObject(ClassThatHasMultipleCastersOnMapProperty::class, $input); |
| 304 | + self::assertEquals($expectedObject, $object); |
| 305 | + |
| 306 | + $payload = $mapper->serializeObject($object); |
| 307 | + self::assertEquals($input, $payload); |
| 308 | + } |
| 309 | + |
| 310 | + private function associativeArraysWithPropertySerializersDataProvider(): iterable |
| 311 | + { |
| 312 | + yield 'associative arrays as objects' => [ |
| 313 | + true, |
| 314 | + new ClassThatHasMultipleCastersOnMapProperty([ |
| 315 | + 'first_level' => [ |
| 316 | + 'second_level' => ['one' => 1, 'two' => 2, 'three' => 3], |
| 317 | + ], |
| 318 | + ]), |
| 319 | + [ |
| 320 | + 'map' => (object) ['one' => 1, 'two' => 2, 'three' => 3], |
| 321 | + ], |
| 322 | + ]; |
| 323 | + |
| 324 | + yield 'associative arrays as arrays' => [ |
| 325 | + false, |
| 326 | + new ClassThatHasMultipleCastersOnMapProperty([ |
| 327 | + 'first_level' => [ |
| 328 | + 'second_level' => ['one' => 1, 'two' => 2, 'three' => 3], |
| 329 | + ], |
| 330 | + ]), |
| 331 | + [ |
| 332 | + 'map' => ['one' => 1, 'two' => 2, 'three' => 3], |
| 333 | + ], |
| 334 | + ]; |
| 335 | + } |
| 336 | + |
140 | 337 | private static function assertExpectedTypes(array $types, object $object): void |
141 | 338 | { |
142 | 339 | foreach ($types as $property => $type) { |
143 | | - $value = $object->$property; |
| 340 | + $value = property_exists($object, $property) ? $object->{$property} : $object->$property(); |
144 | 341 |
|
145 | 342 | self::assertExpectedType($type['type'], $value); |
146 | 343 |
|
@@ -200,8 +397,10 @@ private static function assertArrayIsMap(mixed $value): void |
200 | 397 | { |
201 | 398 | self::assertIsArray($value); |
202 | 399 |
|
| 400 | + self::assertFalse(array_is_list($value)); |
| 401 | + |
203 | 402 | foreach (array_keys($value) as $key) { |
204 | | - self::assertIsString($key); |
| 403 | + self::assertIsScalar($key); |
205 | 404 | } |
206 | 405 | } |
207 | 406 | } |
0 commit comments