Skip to content

Commit

Permalink
FIX number of parameters at ArgumentCountError exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kuschel authored and terrafrost committed Jun 4, 2024
1 parent 09ae1d6 commit 50affd7
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 48 deletions.
36 changes: 18 additions & 18 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="phpseclib Unit Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>lib/</directory>
</whitelist>
</filter>
</phpunit>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="phpseclib Unit Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>lib/</directory>
</include>
</source>
</phpunit>
9 changes: 5 additions & 4 deletions src/BCMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,13 @@ public static function __callStatic($name, $arguments)
'sqrt' => 2,
'sub' => 3
];
if (count($arguments) < $params[$name] - 1) {
$cnt = count($arguments);
if ($cnt < $params[$name] - 1) {
$min = $params[$name] - 1;
throw new \ArgumentCountError("bc$name() expects at least $min parameters, " . func_num_args() . " given");
throw new \ArgumentCountError("bc$name() expects at least $min parameters, " . $cnt . " given");
}
if (count($arguments) > $params[$name]) {
$str = "bc$name() expects at most {$params[$name]} parameters, " . func_num_args() . " given";
if ($cnt > $params[$name]) {
$str = "bc$name() expects at most {$params[$name]} parameters, " . $cnt . " given";
throw new \ArgumentCountError($str);
}
$numbers = array_slice($arguments, 0, $params[$name] - 1);
Expand Down
145 changes: 119 additions & 26 deletions tests/BCMathTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
<?php
<?php //declare(strict_types=1);

use bcmath_compat\BCMath;

use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
// use PHPUnit\Framework\Attributes\TestWith;

/**
* @requires extension bcmath
* requires extension bcmath
*/
class BCMathTest extends PHPUnit\Framework\TestCase
#[RequiresPhpExtension('bcmath')]
class BCMathTest extends TestCase
{
static $emsg = '';
/**
* Produces all combinations of test values.
*
* @return array
*/
public function generateTwoParams()
public static function generateTwoParams()
{
$r = [
['9', '9'],
Expand Down Expand Up @@ -45,9 +53,7 @@ public function generateTwoParams()
return $r;
}

/**
* @dataProvider generateTwoParams
*/
#[DataProvider('generateTwoParams')]
public function testAdd(...$params)
{
$a = bcadd(...$params);
Expand All @@ -60,9 +66,7 @@ public function testAdd(...$params)
$this->assertSame($a, $b);
}

/**
* @dataProvider generateTwoParams
*/
#[DataProvider('generateTwoParams')]
public function testSub(...$params)
{
$a = bcsub(...$params);
Expand All @@ -76,9 +80,11 @@ public function testSub(...$params)
}

/**
* @dataProvider generateTwoParams
* @requires PHP 7.3
* requires PHP 7.3
*/

#[RequiresPhp('>7.3')]
#[DataProvider('generateTwoParams')]
public function testMul(...$params)
{
$a = bcmul(...$params);
Expand All @@ -91,9 +97,7 @@ public function testMul(...$params)
$this->assertSame($a, $b);
}

/**
* @dataProvider generateTwoParams
*/
#[DataProvider('generateTwoParams')]
public function testDiv(...$params)
{
if ($params[1] === '0' || $params[1] === '-0') {
Expand All @@ -110,9 +114,12 @@ public function testDiv(...$params)
}

/**
* @dataProvider generateTwoParams
* @requires PHP 7.2
* dataProvider generateTwoParams
* requires PHP 7.2
*/

#[DataProvider('generateTwoParams')]
#[RequiresPhp('>7.2')]
public function testMod(...$params)
{
if ($params[1] === '0' || $params[1] === '-0') {
Expand All @@ -133,7 +140,7 @@ public function testMod(...$params)
*
* @return array
*/
public function generatePowParams()
public static function generatePowParams()
{
return [
['9', '9'],
Expand All @@ -154,8 +161,10 @@ public function generatePowParams()

/**
* @dataProvider generatePowParams
* @requires PHP 7.3
* requires PHP 7.3
*/
#[DataProvider('generatePowParams')]
#[RequiresPhp('>7.3')]
public function testPow(...$params)
{
$a = bcpow(...$params);
Expand All @@ -168,7 +177,7 @@ public function testPow(...$params)
*
* @return array
*/
public function generatePowModParams()
public static function generatePowModParams()
{
return [
['9', '9', '17'],
Expand All @@ -188,10 +197,13 @@ public function generatePowModParams()
}

/**
* @dataProvider generatePowModParams
* @requires PHP 7.3
* dataProvider generatePowModParams
* requires PHP 7.3
*/
public function testPowMod(...$params)
#[DataProvider('generatePowModParams')]
#[RequiresPhp('>7.3')]

public function testPowMod(...$params)
{
$a = bcpowmod(...$params);
$b = BCMath::powmod(...$params);
Expand All @@ -215,9 +227,19 @@ public function testSqrt()

public function testBoolScale()
{
$a = bcadd('5', '2', false);
$b = BCMath::add('5', '2', false);
$this->assertSame($a, $b);
if(false) {
$exception_thrown = false;
try {
$a = bcadd('5', '2', false);
} catch (TypeError $e) {
$exception_thrown = true;
}
$this->assertSame(true, $exception_thrown);
} else {
$a = bcadd('5','2', false);
$b = BCMath::add('5', '2', false);
$this->assertSame($a, $b);
}
}

public function testIntParam()
Expand Down Expand Up @@ -246,4 +268,75 @@ public function setExpectedException($name, $message = null, $code = null)
$this->expectExceptionCode($code);
}
}

public static function generateScaleCallstaticParams()
{
return [
[4],
[4,2],
[4,2,3],
[4,2,3,5],
];
}

#[DataProvider('generateScaleCallstaticParams')]
public function test_argumentsScaleCallstatic(...$params)
{
//scale with 1, 2, 3 parameters
if (func_num_args() == 1) {
bcscale(...$params);
BCMath::scale(...$params);
$scale = bcscale();
$orig = $params[0];
$this->assertSame($orig,$scale);
$scale = BCMath::scale();
$this->assertSame($orig,$scale);
} else {
$exception_thrown = false;
try{
BCMath::scale(...$params);
} catch (ArgumentCountError $e) {
$exception_thrown = true;
}
$this->assertSame(true, $exception_thrown);
if (true) {
// start the unit test with: (showing the wrong given values)
// phpunit --testdox-test testdox.txt --display-skipped
$this->markTestSkipped('ArgumentCountError in ' . $e->getFile() . ':' . $e->getLine() . ' : ' . $e->getMessage());
}
}
}
public static function generatePowModCallstaticParams()
{
return [
['9'],
['9', '17'],
['9', '17', '-111'],
['9', '17', '-111', 5],
['9', '17', '-111', 5, 8],
];
}
#[DataProvider('generatePowModCallstaticParams')]
public function test_argumentsPowModCallstatic(...$params)
{
//scale with 1, 2, 3 parameters
if (func_num_args() > 2 && func_num_args() < 5) {
$a = bcpowmod(...$params);
$b = BCMath::powmod(...$params);
$this->assertSame($a,$b);
} else {
$exception_thrown = false;
try{
BCMath::powmod(...$params);
} catch (ArgumentCountError $e) {
$exception_thrown = true;
}
$this->assertSame(true, $exception_thrown);
if (true) {
// start the unit test with: (showing the wrong given values)
// phpunit --testdox-test testdox.txt --display-skipped
$this->markTestSkipped('ArgumentCountError in ' . $e->getFile() . ':' . $e->getLine() . ' : ' . $e->getMessage());
}
}
}
}

1 comment on commit 50affd7

@terrafrost
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #6 / #7

Please sign in to comment.