|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Component\Serializer\Tests\Normalizer; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
| 7 | +use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
| 8 | +use Symfony\Component\Serializer\Normalizer\BuiltinTypeDenormalizer; |
| 9 | + |
| 10 | +class BuiltinTypeDenormalizerTest extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var BuiltinTypeDenormalizer |
| 14 | + */ |
| 15 | + private $denormalizer; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + $this->denormalizer = new BuiltinTypeDenormalizer(); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @dataProvider provideSupportedTypes |
| 24 | + */ |
| 25 | + public function testSupportsDenormalization(string $supportedType): void |
| 26 | + { |
| 27 | + $this->assertTrue($this->denormalizer->supportsDenormalization(null, $supportedType)); |
| 28 | + } |
| 29 | + |
| 30 | + public function provideSupportedTypes(): iterable |
| 31 | + { |
| 32 | + return [['int'], ['float'], ['string'], ['bool'], ['resource'], ['callable']]; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @dataProvider provideUnsupportedTypes |
| 37 | + */ |
| 38 | + public function testUnsupportsDenormalization(string $unsupportedType): void |
| 39 | + { |
| 40 | + $this->assertFalse($this->denormalizer->supportsDenormalization(null, $unsupportedType)); |
| 41 | + } |
| 42 | + |
| 43 | + public function provideUnsupportedTypes(): iterable |
| 44 | + { |
| 45 | + return [['null'], ['array'], ['iterable'], ['object'], ['int[]']]; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @dataProvider provideInvalidData |
| 50 | + */ |
| 51 | + public function testDenormalizeInvalidDataThrowsException($invalidData): void |
| 52 | + { |
| 53 | + $this->expectException(InvalidArgumentException::class); |
| 54 | + $this->denormalizer->denormalize($invalidData, 'int'); |
| 55 | + } |
| 56 | + |
| 57 | + public function provideInvalidData(): iterable |
| 58 | + { |
| 59 | + return [ |
| 60 | + 'array' => [[1, 2]], |
| 61 | + 'object' => [new \stdClass()], |
| 62 | + 'null' => [null], |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @dataProvider provideNotNormalizableData |
| 68 | + */ |
| 69 | + public function testDenormalizeNotNormalizableDataThrowsException($data, string $type, string $format): void |
| 70 | + { |
| 71 | + $this->expectException(NotNormalizableValueException::class); |
| 72 | + $this->denormalizer->denormalize($data, $type, $format); |
| 73 | + } |
| 74 | + |
| 75 | + public function provideNotNormalizableData(): iterable |
| 76 | + { |
| 77 | + return [ |
| 78 | + 'not a string' => [true, 'string', 'json'], |
| 79 | + 'not an integer' => [3.1, 'int', 'json'], |
| 80 | + 'not an integer (xml/csv)' => ['+12', 'int', 'xml'], |
| 81 | + 'not a float' => [false, 'float', 'json'], |
| 82 | + 'not a float (xml/csv)' => ['nan', 'float', 'xml'], |
| 83 | + 'not a boolean (json)' => [0, 'bool', 'json'], |
| 84 | + 'not a boolean (xml/csv)' => ['test', 'bool', 'xml'], |
| 85 | + ]; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @dataProvider provideNormalizableData |
| 90 | + */ |
| 91 | + public function testDenormalize($expectedResult, $data, string $type, string $format = null): void |
| 92 | + { |
| 93 | + $result = $this->denormalizer->denormalize($data, $type, $format); |
| 94 | + |
| 95 | + if (\is_float($expectedResult) && is_nan($expectedResult)) { |
| 96 | + $this->assertNan($result); |
| 97 | + } else { |
| 98 | + $this->assertSame($expectedResult, $result); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public function provideNormalizableData(): iterable |
| 103 | + { |
| 104 | + return [ |
| 105 | + 'string' => ['1', '1', 'string', 'json'], |
| 106 | + 'integer' => [-3, -3, 'int', 'json'], |
| 107 | + 'integer (xml/csv)' => [-12, '-12', 'int', 'xml'], |
| 108 | + 'float' => [3.14, 3.14, 'float', 'json'], |
| 109 | + 'float without decimals' => [3.0, 3, 'float', 'json'], |
| 110 | + 'NaN (xml/csv)' => [\NAN, 'NaN', 'float', 'xml'], |
| 111 | + 'INF (xml/csv)' => [\INF, 'INF', 'float', 'xml'], |
| 112 | + '-INF (xml/csv)' => [-\INF, '-INF', 'float', 'xml'], |
| 113 | + 'boolean: true (json)' => [true, true, 'bool', 'json'], |
| 114 | + 'boolean: false (json)' => [false, false, 'bool', 'json'], |
| 115 | + "boolean: 'true' (xml/csv)" => [true, 'true', 'bool', 'xml'], |
| 116 | + "boolean: '1' (xml/csv)" => [true, '1', 'bool', 'xml'], |
| 117 | + "boolean: 'false' (xml/csv)" => [false, 'false', 'bool', 'xml'], |
| 118 | + "boolean: '0' (xml/csv)" => [false, '0', 'bool', 'xml'], |
| 119 | + 'callable' => [[$this, 'provideInvalidData'], [$this, 'provideInvalidData'], 'callable', null], |
| 120 | + 'resource' => [$r = fopen(__FILE__, 'r'), $r, 'resource', null], |
| 121 | + ]; |
| 122 | + } |
| 123 | +} |
0 commit comments