1
- <?php
1
+ <?php //declare(strict_types=1);
2
2
3
3
use bcmath_compat \BCMath ;
4
4
5
+ use PHPUnit \Framework \Attributes \RequiresPhpExtension ;
6
+ use PHPUnit \Framework \Attributes \RequiresPhp ;
7
+ use PHPUnit \Framework \Attributes \DataProvider ;
8
+ use PHPUnit \Framework \TestCase ;
9
+ // use PHPUnit\Framework\Attributes\TestWith;
10
+
5
11
/**
6
- * @ requires extension bcmath
12
+ * requires extension bcmath
7
13
*/
8
- class BCMathTest extends PHPUnit \Framework \TestCase
14
+ #[RequiresPhpExtension('bcmath ' )]
15
+ class BCMathTest extends TestCase
9
16
{
17
+ static $ emsg = '' ;
10
18
/**
11
19
* Produces all combinations of test values.
12
20
*
13
21
* @return array
14
22
*/
15
- public function generateTwoParams ()
23
+ public static function generateTwoParams ()
16
24
{
17
25
$ r = [
18
26
['9 ' , '9 ' ],
@@ -45,9 +53,7 @@ public function generateTwoParams()
45
53
return $ r ;
46
54
}
47
55
48
- /**
49
- * @dataProvider generateTwoParams
50
- */
56
+ #[DataProvider('generateTwoParams ' )]
51
57
public function testAdd (...$ params )
52
58
{
53
59
$ a = bcadd (...$ params );
@@ -60,9 +66,7 @@ public function testAdd(...$params)
60
66
$ this ->assertSame ($ a , $ b );
61
67
}
62
68
63
- /**
64
- * @dataProvider generateTwoParams
65
- */
69
+ #[DataProvider('generateTwoParams ' )]
66
70
public function testSub (...$ params )
67
71
{
68
72
$ a = bcsub (...$ params );
@@ -76,9 +80,11 @@ public function testSub(...$params)
76
80
}
77
81
78
82
/**
79
- * @dataProvider generateTwoParams
80
- * @requires PHP 7.3
83
+ * requires PHP 7.3
81
84
*/
85
+
86
+ #[RequiresPhp('>7.3 ' )]
87
+ #[DataProvider('generateTwoParams ' )]
82
88
public function testMul (...$ params )
83
89
{
84
90
$ a = bcmul (...$ params );
@@ -91,9 +97,7 @@ public function testMul(...$params)
91
97
$ this ->assertSame ($ a , $ b );
92
98
}
93
99
94
- /**
95
- * @dataProvider generateTwoParams
96
- */
100
+ #[DataProvider('generateTwoParams ' )]
97
101
public function testDiv (...$ params )
98
102
{
99
103
if ($ params [1 ] === '0 ' || $ params [1 ] === '-0 ' ) {
@@ -110,9 +114,12 @@ public function testDiv(...$params)
110
114
}
111
115
112
116
/**
113
- * @ dataProvider generateTwoParams
114
- * @ requires PHP 7.2
117
+ * dataProvider generateTwoParams
118
+ * requires PHP 7.2
115
119
*/
120
+
121
+ #[DataProvider('generateTwoParams ' )]
122
+ #[RequiresPhp('>7.2 ' )]
116
123
public function testMod (...$ params )
117
124
{
118
125
if ($ params [1 ] === '0 ' || $ params [1 ] === '-0 ' ) {
@@ -133,7 +140,7 @@ public function testMod(...$params)
133
140
*
134
141
* @return array
135
142
*/
136
- public function generatePowParams ()
143
+ public static function generatePowParams ()
137
144
{
138
145
return [
139
146
['9 ' , '9 ' ],
@@ -154,8 +161,10 @@ public function generatePowParams()
154
161
155
162
/**
156
163
* @dataProvider generatePowParams
157
- * @ requires PHP 7.3
164
+ * requires PHP 7.3
158
165
*/
166
+ #[DataProvider('generatePowParams ' )]
167
+ #[RequiresPhp('>7.3 ' )]
159
168
public function testPow (...$ params )
160
169
{
161
170
$ a = bcpow (...$ params );
@@ -168,7 +177,7 @@ public function testPow(...$params)
168
177
*
169
178
* @return array
170
179
*/
171
- public function generatePowModParams ()
180
+ public static function generatePowModParams ()
172
181
{
173
182
return [
174
183
['9 ' , '9 ' , '17 ' ],
@@ -188,10 +197,13 @@ public function generatePowModParams()
188
197
}
189
198
190
199
/**
191
- * @ dataProvider generatePowModParams
192
- * @ requires PHP 7.3
200
+ * dataProvider generatePowModParams
201
+ * requires PHP 7.3
193
202
*/
194
- public function testPowMod (...$ params )
203
+ #[DataProvider('generatePowModParams ' )]
204
+ #[RequiresPhp('>7.3 ' )]
205
+
206
+ public function testPowMod (...$ params )
195
207
{
196
208
$ a = bcpowmod (...$ params );
197
209
$ b = BCMath::powmod (...$ params );
@@ -215,9 +227,19 @@ public function testSqrt()
215
227
216
228
public function testBoolScale ()
217
229
{
218
- $ a = bcadd ('5 ' , '2 ' , false );
219
- $ b = BCMath::add ('5 ' , '2 ' , false );
220
- $ this ->assertSame ($ a , $ b );
230
+ if (false ) {
231
+ $ exception_thrown = false ;
232
+ try {
233
+ $ a = bcadd ('5 ' , '2 ' , false );
234
+ } catch (TypeError $ e ) {
235
+ $ exception_thrown = true ;
236
+ }
237
+ $ this ->assertSame (true , $ exception_thrown );
238
+ } else {
239
+ $ a = bcadd ('5 ' ,'2 ' , false );
240
+ $ b = BCMath::add ('5 ' , '2 ' , false );
241
+ $ this ->assertSame ($ a , $ b );
242
+ }
221
243
}
222
244
223
245
public function testIntParam ()
@@ -246,4 +268,75 @@ public function setExpectedException($name, $message = null, $code = null)
246
268
$ this ->expectExceptionCode ($ code );
247
269
}
248
270
}
271
+
272
+ public static function generateScaleCallstaticParams ()
273
+ {
274
+ return [
275
+ [4 ],
276
+ [4 ,2 ],
277
+ [4 ,2 ,3 ],
278
+ [4 ,2 ,3 ,5 ],
279
+ ];
280
+ }
281
+
282
+ #[DataProvider('generateScaleCallstaticParams ' )]
283
+ public function test_argumentsScaleCallstatic (...$ params )
284
+ {
285
+ //scale with 1, 2, 3 parameters
286
+ if (func_num_args () == 1 ) {
287
+ bcscale (...$ params );
288
+ BCMath::scale (...$ params );
289
+ $ scale = bcscale ();
290
+ $ orig = $ params [0 ];
291
+ $ this ->assertSame ($ orig ,$ scale );
292
+ $ scale = BCMath::scale ();
293
+ $ this ->assertSame ($ orig ,$ scale );
294
+ } else {
295
+ $ exception_thrown = false ;
296
+ try {
297
+ BCMath::scale (...$ params );
298
+ } catch (ArgumentCountError $ e ) {
299
+ $ exception_thrown = true ;
300
+ }
301
+ $ this ->assertSame (true , $ exception_thrown );
302
+ if (true ) {
303
+ // start the unit test with: (showing the wrong given values)
304
+ // phpunit --testdox-test testdox.txt --display-skipped
305
+ $ this ->markTestSkipped ('ArgumentCountError in ' . $ e ->getFile () . ': ' . $ e ->getLine () . ' : ' . $ e ->getMessage ());
306
+ }
307
+ }
308
+ }
309
+ public static function generatePowModCallstaticParams ()
310
+ {
311
+ return [
312
+ ['9 ' ],
313
+ ['9 ' , '17 ' ],
314
+ ['9 ' , '17 ' , '-111 ' ],
315
+ ['9 ' , '17 ' , '-111 ' , 5 ],
316
+ ['9 ' , '17 ' , '-111 ' , 5 , 8 ],
317
+ ];
318
+ }
319
+ #[DataProvider('generatePowModCallstaticParams ' )]
320
+ public function test_argumentsPowModCallstatic (...$ params )
321
+ {
322
+ //scale with 1, 2, 3 parameters
323
+ if (func_num_args () > 2 && func_num_args () < 5 ) {
324
+ $ a = bcpowmod (...$ params );
325
+ $ b = BCMath::powmod (...$ params );
326
+ $ this ->assertSame ($ a ,$ b );
327
+ } else {
328
+ $ exception_thrown = false ;
329
+ try {
330
+ BCMath::powmod (...$ params );
331
+ } catch (ArgumentCountError $ e ) {
332
+ $ exception_thrown = true ;
333
+ }
334
+ $ this ->assertSame (true , $ exception_thrown );
335
+ if (true ) {
336
+ // start the unit test with: (showing the wrong given values)
337
+ // phpunit --testdox-test testdox.txt --display-skipped
338
+ $ this ->markTestSkipped ('ArgumentCountError in ' . $ e ->getFile () . ': ' . $ e ->getLine () . ' : ' . $ e ->getMessage ());
339
+ }
340
+ }
341
+ }
249
342
}
0 commit comments