17
17
use Symfony \Component \Serializer \Exception \NotNormalizableValueException ;
18
18
use Symfony \Component \Serializer \Normalizer \NumberNormalizer ;
19
19
20
- /**
21
- * @requires PHP 8.4
22
- * @requires extension bcmath
23
- * @requires extension gmp
24
- */
25
20
class NumberNormalizerTest extends TestCase
26
21
{
27
22
private NumberNormalizer $ normalizer ;
@@ -41,8 +36,14 @@ public function testSupportsNormalization(mixed $data, bool $expected)
41
36
42
37
public static function supportsNormalizationProvider (): iterable
43
38
{
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
+
46
47
yield 'object with similar properties as Number ' => [(object ) ['value ' => '1.23 ' , 'scale ' => 2 ], false ];
47
48
yield 'stdClass ' => [new \stdClass (), false ];
48
49
yield 'string ' => ['1.23 ' , false ];
@@ -51,20 +52,40 @@ public static function supportsNormalizationProvider(): iterable
51
52
}
52
53
53
54
/**
54
- * @dataProvider normalizeGoodValueProvider
55
+ * @requires extension bcmath
56
+ *
57
+ * @dataProvider normalizeGoodBcMathNumberValueProvider
55
58
*/
56
- public function testNormalize (mixed $ data , mixed $ expected )
59
+ public function testNormalizeBcMathNumber (mixed $ data , mixed $ expected )
57
60
{
58
61
$ this ->assertSame ($ expected , $ this ->normalizer ->normalize ($ data ));
59
62
}
60
63
61
- public static function normalizeGoodValueProvider (): iterable
64
+ public static function normalizeGoodBcMathNumberValueProvider (): iterable
62
65
{
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 (mixed $ data , mixed $ 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
+ }
68
89
}
69
90
70
91
/**
@@ -86,65 +107,116 @@ public static function normalizeBadValueProvider(): iterable
86
107
}
87
108
88
109
/**
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
90
120
*/
91
- public function testSupportsDenormalization ( mixed $ data , string $ type , bool $ expected )
121
+ public function testSupportsGmpDenormalization ( )
92
122
{
93
- $ this ->assertSame ( $ expected , $ this ->normalizer ->supportsDenormalization ($ data , $ type ));
123
+ $ this ->assertFalse ( $ this ->normalizer ->supportsDenormalization (null , \ GMP ::class ));
94
124
}
95
125
96
- public static function supportsDenormalizationProvider (): iterable
126
+ public function testDoesNotSupportOtherValuesDenormalization ()
97
127
{
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));
101
129
}
102
130
103
131
/**
104
- * @dataProvider denormalizeGoodValueProvider
132
+ * @requires PHP 8.4
133
+ * @requires extension bcmath
134
+ *
135
+ * @dataProvider denormalizeGoodBcMathNumberValueProvider
105
136
*/
106
- public function testDenormalize (mixed $ data , string $ type , mixed $ expected )
137
+ public function testDenormalizeBcMathNumber (mixed $ data , string $ type , mixed $ expected )
107
138
{
108
139
$ this ->assertEquals ($ expected , $ this ->normalizer ->denormalize ($ data , $ type ));
109
140
}
110
141
111
- public static function denormalizeGoodValueProvider (): iterable
142
+ public static function denormalizeGoodBcMathNumberValueProvider (): iterable
112
143
{
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
+ }
118
149
}
119
150
120
151
/**
121
- * @dataProvider denormalizeBadValueProvider
152
+ * @dataProvider denormalizeGoodGmpValueProvider
122
153
*/
123
- public function testDenormalizeBadValueThrows (mixed $ data , string $ type , string $ expectedException , string $ expectedExceptionMessage )
154
+ public function testDenormalizeGmp (mixed $ data , string $ type , mixed $ 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 )
124
174
{
125
175
$ this ->expectException ($ expectedException );
126
176
$ this ->expectExceptionMessage ($ expectedExceptionMessage );
127
177
128
178
$ this ->normalizer ->denormalize ($ data , $ type );
129
179
}
130
180
131
- public static function denormalizeBadValueProvider (): iterable
181
+ public static function denormalizeBadBcMathNumberValueProvider (): iterable
132
182
{
133
183
$ stringOrDecimalExpectedMessage = 'The data must be a "string" representing a decimal number, or an "int". ' ;
134
184
yield 'Number, null ' => [null , Number::class, NotNormalizableValueException::class, $ stringOrDecimalExpectedMessage ];
135
185
yield 'Number, boolean ' => [true , Number::class, NotNormalizableValueException::class, $ stringOrDecimalExpectedMessage ];
136
186
yield 'Number, object ' => [new \stdClass (), Number::class, NotNormalizableValueException::class, $ stringOrDecimalExpectedMessage ];
137
187
yield 'Number, non-numeric string ' => ['foobar ' , Number::class, NotNormalizableValueException::class, $ stringOrDecimalExpectedMessage ];
138
188
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
+ }
139
203
204
+ public static function denormalizeBadGmpValueProvider (): iterable
205
+ {
140
206
$ stringOrIntExpectedMessage = 'The data must be a "string" representing an integer, or an "int". ' ;
141
207
yield 'GMP, null ' => [null , \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
142
208
yield 'GMP, boolean ' => [true , \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
143
209
yield 'GMP, object ' => [new \stdClass (), \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
144
210
yield 'GMP, non-numeric string ' => ['foobar ' , \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
145
211
yield 'GMP, scale > 0 ' => ['1.23 ' , \GMP ::class, NotNormalizableValueException::class, $ stringOrIntExpectedMessage ];
146
212
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. ' );
147
219
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) ;
149
221
}
150
222
}
0 commit comments