aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJavier Marín <javier@marinros.com>2022-05-09 21:13:30 +0300
committerGitHub <noreply@github.com>2022-05-09 21:13:30 +0300
commit645f1dfbc6310185b73852c5008ef321b66a0f18 (patch)
tree2efdfcf4aabeb3c4bc75a80aac7e54dbe68aebe5 /tests
parentb7b46bfc476ea0d22e0e92144f68aa81d390fff0 (diff)
Two more tests + some code refactoring (#104)
* test: add testNullReturnType and testUnsupportedOperands * refactor: fix PhpDoc comments and use PHP 7.4 arrow functions * refactor: fix PHP-CS-Fixer issues * test: run testUnsupportedOperands() only on PHP8+ Co-authored-by: Javier Marín <contacto@ideatic.net>
Diffstat (limited to 'tests')
-rw-r--r--tests/MathTest.php35
1 files changed, 28 insertions, 7 deletions
diff --git a/tests/MathTest.php b/tests/MathTest.php
index 9567dd2..f82452b 100644
--- a/tests/MathTest.php
+++ b/tests/MathTest.php
@@ -497,7 +497,6 @@ class MathTest extends TestCase
if ('undefined' == $varName) {
return 3;
}
-
}
);
$this->assertEquals(15, $calculator->execute('5 * undefined'));
@@ -527,6 +526,13 @@ class MathTest extends TestCase
$this->assertEquals(1.5e+9, $calculator->execute('1.5e+9'));
}
+ public function testNullReturnType() : void
+ {
+ $calculator = new MathExecutor();
+ $calculator->setVar('nullValue', null);
+ $this->assertEquals(null, $calculator->execute('nullValue'));
+ }
+
public function testGetFunctionsReturnsArray() : void
{
$calculator = new MathExecutor();
@@ -568,7 +574,7 @@ class MathTest extends TestCase
$calculator->setVar('null', null);
$calculator->setVar('float', 1.1);
$calculator->setVar('string', 'string');
- $this->assertEquals(8, \count($calculator->getVars()));
+ $this->assertCount(8, $calculator->getVars());
$this->assertEquals(true, $calculator->getVar('boolTrue'));
$this->assertEquals(false, $calculator->getVar('boolFalse'));
$this->assertEquals(1, $calculator->getVar('int'));
@@ -719,19 +725,34 @@ class MathTest extends TestCase
$this->assertEquals(256, $calculator->execute('2 ^ 8')); // second arg $cache is true by default
$this->assertIsArray($calculator->getCache());
- $this->assertEquals(1, \count($calculator->getCache()));
+ $this->assertCount(1, $calculator->getCache());
$this->assertEquals(512, $calculator->execute('2 ^ 9', true));
- $this->assertEquals(2, \count($calculator->getCache()));
+ $this->assertCount(2, $calculator->getCache());
$this->assertEquals(1024, $calculator->execute('2 ^ 10', false));
- $this->assertEquals(2, \count($calculator->getCache()));
+ $this->assertCount(2, $calculator->getCache());
$calculator->clearCache();
$this->assertIsArray($calculator->getCache());
- $this->assertEquals(0, \count($calculator->getCache()));
+ $this->assertCount(0, $calculator->getCache());
$this->assertEquals(2048, $calculator->execute('2 ^ 11', false));
- $this->assertEquals(0, \count($calculator->getCache()));
+ $this->assertCount(0, $calculator->getCache());
+ }
+
+ public function testUnsupportedOperands() : void
+ {
+ if (\version_compare(PHP_VERSION, '8') >= 0) {
+ $calculator = new MathExecutor();
+
+ $calculator->setVar('stringVar', 'string');
+ $calculator->setVar('intVar', 1);
+
+ $this->expectException(\TypeError::class);
+ $calculator->execute('stringVar + intVar');
+ } else {
+ $this->expectNotToPerformAssertions();
+ }
}
}