1- <?php
1+ <?php //declare(strict_types=1);
22
33use bcmath_compat \BCMath ;
44
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+
511/**
6- * @ requires extension bcmath
12+ * requires extension bcmath
713 */
8- class BCMathTest extends PHPUnit \Framework \TestCase
14+ #[RequiresPhpExtension('bcmath ' )]
15+ class BCMathTest extends TestCase
916{
17+ static $ emsg = '' ;
1018 /**
1119 * Produces all combinations of test values.
1220 *
1321 * @return array
1422 */
15- public function generateTwoParams ()
23+ public static function generateTwoParams ()
1624 {
1725 $ r = [
1826 ['9 ' , '9 ' ],
@@ -45,9 +53,7 @@ public function generateTwoParams()
4553 return $ r ;
4654 }
4755
48- /**
49- * @dataProvider generateTwoParams
50- */
56+ #[DataProvider('generateTwoParams ' )]
5157 public function testAdd (...$ params )
5258 {
5359 $ a = bcadd (...$ params );
@@ -60,9 +66,7 @@ public function testAdd(...$params)
6066 $ this ->assertSame ($ a , $ b );
6167 }
6268
63- /**
64- * @dataProvider generateTwoParams
65- */
69+ #[DataProvider('generateTwoParams ' )]
6670 public function testSub (...$ params )
6771 {
6872 $ a = bcsub (...$ params );
@@ -76,9 +80,11 @@ public function testSub(...$params)
7680 }
7781
7882 /**
79- * @dataProvider generateTwoParams
80- * @requires PHP 7.3
83+ * requires PHP 7.3
8184 */
85+
86+ #[RequiresPhp('>7.3 ' )]
87+ #[DataProvider('generateTwoParams ' )]
8288 public function testMul (...$ params )
8389 {
8490 $ a = bcmul (...$ params );
@@ -91,9 +97,7 @@ public function testMul(...$params)
9197 $ this ->assertSame ($ a , $ b );
9298 }
9399
94- /**
95- * @dataProvider generateTwoParams
96- */
100+ #[DataProvider('generateTwoParams ' )]
97101 public function testDiv (...$ params )
98102 {
99103 if ($ params [1 ] === '0 ' || $ params [1 ] === '-0 ' ) {
@@ -110,9 +114,12 @@ public function testDiv(...$params)
110114 }
111115
112116 /**
113- * @ dataProvider generateTwoParams
114- * @ requires PHP 7.2
117+ * dataProvider generateTwoParams
118+ * requires PHP 7.2
115119 */
120+
121+ #[DataProvider('generateTwoParams ' )]
122+ #[RequiresPhp('>7.2 ' )]
116123 public function testMod (...$ params )
117124 {
118125 if ($ params [1 ] === '0 ' || $ params [1 ] === '-0 ' ) {
@@ -133,7 +140,7 @@ public function testMod(...$params)
133140 *
134141 * @return array
135142 */
136- public function generatePowParams ()
143+ public static function generatePowParams ()
137144 {
138145 return [
139146 ['9 ' , '9 ' ],
@@ -154,8 +161,10 @@ public function generatePowParams()
154161
155162 /**
156163 * @dataProvider generatePowParams
157- * @ requires PHP 7.3
164+ * requires PHP 7.3
158165 */
166+ #[DataProvider('generatePowParams ' )]
167+ #[RequiresPhp('>7.3 ' )]
159168 public function testPow (...$ params )
160169 {
161170 $ a = bcpow (...$ params );
@@ -168,7 +177,7 @@ public function testPow(...$params)
168177 *
169178 * @return array
170179 */
171- public function generatePowModParams ()
180+ public static function generatePowModParams ()
172181 {
173182 return [
174183 ['9 ' , '9 ' , '17 ' ],
@@ -188,10 +197,13 @@ public function generatePowModParams()
188197 }
189198
190199 /**
191- * @ dataProvider generatePowModParams
192- * @ requires PHP 7.3
200+ * dataProvider generatePowModParams
201+ * requires PHP 7.3
193202 */
194- public function testPowMod (...$ params )
203+ #[DataProvider('generatePowModParams ' )]
204+ #[RequiresPhp('>7.3 ' )]
205+
206+ public function testPowMod (...$ params )
195207 {
196208 $ a = bcpowmod (...$ params );
197209 $ b = BCMath::powmod (...$ params );
@@ -215,9 +227,19 @@ public function testSqrt()
215227
216228 public function testBoolScale ()
217229 {
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+ }
221243 }
222244
223245 public function testIntParam ()
@@ -246,4 +268,75 @@ public function setExpectedException($name, $message = null, $code = null)
246268 $ this ->expectExceptionCode ($ code );
247269 }
248270 }
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+ }
249342}
0 commit comments