Skip to content

Commit

Permalink
deduplicate testing deserialization of invalid enum value using data …
Browse files Browse the repository at this point in the history
…provider
  • Loading branch information
VasekPurchart authored and petrduda committed Feb 22, 2023
1 parent 71a21b4 commit 63b45fd
Showing 1 changed file with 50 additions and 93 deletions.
143 changes: 50 additions & 93 deletions tests/Enum/EnumSerializerHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 63b45fd

Please sign in to comment.