aboutsummaryrefslogtreecommitdiff
path: root/tests/MathTest.php
diff options
context:
space:
mode:
authorBruce Wells <brucekwells@gmail.com>2022-12-22 02:52:18 +0300
committerGitHub <noreply@github.com>2022-12-22 02:52:18 +0300
commit7704ba918fbbacfd40a87725b2e01fde58b186d0 (patch)
treebd9889db81eaca0e8dc8be46303ea75d9cbf58ac /tests/MathTest.php
parentc59f4cd15317754d2b50bd4bff2243012e815790 (diff)
Drop php74 (#120)
* PHPStan Level 6 * Drop PHP 7.4 support * Add PHPStan badge to readme * Code style cleanup
Diffstat (limited to 'tests/MathTest.php')
-rw-r--r--tests/MathTest.php108
1 files changed, 50 insertions, 58 deletions
diff --git a/tests/MathTest.php b/tests/MathTest.php
index 0c87c45..2c9197f 100644
--- a/tests/MathTest.php
+++ b/tests/MathTest.php
@@ -31,12 +31,13 @@ class MathTest extends TestCase
$calculator = new MathExecutor();
/** @var float $phpResult */
+ $phpResult = 0.0;
eval('$phpResult = ' . $expression . ';');
try {
$result = $calculator->execute($expression);
} catch (Exception $e) {
- $this->fail(\sprintf('Exception: %s (%s:%d), expression was: %s', \get_class($e), $e->getFile(), $e->getLine(), $expression));
+ $this->fail(\sprintf('Exception: %s (%s:%d), expression was: %s', $e::class, $e->getFile(), $e->getLine(), $expression));
}
$this->assertEquals($phpResult, $result, "Expression was: {$expression}");
}
@@ -47,6 +48,8 @@ class MathTest extends TestCase
* Most tests can go in here. The idea is that each expression will be evaluated by MathExecutor and by PHP with eval.
* The results should be the same. If they are not, then the test fails. No need to add extra test unless you are doing
* something more complex and not a simple mathmatical expression.
+ *
+ * @return array<array<string>>
*/
public function providerExpressions()
{
@@ -270,12 +273,13 @@ class MathTest extends TestCase
}
/** @var float $phpResult */
+ $phpResult = 0.0;
eval('$phpResult = ' . $expected . ';');
try {
$result = $calculator->execute($expression);
} catch (Exception $e) {
- $this->fail(\sprintf('Exception: %s (%s:%d), expression was: %s', \get_class($e), $e->getFile(), $e->getLine(), $expression));
+ $this->fail(\sprintf('Exception: %s (%s:%d), expression was: %s', $e::class, $e->getFile(), $e->getLine(), $expression));
}
$this->assertEquals($phpResult, $result, "Expression was: {$expression}");
}
@@ -286,6 +290,8 @@ class MathTest extends TestCase
* Most tests can go in here. The idea is that each expression will be evaluated by MathExecutor and by PHP with eval.
* The results should be the same. If they are not, then the test fails. No need to add extra test unless you are doing
* something more complex and not a simple mathmatical expression.
+ *
+ * @return array<array<string>>
*/
public function bcMathExpressions()
{
@@ -505,6 +511,8 @@ class MathTest extends TestCase
* Incorrect Expressions data provider
*
* These expressions should not pass validation
+ *
+ * @return array<array<string>>
*/
public function incorrectExpressions()
{
@@ -592,9 +600,7 @@ class MathTest extends TestCase
$this->assertEquals("'teststring", $calculator->execute("'\'teststring'"));
$this->assertEquals("teststring'", $calculator->execute("'teststring\''"));
- $calculator->addFunction('concat', static function($arg1, $arg2) {
- return $arg1 . $arg2;
- });
+ $calculator->addFunction('concat', static fn ($arg1, $arg2) => $arg1 . $arg2);
$this->assertEquals('test"ing', $calculator->execute('concat("test\"","ing")'));
$this->assertEquals("test'ing", $calculator->execute("concat('test\'','ing')"));
}
@@ -608,7 +614,7 @@ class MathTest extends TestCase
$this->assertEquals(\max([1, 5, 2]), $calculator->execute('max(array(1, 5, 2))'));
$calculator->addFunction('arr_with_max_elements', static function($arg1, ...$args) {
$args = \is_array($arg1) ? $arg1 : [$arg1, ...$args];
- \usort($args, static fn ($arr1, $arr2) => \count($arr2) <=> \count($arr1));
+ \usort($args, static fn ($arr1, $arr2) => (\is_countable($arr2) ? \count($arr2) : 0) <=> \count($arr1));
return $args[0];
});
@@ -619,9 +625,7 @@ class MathTest extends TestCase
{
$calculator = new MathExecutor();
- $calculator->addFunction('concat', static function($arg1, $arg2) {
- return $arg1 . $arg2;
- });
+ $calculator->addFunction('concat', static fn ($arg1, $arg2) => $arg1 . $arg2);
$this->assertEquals('testing', $calculator->execute('concat("test","ing")'));
$this->assertEquals('testing', $calculator->execute("concat('test','ing')"));
}
@@ -629,18 +633,14 @@ class MathTest extends TestCase
public function testFunction() : void
{
$calculator = new MathExecutor();
- $calculator->addFunction('round', static function($arg) {
- return \round($arg);
- });
+ $calculator->addFunction('round', static fn ($arg) => \round($arg));
$this->assertEquals(\round(100 / 30), $calculator->execute('round(100/30)'));
}
public function testFunctionUnlimitedParameters() : void
{
$calculator = new MathExecutor();
- $calculator->addFunction('give_me_an_array', static function() {
- return [5, 3, 7, 9, 8];
- });
+ $calculator->addFunction('give_me_an_array', static fn () => [5, 3, 7, 9, 8]);
$this->assertEquals(6.4, $calculator->execute('avg(give_me_an_array())'));
$this->assertEquals(10, $calculator->execute('avg(12,8,15,5)'));
$this->assertEquals(3, $calculator->execute('min(give_me_an_array())'));
@@ -657,9 +657,7 @@ class MathTest extends TestCase
public function testFunctionOptionalParameters() : void
{
$calculator = new MathExecutor();
- $calculator->addFunction('round', static function($num, $precision = 0) {
- return \round($num, $precision);
- });
+ $calculator->addFunction('round', static fn ($num, $precision = 0) => \round($num, $precision));
$this->assertEquals(\round(11.176), $calculator->execute('round(11.176)'));
$this->assertEquals(\round(11.176, 2), $calculator->execute('round(11.176,2)'));
}
@@ -668,9 +666,7 @@ class MathTest extends TestCase
{
$calculator = new MathExecutor();
$this->expectException(IncorrectNumberOfFunctionParametersException::class);
- $calculator->addFunction('myfunc', static function($arg1, $arg2) {
- return $arg1 + $arg2;
- });
+ $calculator->addFunction('myfunc', static fn ($arg1, $arg2) => $arg1 + $arg2);
$calculator->execute('myfunc(1)');
}
@@ -678,9 +674,7 @@ class MathTest extends TestCase
{
$calculator = new MathExecutor();
$this->expectException(IncorrectNumberOfFunctionParametersException::class);
- $calculator->addFunction('myfunc', static function($arg1, $arg2) {
- return $arg1 + $arg2;
- });
+ $calculator->addFunction('myfunc', static fn ($arg1, $arg2) => $arg1 + $arg2);
$calculator->execute('myfunc(1,2,3)');
}
@@ -690,57 +684,57 @@ class MathTest extends TestCase
$this->assertEquals(
30,
$calculator->execute(
- 'if(100 > 99, 30, 0)'
- ),
+ 'if(100 > 99, 30, 0)'
+ ),
'Expression failed: if(100 > 99, 30, 0)'
);
$this->assertEquals(
0,
$calculator->execute(
- 'if(100 < 99, 30, 0)'
- ),
+ 'if(100 < 99, 30, 0)'
+ ),
'Expression failed: if(100 < 99, 30, 0)'
);
$this->assertEquals(
30,
$calculator->execute(
- 'if(98 < 99 && sin(1) < 1, 30, 0)'
- ),
+ 'if(98 < 99 && sin(1) < 1, 30, 0)'
+ ),
'Expression failed: if(98 < 99 && sin(1) < 1, 30, 0)'
);
$this->assertEquals(
40,
$calculator->execute(
- 'if(98 < 99 && sin(1) < 1, max(30, 40), 0)'
- ),
+ 'if(98 < 99 && sin(1) < 1, max(30, 40), 0)'
+ ),
'Expression failed: if(98 < 99 && sin(1) < 1, max(30, 40), 0)'
);
$this->assertEquals(
40,
$calculator->execute(
- 'if(98 < 99 && sin(1) < 1, if(10 > 5, max(30, 40), 1), 0)'
- ),
+ 'if(98 < 99 && sin(1) < 1, if(10 > 5, max(30, 40), 1), 0)'
+ ),
'Expression failed: if(98 < 99 && sin(1) < 1, if(10 > 5, max(30, 40), 1), 0)'
);
$this->assertEquals(
20,
$calculator->execute(
- 'if(98 < 99 && sin(1) > 1, if(10 > 5, max(30, 40), 1), if(4 <= 4, 20, 21))'
- ),
+ 'if(98 < 99 && sin(1) > 1, if(10 > 5, max(30, 40), 1), if(4 <= 4, 20, 21))'
+ ),
'Expression failed: if(98 < 99 && sin(1) > 1, if(10 > 5, max(30, 40), 1), if(4 <= 4, 20, 21))'
);
$this->assertEquals(
\cos(2),
$calculator->execute(
- 'if(98 < 99 && sin(1) >= 1, max(30, 40), cos(2))'
- ),
+ 'if(98 < 99 && sin(1) >= 1, max(30, 40), cos(2))'
+ ),
'Expression failed: if(98 < 99 && sin(1) >= 1, max(30, 40), cos(2))'
);
$this->assertEquals(
\cos(2),
$calculator->execute(
- 'if(cos(2), cos(2), 0)'
- ),
+ 'if(cos(2), cos(2), 0)'
+ ),
'Expression failed: if(cos(2), cos(2), 0)'
);
$trx_amount = 100000;
@@ -749,15 +743,15 @@ class MathTest extends TestCase
$this->assertEquals(
$trx_amount * 0.03,
$calculator->execute(
- 'if($trx_amount < 40000, $trx_amount * 0.06, $trx_amount * 0.03)'
- ),
+ 'if($trx_amount < 40000, $trx_amount * 0.06, $trx_amount * 0.03)'
+ ),
'Expression failed: if($trx_amount < 40000, $trx_amount * 0.06, $trx_amount * 0.03)'
);
$this->assertEquals(
$trx_amount * 0.03,
$calculator->execute(
- 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
- ),
+ 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
+ ),
'Expression failed: if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
);
$trx_amount = 39000;
@@ -765,8 +759,8 @@ class MathTest extends TestCase
$this->assertEquals(
$trx_amount * 0.06,
$calculator->execute(
- 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
- ),
+ 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
+ ),
'Expression failed: if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
);
$trx_amount = 59000;
@@ -774,16 +768,16 @@ class MathTest extends TestCase
$this->assertEquals(
$trx_amount * 0.05,
$calculator->execute(
- 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
- ),
+ 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
+ ),
'Expression failed: if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
);
$this->expectException(IncorrectNumberOfFunctionParametersException::class);
$this->assertEquals(
0.0,
$calculator->execute(
- 'if($trx_amount < 40000, $trx_amount * 0.06)'
- ),
+ 'if($trx_amount < 40000, $trx_amount * 0.06)'
+ ),
'Expression failed: if($trx_amount < 40000, $trx_amount * 0.06)'
);
}
@@ -832,9 +826,7 @@ class MathTest extends TestCase
$calculator = new MathExecutor();
$calculator->addFunction(
'round',
- static function($value, $decimals) {
- return \round($value, $decimals);
- }
+ static fn ($value, $decimals) => \round($value, $decimals)
);
$expression = 'round(100 * 1.111111, 2)';
$phpResult = 0;
@@ -848,9 +840,7 @@ class MathTest extends TestCase
public function testFunctionsWithQuotes() : void
{
$calculator = new MathExecutor();
- $calculator->addFunction('concat', static function($first, $second) {
- return $first . $second;
- });
+ $calculator->addFunction('concat', static fn ($first, $second) => $first . $second);
$this->assertEquals('testing', $calculator->execute('concat("test", "ing")'));
$this->assertEquals('testing', $calculator->execute("concat('test', 'ing')"));
}
@@ -1073,14 +1063,14 @@ class MathTest extends TestCase
/**
* @dataProvider providerExpressionValues
*/
- public function testCalculatingValues($expression, $value) : void
+ public function testCalculatingValues(string $expression, mixed $value) : void
{
$calculator = new MathExecutor();
try {
$result = $calculator->execute($expression);
} catch (Exception $e) {
- $this->fail(\sprintf('Exception: %s (%s:%d), expression was: %s', \get_class($e), $e->getFile(), $e->getLine(), $expression));
+ $this->fail(\sprintf('Exception: %s (%s:%d), expression was: %s', $e::class, $e->getFile(), $e->getLine(), $expression));
}
$this->assertEquals($value, $result, "{$expression} did not evaluate to {$value}");
}
@@ -1091,6 +1081,8 @@ class MathTest extends TestCase
* Most tests can go in here. The idea is that each expression will be evaluated by MathExecutor and by PHP directly.
* The results should be the same. If they are not, then the test fails. No need to add extra test unless you are doing
* something more complex and not a simple mathmatical expression.
+ *
+ * @return array<array<mixed>>
*/
public function providerExpressionValues()
{