1717use Symfony \Component \Serializer \Exception \NotNormalizableValueException ;
1818use Symfony \Component \Serializer \Normalizer \NumberNormalizer ;
1919
20- /**
21- * @requires PHP 8.4
22- * @requires extension bcmath
23- * @requires extension gmp
24- */
2520class NumberNormalizerTest extends TestCase
2621{
2722 private NumberNormalizer $ normalizer ;
@@ -41,8 +36,14 @@ public function testSupportsNormalization(mixed $data, bool $expected)
4136
4237 public static function supportsNormalizationProvider (): iterable
4338 {
44- yield 'GMP object ' => [new \GMP ('0b111 ' ), true ];
45- yield 'Number object ' => [new Number ('1.23 ' ), true ];
39+ if (class_exists (\GMP ::class)) {
40+ yield 'GMP object ' => [new \GMP ('0b111 ' ), true ];
41+ }
42+
43+ if (class_exists (Number::class)) {
44+ yield 'Number object ' => [new Number ('1.23 ' ), true ];
45+ }
46+
4647 yield 'object with similar properties as Number ' => [(object ) ['value ' => '1.23 ' , 'scale ' => 2 ], false ];
4748 yield 'stdClass ' => [new \stdClass (), false ];
4849 yield 'string ' => ['1.23 ' , false ];
@@ -51,20 +52,40 @@ public static function supportsNormalizationProvider(): iterable
5152 }
5253
5354 /**
54- * @dataProvider normalizeGoodValueProvider
55+ * @requires extension bcmath
56+ *
57+ * @dataProvider normalizeGoodBcMathNumberValueProvider
5558 */
56- public function testNormalize ( mixed $ data , mixed $ expected )
59+ public function testNormalizeBcMathNumber ( Number $ data , string $ expected )
5760 {
5861 $ this ->assertSame ($ expected , $ this ->normalizer ->normalize ($ data ));
5962 }
6063
61- public static function normalizeGoodValueProvider (): iterable
64+ public static function normalizeGoodBcMathNumberValueProvider (): iterable
6265 {
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 ' ];
66+ if (class_exists (Number::class)) {
67+ yield 'Number with scale=2 ' => [new Number ('1.23 ' ), '1.23 ' ];
68+ yield 'Number with scale=0 ' => [new Number ('1 ' ), '1 ' ];
69+ yield 'Number with integer ' => [new Number (123 ), '123 ' ];
70+ }
71+ }
72+
73+ /**
74+ * @requires extension gmp
75+ *
76+ * @dataProvider normalizeGoodGmpValueProvider
77+ */
78+ public function testNormalizeGmp (\GMP $ data , string $ expected )
79+ {
80+ $ this ->assertSame ($ expected , $ this ->normalizer ->normalize ($ data ));
81+ }
82+
83+ public static function normalizeGoodGmpValueProvider (): iterable
84+ {
85+ if (class_exists (\GMP ::class)) {
86+ yield 'GMP hex ' => [new \GMP ('0x10 ' ), '16 ' ];
87+ yield 'GMP base=10 ' => [new \GMP ('10 ' ), '10 ' ];
88+ }
6889 }
6990
7091 /**
@@ -86,65 +107,116 @@ public static function normalizeBadValueProvider(): iterable
86107 }
87108
88109 /**
89- * @dataProvider supportsDenormalizationProvider
110+ * @requires PHP 8.4
111+ * @requires extension bcmath
112+ */
113+ public function testSupportsBcMathNumberDenormalization ()
114+ {
115+ $ this ->assertFalse ($ this ->normalizer ->supportsDenormalization (null , Number::class));
116+ }
117+
118+ /**
119+ * @requires extension gmp
90120 */
91- public function testSupportsDenormalization ( mixed $ data , string $ type , bool $ expected )
121+ public function testSupportsGmpDenormalization ( )
92122 {
93- $ this ->assertSame ( $ expected , $ this ->normalizer ->supportsDenormalization ($ data , $ type ));
123+ $ this ->assertFalse ( $ this ->normalizer ->supportsDenormalization (null , \ GMP ::class ));
94124 }
95125
96- public static function supportsDenormalizationProvider (): iterable
126+ public function testDoesNotSupportOtherValuesDenormalization ()
97127 {
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 ];
128+ $ this ->assertFalse ($ this ->normalizer ->supportsDenormalization (null , \stdClass::class));
101129 }
102130
103131 /**
104- * @dataProvider denormalizeGoodValueProvider
132+ * @requires PHP 8.4
133+ * @requires extension bcmath
134+ *
135+ * @dataProvider denormalizeGoodBcMathNumberValueProvider
105136 */
106- public function testDenormalize ( mixed $ data , string $ type , mixed $ expected )
137+ public function testDenormalizeBcMathNumber ( string | int $ data , string $ type , Number $ expected )
107138 {
108139 $ this ->assertEquals ($ expected , $ this ->normalizer ->denormalize ($ data , $ type ));
109140 }
110141
111- public static function denormalizeGoodValueProvider (): iterable
142+ public static function denormalizeGoodBcMathNumberValueProvider (): iterable
112143 {
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 ' )];
144+ if ( class_exists ( Number::class)) {
145+ yield 'Number, string with decimal point ' => ['1.23 ' , Number::class, new Number ('1.23 ' )];
146+ yield 'Number, integer as string ' => [' 123 ' , Number::class, new Number ('123 ' )];
147+ yield 'Number, integer ' => [123 , Number ::class, new Number ( ' 123 ' )];
148+ }
118149 }
119150
120151 /**
121- * @dataProvider denormalizeBadValueProvider
152+ * @dataProvider denormalizeGoodGmpValueProvider
122153 */
123- public function testDenormalizeBadValueThrows (mixed $ data , string $ type , string $ expectedException , string $ expectedExceptionMessage )
154+ public function testDenormalizeGmp (string |int $ data , string $ type , \GMP $ expected )
155+ {
156+ $ this ->assertEquals ($ expected , $ this ->normalizer ->denormalize ($ data , $ type ));
157+ }
158+
159+ public static function denormalizeGoodGmpValueProvider (): iterable
160+ {
161+ if (class_exists (\GMP ::class)) {
162+ yield 'GMP, large number ' => ['9223372036854775808 ' , \GMP ::class, new \GMP ('9223372036854775808 ' )];
163+ yield 'GMP, integer ' => [123 , \GMP ::class, new \GMP ('123 ' )];
164+ }
165+ }
166+
167+ /**
168+ * @requires PHP 8.4
169+ * @requires extension bcmath
170+ *
171+ * @dataProvider denormalizeBadBcMathNumberValueProvider
172+ */
173+ public function testDenormalizeBadBcMathNumberValueThrows (mixed $ data , string $ type , string $ expectedException , string $ expectedExceptionMessage )
124174 {
125175 $ this ->expectException ($ expectedException );
126176 $ this ->expectExceptionMessage ($ expectedExceptionMessage );
127177
128178 $ this ->normalizer ->denormalize ($ data , $ type );
129179 }
130180
131- public static function denormalizeBadValueProvider (): iterable
181+ public static function denormalizeBadBcMathNumberValueProvider (): iterable
132182 {
133183 $ stringOrDecimalExpectedMessage = 'The data must be a "string" representing a decimal number, or an "int". ' ;
134184 yield 'Number, null ' => [null , Number::class, NotNormalizableValueException::class, $ stringOrDecimalExpectedMessage ];
135185 yield 'Number, boolean ' => [true , Number::class, NotNormalizableValueException::class, $ stringOrDecimalExpectedMessage ];
136186 yield 'Number, object ' => [new \stdClass (), Number::class, NotNormalizableValueException::class, $ stringOrDecimalExpectedMessage ];
137187 yield 'Number, non-numeric string ' => ['foobar ' , Number::class, NotNormalizableValueException::class, $ stringOrDecimalExpectedMessage ];
138188 yield 'Number, float ' => [1.23 , Number::class, NotNormalizableValueException::class, $ stringOrDecimalExpectedMessage ];
189+ }
190+
191+ /**
192+ * @requires extension gmp
193+ *
194+ * @dataProvider denormalizeBadGmpValueProvider
195+ */
196+ public function testDenormalizeBadGmpValueThrows (mixed $ data , string $ type , string $ expectedException , string $ expectedExceptionMessage )
197+ {
198+ $ this ->expectException ($ expectedException );
199+ $ this ->expectExceptionMessage ($ expectedExceptionMessage );
200+
201+ $ this ->normalizer ->denormalize ($ data , $ type );
202+ }
139203
204+ public static function denormalizeBadGmpValueProvider (): iterable
205+ {
140206 $ stringOrIntExpectedMessage = 'The data must be a "string" representing an integer, or an "int". ' ;
141207 yield 'GMP, null ' => [null , \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
142208 yield 'GMP, boolean ' => [true , \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
143209 yield 'GMP, object ' => [new \stdClass (), \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
144210 yield 'GMP, non-numeric string ' => ['foobar ' , \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
145211 yield 'GMP, scale > 0 ' => ['1.23 ' , \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
146212 yield 'GMP, float ' => [1.23 , \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
213+ }
214+
215+ public function testDenormalizeBadValueThrows ()
216+ {
217+ $ this ->expectException (InvalidArgumentException::class);
218+ $ this ->expectExceptionMessage ('Only "BcMath\Number" and "GMP" types are supported. ' );
147219
148- yield ' unsupported type ' => [ '1.23 ' , \stdClass::class, InvalidArgumentException::class, ' Only "BcMath\Number" and "GMP" types are supported. ' ] ;
220+ $ this -> normalizer -> denormalize ( '1.23 ' , \stdClass::class) ;
149221 }
150222}
0 commit comments