@@ -449,94 +449,68 @@ public function testSerializeEnumInvalidValue(
449449 }
450450 }
451451
452- public function testDeserializeEnumInvalidValue (): void
453- {
454- $ serializer = $ this ->getSerializer ();
455-
456- try {
457- $ serializer ->deserialize ('{
458- "single_enum": "foo"
459- } ' , User::class, 'json ' );
460- Assert::fail ('Exception expected ' );
461- } catch (\Consistence \JmsSerializer \Enum \DeserializationInvalidValueException $ e ) {
462- Assert::assertSame ('single_enum ' , $ e ->getFieldPath ());
463- $ previous = $ e ->getPrevious ();
464- Assert::assertInstanceOf (\Consistence \Enum \InvalidEnumValueException::class, $ previous );
465- Assert::assertSame ('foo ' , $ previous ->getValue ());
466- }
467- }
468-
469- public function testDeserializeEnumInvalidValueEmbeddedObject (): void
452+ /**
453+ * @return mixed[][]|\Generator
454+ */
455+ public function deserializeEnumInvalidValueDataProvider (): Generator
470456 {
471- $ serializer = $ this ->getSerializer ();
472-
473- try {
474- $ serializer ->deserialize ('{"embedded_object": {
457+ yield 'string instead of single enum ' => [
458+ 'serializedProperty ' => '"single_enum": "foo" ' ,
459+ 'field ' => 'single_enum ' ,
460+ 'value ' => 'foo ' ,
461+ ];
462+ yield 'string instead of single enum in embedded object ' => [
463+ 'serializedProperty ' => '"embedded_object": {
475464 "single_enum": "foo"
476- }} ' , User::class, 'json ' );
477- Assert::fail ('Exception expected ' );
478- } catch (\Consistence \JmsSerializer \Enum \DeserializationInvalidValueException $ e ) {
479- Assert::assertSame ('embedded_object.single_enum ' , $ e ->getFieldPath ());
480- $ previous = $ e ->getPrevious ();
481- Assert::assertInstanceOf (\Consistence \Enum \InvalidEnumValueException::class, $ previous );
482- Assert::assertSame ('foo ' , $ previous ->getValue ());
483- }
484- }
485-
486- public function testDeserializeEnumWhenValueIsArray (): void
487- {
488- $ serializer = $ this ->getSerializer ();
489-
490- try {
491- $ serializer ->deserialize ('{
492- "single_enum": [1, 2, 3]
493- } ' , User::class, 'json ' );
494-
495- Assert::fail ('Exception expected ' );
496-
497- } catch (\Consistence \JmsSerializer \Enum \DeserializationInvalidValueException $ e ) {
498- Assert::assertSame ('single_enum ' , $ e ->getFieldPath ());
499- $ previous = $ e ->getPrevious ();
500- Assert::assertInstanceOf (\Consistence \Enum \InvalidEnumValueException::class, $ previous );
501- Assert::assertSame ([1 , 2 , 3 ], $ previous ->getValue ());
502- }
503- }
504-
505- public function testDeserializeEnumWhenValueIsObject (): void
506- {
507- $ serializer = $ this ->getSerializer ();
508-
509- try {
510- $ serializer ->deserialize ('{
511- "single_enum": {"foo": "bar"}
512- } ' , User::class, 'json ' );
513-
514- Assert::fail ('Exception expected ' );
515-
516- } catch (\Consistence \JmsSerializer \Enum \DeserializationInvalidValueException $ e ) {
517- Assert::assertSame ('single_enum ' , $ e ->getFieldPath ());
518- $ previous = $ e ->getPrevious ();
519- Assert::assertInstanceOf (\Consistence \Enum \InvalidEnumValueException::class, $ previous );
520- Assert::assertSame (['foo ' => 'bar ' ], $ previous ->getValue ());
521- }
465+ } ' ,
466+ 'field ' => 'embedded_object.single_enum ' ,
467+ 'value ' => 'foo ' ,
468+ ];
469+ yield 'array instead of single enum ' => [
470+ 'serializedProperty ' => '"single_enum": [1, 2, 3] ' ,
471+ 'field ' => 'single_enum ' ,
472+ 'value ' => [1 , 2 , 3 ],
473+ ];
474+ yield 'object instead of single enum ' => [
475+ 'serializedProperty ' => '"single_enum": {"foo": "bar"} ' ,
476+ 'field ' => 'single_enum ' ,
477+ 'value ' => ['foo ' => 'bar ' ],
478+ ];
479+ yield 'string instead of multi enum ' => [
480+ 'serializedProperty ' => '"multi_enum": "foo" ' ,
481+ 'field ' => 'multi_enum ' ,
482+ 'value ' => 'foo ' ,
483+ ];
484+ yield 'integer instead of string with explicitly mapped type ' => [
485+ 'serializedProperty ' => '"type_enum_with_type": 1 ' ,
486+ 'field ' => 'type_enum_with_type ' ,
487+ 'value ' => '1 ' ,
488+ ];
522489 }
523490
524- public function testDeserializeMultiEnumWithInvalidValueType (): void
491+ /**
492+ * @dataProvider deserializeEnumInvalidValueDataProvider
493+ *
494+ * @param string $serializedProperty
495+ * @param string $field
496+ * @param mixed $value
497+ */
498+ public function testDeserializeEnumInvalidValue (
499+ string $ serializedProperty ,
500+ string $ field ,
501+ $ value
502+ ): void
525503 {
526504 $ serializer = $ this ->getSerializer ();
527505
528506 try {
529- $ serializer ->deserialize ('{
530- "multi_enum": "foo"
531- } ' , User::class, 'json ' );
532-
507+ $ serializer ->deserialize (sprintf ('{%s} ' , $ serializedProperty ), User::class, 'json ' );
533508 Assert::fail ('Exception expected ' );
534-
535509 } catch (\Consistence \JmsSerializer \Enum \DeserializationInvalidValueException $ e ) {
536- Assert::assertSame (' multi_enum ' , $ e ->getFieldPath ());
510+ Assert::assertSame ($ field , $ e ->getFieldPath ());
537511 $ previous = $ e ->getPrevious ();
538512 Assert::assertInstanceOf (\Consistence \Enum \InvalidEnumValueException::class, $ previous );
539- Assert::assertSame (' foo ' , $ previous ->getValue ());
513+ Assert::assertSame ($ value , $ previous ->getValue ());
540514 }
541515 }
542516
@@ -638,23 +612,6 @@ public function testDeserializeEnumFromXmlWithoutDeserializationType(): void
638612 }
639613 }
640614
641- public function testDeserializeEnumWithWrongDeserializationType (): void
642- {
643- $ serializer = $ this ->getSerializer ();
644-
645- try {
646- $ serializer ->deserialize ('{
647- "type_enum_with_type": 1
648- } ' , User::class, 'json ' );
649- Assert::fail ('Exception expected ' );
650- } catch (\Consistence \JmsSerializer \Enum \DeserializationInvalidValueException $ e ) {
651- Assert::assertSame ('type_enum_with_type ' , $ e ->getFieldPath ());
652- $ previous = $ e ->getPrevious ();
653- Assert::assertInstanceOf (\Consistence \Enum \InvalidEnumValueException::class, $ previous );
654- Assert::assertSame ('1 ' , $ previous ->getValue ());
655- }
656- }
657-
658615 private function getSerializer (): SerializerInterface
659616 {
660617 $ xmlSerializationVisitorFactory = new XmlSerializationVisitorFactory ();
0 commit comments