Skip to content

Commit 50affd7

Browse files
Thomas Kuschelterrafrost
Thomas Kuschel
authored andcommitted
FIX number of parameters at ArgumentCountError exception
1 parent 09ae1d6 commit 50affd7

File tree

3 files changed

+142
-48
lines changed

3 files changed

+142
-48
lines changed

phpunit.xml.dist

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
5-
backupGlobals="false"
6-
colors="true"
7-
bootstrap="vendor/autoload.php"
8-
>
9-
<testsuites>
10-
<testsuite name="phpseclib Unit Test Suite">
11-
<directory>tests/</directory>
12-
</testsuite>
13-
</testsuites>
14-
15-
<filter>
16-
<whitelist>
17-
<directory>lib/</directory>
18-
</whitelist>
19-
</filter>
20-
</phpunit>
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
cacheDirectory=".phpunit.cache"
9+
>
10+
<testsuites>
11+
<testsuite name="phpseclib Unit Test Suite">
12+
<directory>tests/</directory>
13+
</testsuite>
14+
</testsuites>
15+
<source>
16+
<include>
17+
<directory>lib/</directory>
18+
</include>
19+
</source>
20+
</phpunit>

src/BCMath.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,13 @@ public static function __callStatic($name, $arguments)
363363
'sqrt' => 2,
364364
'sub' => 3
365365
];
366-
if (count($arguments) < $params[$name] - 1) {
366+
$cnt = count($arguments);
367+
if ($cnt < $params[$name] - 1) {
367368
$min = $params[$name] - 1;
368-
throw new \ArgumentCountError("bc$name() expects at least $min parameters, " . func_num_args() . " given");
369+
throw new \ArgumentCountError("bc$name() expects at least $min parameters, " . $cnt . " given");
369370
}
370-
if (count($arguments) > $params[$name]) {
371-
$str = "bc$name() expects at most {$params[$name]} parameters, " . func_num_args() . " given";
371+
if ($cnt > $params[$name]) {
372+
$str = "bc$name() expects at most {$params[$name]} parameters, " . $cnt . " given";
372373
throw new \ArgumentCountError($str);
373374
}
374375
$numbers = array_slice($arguments, 0, $params[$name] - 1);

tests/BCMathTest.php

Lines changed: 119 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
<?php
1+
<?php //declare(strict_types=1);
22

33
use 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

Comments
 (0)