|
| 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 | + * @requires extension gmp |
| 24 | + */ |
| 25 | +class NumberNormalizerTest extends TestCase |
| 26 | +{ |
| 27 | + private NumberNormalizer $normalizer; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + $this->normalizer = new NumberNormalizer(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @dataProvider supportsNormalizationProvider |
| 36 | + */ |
| 37 | + public function testSupportsNormalization(mixed $data, bool $expected) |
| 38 | + { |
| 39 | + $this->assertSame($expected, $this->normalizer->supportsNormalization($data)); |
| 40 | + } |
| 41 | + |
| 42 | + public static function supportsNormalizationProvider(): iterable |
| 43 | + { |
| 44 | + yield 'GMP object' => [new \GMP('0b111'), true]; |
| 45 | + yield 'Number object' => [new Number('1.23'), true]; |
| 46 | + yield 'object with similar properties as Number' => [(object) ['value' => '1.23', 'scale' => 2], false]; |
| 47 | + yield 'stdClass' => [new \stdClass(), false]; |
| 48 | + yield 'string' => ['1.23', false]; |
| 49 | + yield 'float' => [1.23, false]; |
| 50 | + yield 'null' => [null, false]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @dataProvider normalizeGoodValueProvider |
| 55 | + */ |
| 56 | + public function testNormalize(mixed $data, mixed $expected) |
| 57 | + { |
| 58 | + $this->assertSame($expected, $this->normalizer->normalize($data)); |
| 59 | + } |
| 60 | + |
| 61 | + public static function normalizeGoodValueProvider(): iterable |
| 62 | + { |
| 63 | + yield 'Number with scale=2' => [new Number('1.23'), '1.23']; |
| 64 | + yield 'Number with scale=0' => [new Number('1'), '1']; |
| 65 | + yield 'Number with integer' => [new Number(123), '123']; |
| 66 | + yield 'GMP hex' => [new \GMP('0x10'), '16']; |
| 67 | + yield 'GMP base=10' => [new \GMP('10'), '10']; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @dataProvider normalizeBadValueProvider |
| 72 | + */ |
| 73 | + public function testNormalizeBadValueThrows(mixed $data) |
| 74 | + { |
| 75 | + $this->expectException(InvalidArgumentException::class); |
| 76 | + $this->expectExceptionMessage('The data must be an instance of "BcMath\Number" or "GMP".'); |
| 77 | + |
| 78 | + $this->normalizer->normalize($data); |
| 79 | + } |
| 80 | + |
| 81 | + public static function normalizeBadValueProvider(): iterable |
| 82 | + { |
| 83 | + yield 'stdClass' => [new \stdClass()]; |
| 84 | + yield 'string' => ['1.23']; |
| 85 | + yield 'null' => [null]; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @dataProvider supportsDenormalizationProvider |
| 90 | + */ |
| 91 | + public function testSupportsDenormalization(mixed $data, string $type, bool $expected) |
| 92 | + { |
| 93 | + $this->assertSame($expected, $this->normalizer->supportsDenormalization($data, $type)); |
| 94 | + } |
| 95 | + |
| 96 | + public static function supportsDenormalizationProvider(): iterable |
| 97 | + { |
| 98 | + yield 'null value, Number' => [null, Number::class, false]; |
| 99 | + yield 'null value, GMP' => [null, \GMP::class, false]; |
| 100 | + yield 'null value, unmatching type' => [null, \stdClass::class, false]; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @dataProvider denormalizeGoodValueProvider |
| 105 | + */ |
| 106 | + public function testDenormalize(mixed $data, string $type, mixed $expected) |
| 107 | + { |
| 108 | + $this->assertEquals($expected, $this->normalizer->denormalize($data, $type)); |
| 109 | + } |
| 110 | + |
| 111 | + public static function denormalizeGoodValueProvider(): iterable |
| 112 | + { |
| 113 | + yield 'Number, string with decimal point' => ['1.23', Number::class, new Number('1.23')]; |
| 114 | + yield 'Number, integer as string' => ['123', Number::class, new Number('123')]; |
| 115 | + yield 'Number, integer' => [123, Number::class, new Number('123')]; |
| 116 | + yield 'GMP, large number' => ['9223372036854775808', \GMP::class, new \GMP('9223372036854775808')]; |
| 117 | + yield 'GMP, integer' => [123, \GMP::class, new \GMP('123')]; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @dataProvider denormalizeBadValueProvider |
| 122 | + */ |
| 123 | + public function testDenormalizeBadValueThrows(mixed $data, string $type, string $expectedException, string $expectedExceptionMessage) |
| 124 | + { |
| 125 | + $this->expectException($expectedException); |
| 126 | + $this->expectExceptionMessage($expectedExceptionMessage); |
| 127 | + |
| 128 | + $this->normalizer->denormalize($data, $type); |
| 129 | + } |
| 130 | + |
| 131 | + public static function denormalizeBadValueProvider(): iterable |
| 132 | + { |
| 133 | + $stringOrDecimalExpectedMessage = 'The data must be a "string" representing a decimal number, or an "int".'; |
| 134 | + yield 'Number, null' => [null, Number::class, NotNormalizableValueException::class, $stringOrDecimalExpectedMessage]; |
| 135 | + yield 'Number, boolean' => [true, Number::class, NotNormalizableValueException::class, $stringOrDecimalExpectedMessage]; |
| 136 | + yield 'Number, object' => [new \stdClass(), Number::class, NotNormalizableValueException::class, $stringOrDecimalExpectedMessage]; |
| 137 | + yield 'Number, non-numeric string' => ['foobar', Number::class, NotNormalizableValueException::class, $stringOrDecimalExpectedMessage]; |
| 138 | + yield 'Number, float' => [1.23, Number::class, NotNormalizableValueException::class, $stringOrDecimalExpectedMessage]; |
| 139 | + |
| 140 | + $stringOrIntExpectedMessage = 'The data must be a "string" representing an integer, or an "int".'; |
| 141 | + yield 'GMP, null' => [null, \GMP::class, NotNormalizableValueException::class, $stringOrIntExpectedMessage]; |
| 142 | + yield 'GMP, boolean' => [true, \GMP::class, NotNormalizableValueException::class, $stringOrIntExpectedMessage]; |
| 143 | + yield 'GMP, object' => [new \stdClass(), \GMP::class, NotNormalizableValueException::class, $stringOrIntExpectedMessage]; |
| 144 | + yield 'GMP, non-numeric string' => ['foobar', \GMP::class, NotNormalizableValueException::class, $stringOrIntExpectedMessage]; |
| 145 | + yield 'GMP, scale > 0' => ['1.23', \GMP::class, NotNormalizableValueException::class, $stringOrIntExpectedMessage]; |
| 146 | + yield 'GMP, float' => [1.23, \GMP::class, NotNormalizableValueException::class, $stringOrIntExpectedMessage]; |
| 147 | + |
| 148 | + yield 'unsupported type' => ['1.23', \stdClass::class, InvalidArgumentException::class, 'Only "BcMath\Number" and "GMP" types are supported.']; |
| 149 | + } |
| 150 | +} |
0 commit comments