|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Serializer\Tests\Normalizer; |
| 13 | + |
| 14 | +use BcMath\Number; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
| 17 | +use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
| 18 | +use Symfony\Component\Serializer\Normalizer\NumberNormalizer; |
| 19 | + |
| 20 | +/** |
| 21 | + * @requires PHP 8.4 |
| 22 | + * @requires extension bcmath |
| 23 | + */ |
| 24 | +class NumberNormalizerTest extends TestCase |
| 25 | +{ |
| 26 | + private NumberNormalizer $normalizer; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->normalizer = new NumberNormalizer(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @dataProvider supportsNormalizationProvider |
| 35 | + */ |
| 36 | + public function testSupportsNormalization(mixed $data, bool $expected) |
| 37 | + { |
| 38 | + $this->assertSame($expected, $this->normalizer->supportsNormalization($data)); |
| 39 | + } |
| 40 | + |
| 41 | + public static function supportsNormalizationProvider(): iterable |
| 42 | + { |
| 43 | + yield 'Number object' => [new Number('1.23'), true]; |
| 44 | + yield 'object with similar properties as Number' => [(object) ['value' => '1.23', 'scale' => 2], false]; |
| 45 | + yield 'stdClass' => [new \stdClass(), false]; |
| 46 | + yield 'string' => ['1.23', false]; |
| 47 | + yield 'float' => [1.23, false]; |
| 48 | + yield 'null' => [null, false]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dataProvider normalizeGoodValueProvider |
| 53 | + */ |
| 54 | + public function testNormalize(mixed $data, mixed $expected) |
| 55 | + { |
| 56 | + $this->assertSame($expected, $this->normalizer->normalize($data)); |
| 57 | + } |
| 58 | + |
| 59 | + public static function normalizeGoodValueProvider(): iterable |
| 60 | + { |
| 61 | + yield 'Number with scale=2' => [new Number('1.23'), '1.23']; |
| 62 | + yield 'Number with scale=0' => [new Number('1'), '1']; |
| 63 | + yield 'Number with integer' => [new Number(123), '123']; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @dataProvider normalizeBadValueProvider |
| 68 | + */ |
| 69 | + public function testNormalizeBadValueThrows(mixed $data) |
| 70 | + { |
| 71 | + $this->expectException(InvalidArgumentException::class); |
| 72 | + $this->expectExceptionMessage('The data must be an instance of "BcMath\Number".'); |
| 73 | + |
| 74 | + $this->normalizer->normalize($data); |
| 75 | + } |
| 76 | + |
| 77 | + public static function normalizeBadValueProvider(): iterable |
| 78 | + { |
| 79 | + yield 'stdClass' => [new \stdClass()]; |
| 80 | + yield 'string' => ['1.23']; |
| 81 | + yield 'null' => [null]; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @dataProvider supportsDenormalizationProvider |
| 86 | + */ |
| 87 | + public function testSupportsDenormalization(mixed $data, string $type, bool $expected) |
| 88 | + { |
| 89 | + $this->assertSame($expected, $this->normalizer->supportsDenormalization($data, $type)); |
| 90 | + } |
| 91 | + |
| 92 | + public static function supportsDenormalizationProvider(): iterable |
| 93 | + { |
| 94 | + yield 'null value, matching type' => [null, Number::class, false]; |
| 95 | + yield 'null value, unmatching type' => [null, \stdClass::class, false]; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @dataProvider denormalizeGoodValueProvider |
| 100 | + */ |
| 101 | + public function testDenormalize(mixed $data, string $type, mixed $expected) |
| 102 | + { |
| 103 | + $this->assertEquals($expected, $this->normalizer->denormalize($data, $type)); |
| 104 | + } |
| 105 | + |
| 106 | + public static function denormalizeGoodValueProvider(): iterable |
| 107 | + { |
| 108 | + yield 'string with decimal point' => ['1.23', Number::class, new Number('1.23')]; |
| 109 | + yield 'integer as string' => ['123', Number::class, new Number('123')]; |
| 110 | + yield 'integer' => [123, Number::class, new Number('123')]; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @dataProvider denormalizeBadValueProvider |
| 115 | + */ |
| 116 | + public function testDenormalizeBadValueThrows(mixed $data, string $type, string $expectedException, string $expectedExceptionMessage) |
| 117 | + { |
| 118 | + $this->expectException($expectedException); |
| 119 | + $this->expectExceptionMessage($expectedExceptionMessage); |
| 120 | + |
| 121 | + $this->normalizer->denormalize($data, $type); |
| 122 | + } |
| 123 | + |
| 124 | + public static function denormalizeBadValueProvider(): iterable |
| 125 | + { |
| 126 | + yield 'null' => [null, Number::class, NotNormalizableValueException::class, 'The data is neither a "string" nor an "int", you should pass a "string" that represents a decimal number, or an "int".']; |
| 127 | + yield 'boolean' => [true, Number::class, NotNormalizableValueException::class, 'The data is neither a "string" nor an "int", you should pass a "string" that represents a decimal number, or an "int".']; |
| 128 | + yield 'object' => [new \stdClass(), Number::class, NotNormalizableValueException::class, 'The data is neither a "string" nor an "int", you should pass a "string" that represents a decimal number, or an "int".']; |
| 129 | + yield 'non-numeric string' => ['foobar', Number::class, NotNormalizableValueException::class, 'The data must represent a decimal number']; |
| 130 | + yield 'unsupported type' => ['1.23', \stdClass::class, InvalidArgumentException::class, 'Only "BcMath\Number" type is supported.']; |
| 131 | + yield 'float' => [1.23, Number::class, NotNormalizableValueException::class, 'The data is neither a "string" nor an "int", you should pass a "string" that represents a decimal number, or an "int".']; |
| 132 | + } |
| 133 | +} |
0 commit comments