Skip to content

Commit

Permalink
Php Expression Check
Browse files Browse the repository at this point in the history
  • Loading branch information
svilborg committed Jul 17, 2019
1 parent 59399b9 commit e9a001f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Checks/Php/Expression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace Health\Checks\Php;

use Health\Checks\BaseCheck;
use Health\Checks\HealthCheckInterface;

class Expression extends BaseCheck implements HealthCheckInterface
{

/**
*
* {@inheritdoc}
* @see \Health\Checks\HealthCheckInterface::call()
*/
public function call()
{
$builder = $this->getBuilder();

$expression = $this->getParam('expression', '1==1');
$result = $this->getParam('result', true);

$expResult = $this->evalExpression($expression);

if ((is_bool($result) && $result !== (bool) $expResult) || ! preg_match("|{$result}|", $expResult)) {
$builder->down();
}

return $builder->withData('expected_result', $result)
->withData('expression_result', $expResult)
->build();
}

/**
*
* @param string $expression
* @return mixed
*/
private function evalExpression(string $expression)
{
return eval("return {$expression};");
}
}
32 changes: 32 additions & 0 deletions tests/Checks/PhpExpressionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace Tests\Checks;

use Health\Checks\Php\Expression;

class PhpExpressionTest extends CheckTestCase
{

public function testCheckPhpClassExists()
{
$this->assertCheck($this->runCheck(Expression::class, []), 'UP');

$this->assertCheck($this->runCheck(Expression::class, [
'expression' => '1+2'
]), 'DOWN');

$this->assertCheck($this->runCheck(Expression::class, [
'expression' => '1+2',
'result' => '3'
]), 'UP');

$this->assertCheck($this->runCheck(Expression::class, [
'expression' => 'max([1,10,45])',
'result' => '45'
]), 'UP');

$this->assertCheck($this->runCheck(Expression::class, [
'expression' => "\Health\Checks\Php\Expression::class",
'result' => 'Health\\\Checks\\\Php\\\Expression'
]), 'UP');
}
}

0 comments on commit e9a001f

Please sign in to comment.