Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX number of parameters at ArgumentCountError exception #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand All @@ -24,4 +24,4 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
php-version: ['8.1', '8.2', '8.3']
php-version: ['8.1', '8.2', '8.3']
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"phpseclib/phpseclib": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35|^5.7|^6.0|^9.4",
"phpunit/phpunit": "^10.5|^11.1",
"squizlabs/php_codesniffer": "^3.0"
},
"suggest": {
Expand Down
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());
}
}
}
}