2424use JMS \Serializer \Tests \Fixtures \ObjectWithInlineArray ;
2525use JMS \Serializer \Tests \Fixtures \ObjectWithObjectProperty ;
2626use JMS \Serializer \Tests \Fixtures \Tag ;
27+ use JMS \Serializer \Tests \Fixtures \TypedProperties \BoolOrString ;
2728use JMS \Serializer \Tests \Fixtures \TypedProperties \ComplexDiscriminatedUnion ;
29+ use JMS \Serializer \Tests \Fixtures \TypedProperties \FalseOrString ;
2830use JMS \Serializer \Tests \Fixtures \TypedProperties \UnionTypedProperties ;
2931use JMS \Serializer \Visitor \Factory \JsonSerializationVisitorFactory ;
3032use JMS \Serializer \Visitor \SerializationVisitorInterface ;
@@ -151,9 +153,12 @@ protected static function getContent($key)
151153 $ outputs ['uninitialized_typed_props ' ] = '{"virtual_role":{},"id":1,"role":{},"tags":[]} ' ;
152154 $ outputs ['custom_datetimeinterface ' ] = '{"custom":"2021-09-07"} ' ;
153155 $ outputs ['data_integer ' ] = '{"data":10000} ' ;
156+ $ outputs ['data_integer_one ' ] = '{"data":1} ' ;
154157 $ outputs ['data_float ' ] = '{"data":1.236} ' ;
155158 $ outputs ['data_bool ' ] = '{"data":false} ' ;
156159 $ outputs ['data_string ' ] = '{"data":"foo"} ' ;
160+ $ outputs ['data_string_empty ' ] = '{"data":""} ' ;
161+ $ outputs ['data_string_zero ' ] = '{"data":"0"} ' ;
157162 $ outputs ['data_array ' ] = '{"data":[1,2,3]} ' ;
158163 $ outputs ['data_true ' ] = '{"data":true} ' ;
159164 $ outputs ['data_false ' ] = '{"data":false} ' ;
@@ -452,12 +457,16 @@ public static function getTypeHintedArraysAndStdClass()
452457
453458 public static function getSimpleUnionProperties (): iterable
454459 {
455- yield 'int ' => [[10000 , null , false ], 'union_typed_properties_integer ' ];
456- yield 'float ' => [[1.236 , null , false ], 'union_typed_properties_float ' ];
457- yield 'bool ' => [[false , null , false ], 'union_typed_properties_bool ' ];
458- yield 'string ' => [['foo ' , null , false ], 'union_typed_properties_string ' ];
459- yield 'array ' => [[[1 , 2 , 3 ], null , false ], 'union_typed_properties_array ' ];
460- yield 'false_array ' => [[false , null , 'foo ' ], 'union_typed_properties_false_string ' ];
460+ yield [10000 , 'data_integer ' ];
461+ yield [1.236 , 'data_float ' ];
462+ yield [false , 'data_bool ' ];
463+ yield ['foo ' , 'data_string ' ];
464+ yield [[1 , 2 , 3 ], 'data_array ' ];
465+ yield [1 , 'data_integer_one ' ];
466+ yield ['0 ' , 'data_string_zero ' ];
467+ yield ['' , 'data_string_empty ' ];
468+ yield [true , 'data_true ' ];
469+ yield [false , 'data_false ' ];
461470 }
462471
463472 /**
@@ -472,9 +481,57 @@ public function testUnionProperties($data, string $expected): void
472481 return ;
473482 }
474483
475- $ object = new UnionTypedProperties (...$ data );
476- self ::assertEquals ($ object , $ this ->deserialize (static ::getContent ($ expected ), UnionTypedProperties::class));
477- self ::assertEquals ($ this ->serialize ($ object ), static ::getContent ($ expected ));
484+ $ deserialized = $ this ->deserialize (static ::getContent ($ expected ), UnionTypedProperties::class);
485+
486+ self ::assertSame ($ data , $ deserialized ->data );
487+ self ::assertSame ($ this ->serialize ($ deserialized ), static ::getContent ($ expected ));
488+ }
489+
490+ public static function getUnionCastableTypes (): iterable
491+ {
492+ yield ['10000 ' , 'data_integer ' ];
493+ yield ['1.236 ' , 'data_float ' ];
494+ yield [true , 'data_integer_one ' ];
495+ }
496+
497+ /**
498+ * @dataProvider getUnionCastableTypes
499+ */
500+ #[DataProvider('getUnionCastableTypes ' )]
501+ public function testUnionPropertiesWithCastableType ($ data , string $ expected )
502+ {
503+ if (PHP_VERSION_ID < 80000 ) {
504+ $ this ->markTestSkipped (sprintf ('%s requires PHP 8.0 ' , TypedPropertiesDriver::class));
505+
506+ return ;
507+ }
508+
509+ $ deserialized = $ this ->deserialize (static ::getContent ($ expected ), BoolOrString::class);
510+
511+ self ::assertSame ($ data , $ deserialized ->data );
512+ }
513+
514+ public static function getUnionNotCastableTypes (): iterable
515+ {
516+ yield ['data_array ' ];
517+ }
518+
519+ /**
520+ * @dataProvider getUnionNotCastableTypes
521+ */
522+ #[DataProvider('getUnionNotCastableTypes ' )]
523+ public function testUnionPropertiesWithNotCastableType (string $ expected )
524+ {
525+ if (PHP_VERSION_ID < 80000 ) {
526+ $ this ->markTestSkipped (sprintf ('%s requires PHP 8.0 ' , TypedPropertiesDriver::class));
527+
528+ return ;
529+ }
530+
531+ $ deserialized = $ this ->deserialize (static ::getContent ($ expected ), BoolOrString::class);
532+
533+ $ this ->expectException (\Error::class);
534+ $ deserialized ->data ;
478535 }
479536
480537 public function testTrueDataType ()
@@ -489,7 +546,6 @@ public function testTrueDataType()
489546 static ::getContent ('data_true ' ),
490547 $ this ->serialize (new DataTrue (true )),
491548 );
492-
493549 self ::assertEquals (
494550 new DataTrue (true ),
495551 $ this ->deserialize (static ::getContent ('data_true ' ), DataTrue::class),
@@ -517,6 +573,16 @@ public function testFalseDataType()
517573 $ this ->deserialize (static ::getContent ('data_false ' ), DataFalse::class),
518574 );
519575
576+ self ::assertEquals (
577+ static ::getContent ('data_false ' ),
578+ $ this ->serialize (new FalseOrString (false )),
579+ );
580+
581+ self ::assertEquals (
582+ new FalseOrString (false ),
583+ $ this ->deserialize (static ::getContent ('data_false ' ), FalseOrString::class),
584+ );
585+
520586 $ this ->expectException (TypeError::class);
521587 $ this ->deserialize (static ::getContent ('data_true ' ), DataFalse::class);
522588 }
0 commit comments