diff --git a/tests/Enum/EnumSerializerHandlerTest.php b/tests/Enum/EnumSerializerHandlerTest.php index 8339bc0..3e4d58a 100644 --- a/tests/Enum/EnumSerializerHandlerTest.php +++ b/tests/Enum/EnumSerializerHandlerTest.php @@ -449,94 +449,68 @@ public function testSerializeEnumInvalidValue( } } - public function testDeserializeEnumInvalidValue(): void - { - $serializer = $this->getSerializer(); - - try { - $serializer->deserialize('{ - "single_enum": "foo" - }', User::class, 'json'); - Assert::fail('Exception expected'); - } catch (\Consistence\JmsSerializer\Enum\DeserializationInvalidValueException $e) { - Assert::assertSame('single_enum', $e->getFieldPath()); - $previous = $e->getPrevious(); - Assert::assertInstanceOf(\Consistence\Enum\InvalidEnumValueException::class, $previous); - Assert::assertSame('foo', $previous->getValue()); - } - } - - public function testDeserializeEnumInvalidValueEmbeddedObject(): void + /** + * @return mixed[][]|\Generator + */ + public function deserializeEnumInvalidValueDataProvider(): Generator { - $serializer = $this->getSerializer(); - - try { - $serializer->deserialize('{"embedded_object": { + yield 'string instead of single enum' => [ + 'serializedProperty' => '"single_enum": "foo"', + 'field' => 'single_enum', + 'value' => 'foo', + ]; + yield 'string instead of single enum in embedded object' => [ + 'serializedProperty' => '"embedded_object": { "single_enum": "foo" - }}', User::class, 'json'); - Assert::fail('Exception expected'); - } catch (\Consistence\JmsSerializer\Enum\DeserializationInvalidValueException $e) { - Assert::assertSame('embedded_object.single_enum', $e->getFieldPath()); - $previous = $e->getPrevious(); - Assert::assertInstanceOf(\Consistence\Enum\InvalidEnumValueException::class, $previous); - Assert::assertSame('foo', $previous->getValue()); - } - } - - public function testDeserializeEnumWhenValueIsArray(): void - { - $serializer = $this->getSerializer(); - - try { - $serializer->deserialize('{ - "single_enum": [1, 2, 3] - }', User::class, 'json'); - - Assert::fail('Exception expected'); - - } catch (\Consistence\JmsSerializer\Enum\DeserializationInvalidValueException $e) { - Assert::assertSame('single_enum', $e->getFieldPath()); - $previous = $e->getPrevious(); - Assert::assertInstanceOf(\Consistence\Enum\InvalidEnumValueException::class, $previous); - Assert::assertSame([1, 2, 3], $previous->getValue()); - } - } - - public function testDeserializeEnumWhenValueIsObject(): void - { - $serializer = $this->getSerializer(); - - try { - $serializer->deserialize('{ - "single_enum": {"foo": "bar"} - }', User::class, 'json'); - - Assert::fail('Exception expected'); - - } catch (\Consistence\JmsSerializer\Enum\DeserializationInvalidValueException $e) { - Assert::assertSame('single_enum', $e->getFieldPath()); - $previous = $e->getPrevious(); - Assert::assertInstanceOf(\Consistence\Enum\InvalidEnumValueException::class, $previous); - Assert::assertSame(['foo' => 'bar'], $previous->getValue()); - } + }', + 'field' => 'embedded_object.single_enum', + 'value' => 'foo', + ]; + yield 'array instead of single enum' => [ + 'serializedProperty' => '"single_enum": [1, 2, 3]', + 'field' => 'single_enum', + 'value' => [1, 2, 3], + ]; + yield 'object instead of single enum' => [ + 'serializedProperty' => '"single_enum": {"foo": "bar"}', + 'field' => 'single_enum', + 'value' => ['foo' => 'bar'], + ]; + yield 'string instead of multi enum' => [ + 'serializedProperty' => '"multi_enum": "foo"', + 'field' => 'multi_enum', + 'value' => 'foo', + ]; + yield 'integer instead of string with explicitly mapped type' => [ + 'serializedProperty' => '"type_enum_with_type": 1', + 'field' => 'type_enum_with_type', + 'value' => '1', + ]; } - public function testDeserializeMultiEnumWithInvalidValueType(): void + /** + * @dataProvider deserializeEnumInvalidValueDataProvider + * + * @param string $serializedProperty + * @param string $field + * @param mixed $value + */ + public function testDeserializeEnumInvalidValue( + string $serializedProperty, + string $field, + $value + ): void { $serializer = $this->getSerializer(); try { - $serializer->deserialize('{ - "multi_enum": "foo" - }', User::class, 'json'); - + $serializer->deserialize(sprintf('{%s}', $serializedProperty), User::class, 'json'); Assert::fail('Exception expected'); - } catch (\Consistence\JmsSerializer\Enum\DeserializationInvalidValueException $e) { - Assert::assertSame('multi_enum', $e->getFieldPath()); + Assert::assertSame($field, $e->getFieldPath()); $previous = $e->getPrevious(); Assert::assertInstanceOf(\Consistence\Enum\InvalidEnumValueException::class, $previous); - Assert::assertSame('foo', $previous->getValue()); + Assert::assertSame($value, $previous->getValue()); } } @@ -638,23 +612,6 @@ public function testDeserializeEnumFromXmlWithoutDeserializationType(): void } } - public function testDeserializeEnumWithWrongDeserializationType(): void - { - $serializer = $this->getSerializer(); - - try { - $serializer->deserialize('{ - "type_enum_with_type": 1 - }', User::class, 'json'); - Assert::fail('Exception expected'); - } catch (\Consistence\JmsSerializer\Enum\DeserializationInvalidValueException $e) { - Assert::assertSame('type_enum_with_type', $e->getFieldPath()); - $previous = $e->getPrevious(); - Assert::assertInstanceOf(\Consistence\Enum\InvalidEnumValueException::class, $previous); - Assert::assertSame('1', $previous->getValue()); - } - } - private function getSerializer(): SerializerInterface { $xmlSerializationVisitorFactory = new XmlSerializationVisitorFactory();