aboutsummaryrefslogtreecommitdiff
path: root/tests/MathTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/MathTest.php')
-rw-r--r--tests/MathTest.php46
1 files changed, 41 insertions, 5 deletions
diff --git a/tests/MathTest.php b/tests/MathTest.php
index 14de2bf..07b94fa 100644
--- a/tests/MathTest.php
+++ b/tests/MathTest.php
@@ -573,6 +573,9 @@ class MathTest extends TestCase
$this->assertEquals(null, $calculator->getVar('null'));
$this->assertEquals(1.1, $calculator->getVar('float'));
$this->assertEquals('string', $calculator->getVar('string'));
+
+ $this->expectException(MathExecutorException::class);
+ $calculator->setVar('validVar', new \DateTime());
}
public function testSetVarsDoesNotAcceptObject()
@@ -592,16 +595,49 @@ class MathTest extends TestCase
public function testSetCustomVarValidator()
{
$calculator = new MathExecutor();
- $calculator->setVarValidationHandler(function ($name, $variable) {
- if ($name === 'invalidVar' && $variable === 'invalid') {
- throw new MathExecutorException("Invalid variable");
+ $calculator->setVarValidationHandler(function (string $name, $variable) {
+ // allow all scalars and null
+ if (is_scalar($variable) || $variable === null) {
+ return;
+ }
+ // Allow variables of type DateTime, but not others
+ if (! $variable instanceof \DateTime) {
+ throw new MathExecutorException("Invalid variable type");
+ }
+ });
+
+ $calculator->setVar('validFloat', 0.0);
+ $calculator->setVar('validInt', 0);
+ $calculator->setVar('validTrue', true);
+ $calculator->setVar('validFalse', false);
+ $calculator->setVar('validString', 'string');
+ $calculator->setVar('validNull', null);
+ $calculator->setVar('validDateTime', new \DateTime());
+
+ $this->expectException(MathExecutorException::class);
+ $calculator->setVar('validVar', $this);
+ }
+
+ public function testSetCustomVarNameValidator()
+ {
+ $calculator = new MathExecutor();
+ $calculator->setVarValidationHandler(function (string $name, $variable) {
+ // don't allow variable names with the word invalid in them
+ if (str_contains($name, 'invalid')) {
+ throw new MathExecutorException("Invalid variable name");
}
});
- $calculator->setVar('valid', $this);
+ $calculator->setVar('validFloat', 0.0);
+ $calculator->setVar('validInt', 0);
+ $calculator->setVar('validTrue', true);
+ $calculator->setVar('validFalse', false);
+ $calculator->setVar('validString', 'string');
+ $calculator->setVar('validNull', null);
+ $calculator->setVar('validDateTime', new \DateTime());
$this->expectException(MathExecutorException::class);
- $calculator->setVar('invalidVar', 'invalid');
+ $calculator->setVar('invalidVar', 12);
}
public function testVarExists()