Skip to content

Commit

Permalink
added rest of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArekX committed May 1, 2019
1 parent 55396a8 commit 0486a3d
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 13 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Array Expression Engine

[![Build Status](https://scrutinizer-ci.com/g/ArekX/array-expression-engine/badges/build.png?b=master)](https://scrutinizer-ci.com/g/ArekX/array-expression-engine/build-status/master) [![Code Coverage](https://scrutinizer-ci.com/g/ArekX/array-expression-engine/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/ArekX/array-expression-engine/?branch=master)

This is an array expression parser which can be used to parse values using
configuration specified in PHP arrays. These arrays can be loaded from anywhere,
like from JSON string, PHP files, etc.
Expand Down Expand Up @@ -290,10 +292,4 @@ $result = $evaluator->run($test, ['sentence' => 'Hello this is cat.']); // Retur

## Tests

Run `composer test` to run tests.

## Test Coverage

Ideally, goal test coverage is 100%, to check the test coverage run `composer coverage`

Current coverage: 100%
Run `composer test` to run tests.
8 changes: 7 additions & 1 deletion src/Operators/BetweenOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@
class BetweenOperator extends BaseOperator
{
/**
* Operator result which will be checked.
*
* @var Operator
*/
public $operandA;

/**
* Operator result which will be used for minimum.
*
* @var Operator
*/
public $operandB;

/**
* Operator result which will be used for maximum.
*
* @var Operator
*/
public $operandC;
Expand All @@ -41,7 +47,7 @@ public function configure(array $config)
$this->setName($config[0] ?? 'unknown');

if (count($config) < 4) {
throw new \InvalidArgumentException("No matching minimum format: ['' expression operator");
throw new \InvalidArgumentException("No matching minimum format: ['{$this->getName()}', <valueExpression>, <minExpression>, <maxExpression>");
}

$this->assertIsExpression($config[1]);
Expand Down
10 changes: 7 additions & 3 deletions src/Operators/GetOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class GetOperator extends BaseOperator
*
* @var string
*/
public $name;
public $key;

/**
* Default value to be returned if that key was not found.
Expand All @@ -47,7 +47,11 @@ public function configure(array $config)
{
$this->setName($config[0] ?? 'unknown');

$this->name = $config[1] ?? '';
if (count($config) < 1) {
throw new \InvalidArgumentException("Minimum format must be satisfied: ['{$this->getName()}']");
}

$this->key = $config[1] ?? '';
$this->default = $config['default'] ?? null;
}

Expand All @@ -59,7 +63,7 @@ public function configure(array $config)
*/
public function evaluate(ValueParser $value)
{
return $value->getValue($this->name, $this->default);
return $value->getValue($this->key, $this->default);
}

}
95 changes: 95 additions & 0 deletions tests/Operators/BetweenOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* @author Aleksandar Panic
* @license http://www.apache.org/licenses/LICENSE-2.0
* @since 1.0.0
**/

namespace tests\Operators;


use ArekX\ArrayExpression\ExpressionParser;
use ArekX\ArrayExpression\Operators\ValueOperator;
use ArekX\ArrayExpression\ValueParsers\ArrayValueParser;
use ArekX\ArrayExpression\ValueParsers\SingleValueParser;
use tests\Mocks\MockOperator;
use tests\Spies\BetweenOperatorSpy;
use tests\Spies\GetOperatorSpy;
use tests\TestCase;

/**
* Class BetweenOperatorTest
* @package tests\Operators
*
*/
class BetweenOperatorTest extends TestCase
{
public function testParserIsSet()
{
$i = $this->createInstance();
$parser = new ExpressionParser();
$i->setParser($parser);
$this->assertSame($parser, $i->getParser());
}

public function testNameIsSet()
{
$i = $this->createInstance();
$i->configure(['between', ['value', 1], ['value', 1], ['value', 1]]);
$this->assertSame('between', $i->getName());
$i->configure([-1 => 'nope', 1 => ['value', 1], 2 => ['value', 1], 3 => ['value', 1]]);
$this->assertSame('unknown', $i->getName());
}

public function testThrowsErrorIfNoParams()
{
$i = $this->createInstance();
$this->expectException(\InvalidArgumentException::class);
$i->configure(['between']);
}

public function testThrowsErrorIfOnlyOneParam()
{
$i = $this->createInstance();
$this->expectException(\InvalidArgumentException::class);
$i->configure(['between', ['value', 1]]);
}

public function testThrowsErrorIfOnlyTwoParams()
{
$i = $this->createInstance();
$this->expectException(\InvalidArgumentException::class);
$i->configure(['between', ['value', 1], ['value', 1]]);
}


public function testBetweenTests()
{
$i = $this->createInstance();

$tests = [
[['between', ['value', 1], ['value', 0], ['value', 2]], true],
[['between', ['value', 22], ['value', 0], ['value', 2]], false],
[['between', ['value', -200], ['value', 0], ['value', 2]], false],
[['between', ['value', 0], ['value', 0], ['value', 2]], true],
[['between', ['value', 2], ['value', 0], ['value', 2]], true],
];

$value = SingleValueParser::from("");
foreach ($tests as $test) {
$i->configure($test[0]);
$this->assertSame($test[1], $i->evaluate($value));
}
}

protected function createInstance(): BetweenOperatorSpy
{
$parser = new ExpressionParser();
$parser->setType('mock', MockOperator::class);
$parser->setType('value', ValueOperator::class);
$operator = new BetweenOperatorSpy();
$operator->setParser($parser);

return $operator;
}
}
2 changes: 1 addition & 1 deletion tests/Operators/CompareOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use tests\TestCase;

/**
* Class OrOperatorTest
* Class CompareOperatorTest
* @package tests\Operators
*
*/
Expand Down
64 changes: 64 additions & 0 deletions tests/Operators/GetOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* @author Aleksandar Panic
* @license http://www.apache.org/licenses/LICENSE-2.0
* @since 1.0.0
**/

namespace tests\Operators;


use ArekX\ArrayExpression\ExpressionParser;
use ArekX\ArrayExpression\ValueParsers\ArrayValueParser;
use tests\Mocks\MockOperator;
use tests\Spies\GetOperatorSpy;
use tests\TestCase;

/**
* Class GetOperatorTest
* @package tests\Operators
*
*/
class GetOperatorTest extends TestCase
{
public function testParserIsSet()
{
$i = $this->createInstance();
$parser = new ExpressionParser();
$i->setParser($parser);
$this->assertSame($parser, $i->getParser());
}

public function testNameIsSet()
{
$i = $this->createInstance();
$i->configure(['get', 'name']);
$this->assertSame('get', $i->getName());
$i->configure([1 => 'get', 2 => 'name']);
$this->assertSame('unknown', $i->getName());
}

public function testAlwaysReturnAValue()
{
$i = $this->createInstance();
$i->configure(['get', 'name']);
$this->assertSame('this is a name', $i->evaluate(ArrayValueParser::from(['name' => 'this is a name'])));
}

public function testThrowsErrorIfNotValidSyntax()
{
$i = $this->createInstance();
$this->expectException(\InvalidArgumentException::class);
$i->configure([]);
}

protected function createInstance(): GetOperatorSpy
{
$parser = new ExpressionParser();
$parser->setType('mock', MockOperator::class);
$operator = new GetOperatorSpy();
$operator->setParser($parser);

return $operator;
}
}
28 changes: 27 additions & 1 deletion tests/Operators/RegexOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
namespace tests\Operators;


use ArekX\ArrayExpression\Exceptions\InvalidEvaluationResultType;
use ArekX\ArrayExpression\ExpressionParser;
use ArekX\ArrayExpression\Operators\GetOperator;
use ArekX\ArrayExpression\Operators\ValueOperator;
use ArekX\ArrayExpression\ValueParsers\ArrayValueParser;
use ArekX\ArrayExpression\ValueParsers\SingleValueParser;
use tests\Mocks\MockOperator;
use tests\Spies\RegexOperatorSpy;
use tests\TestCase;

/**
* Class OrOperatorTest
* Class RegexOperatorTest
* @package tests\Operators
*
*/
Expand Down Expand Up @@ -55,6 +57,29 @@ public function testChecksByByName()
$this->assertFalse($i->evaluate(ArrayValueParser::from(['name' => 'value'])));
}

public function testChecksAgainstExpressionPattern()
{
$i = $this->createInstance();
$i->configure(['regex', ['get', 'name'], ['value', '/^a+$/']]);
$this->assertFalse($i->evaluate(ArrayValueParser::from(['name' => 'value'])));
}

public function testThrowErrorIfNameIsNotString()
{
$i = $this->createInstance();
$this->expectException(InvalidEvaluationResultType::class);
$i->configure(['regex', ['value', false], '/^a+$/']);
$this->assertFalse($i->evaluate(ArrayValueParser::from(['name' => 'value'])));
}

public function testThrowErrorIfPatternIsNotString()
{
$i = $this->createInstance();
$this->expectException(InvalidEvaluationResultType::class);
$i->configure(['regex', ['value', 'test'], ['value', false]]);
$this->assertFalse($i->evaluate(ArrayValueParser::from(['name' => 'value'])));
}


public function testUseDefaultValue()
{
Expand All @@ -68,6 +93,7 @@ protected function createInstance(): RegexOperatorSpy
$parser = new ExpressionParser();
$parser->setType('mock', MockOperator::class);
$parser->setType('get', GetOperator::class);
$parser->setType('value', ValueOperator::class);
$operator = new RegexOperatorSpy();
$operator->setParser($parser);

Expand Down
66 changes: 66 additions & 0 deletions tests/Operators/ValueOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* @author Aleksandar Panic
* @license http://www.apache.org/licenses/LICENSE-2.0
* @since 1.0.0
**/

namespace tests\Operators;


use ArekX\ArrayExpression\ExpressionParser;
use ArekX\ArrayExpression\ValueParsers\SingleValueParser;
use tests\Mocks\MockOperator;
use tests\Spies\ValueOperatorSpy;
use tests\TestCase;

/**
* Class ValueOperatorTest
* @package tests\Operators
*
*/
class ValueOperatorTest extends TestCase
{
public function testParserIsSet()
{
$i = $this->createInstance();
$parser = new ExpressionParser();
$i->setParser($parser);
$this->assertSame($parser, $i->getParser());
}

public function testNameIsSet()
{
$i = $this->createInstance();
$value = rand(1, 50000);
$i->configure(['value', $value]);
$this->assertSame('value', $i->getName());
$i->configure([1 => 'value', 2 => $value]);
$this->assertSame('unknown', $i->getName());
}

public function testAlwaysReturnAValue()
{
$i = $this->createInstance();
$value = rand(1, 50000);
$i->configure(['value', $value]);
$this->assertSame($value, $i->evaluate(SingleValueParser::from('')));
}

public function testThrowsErrorIfNotValidSyntax()
{
$i = $this->createInstance();
$this->expectException(\InvalidArgumentException::class);
$i->configure(['value']);
}

protected function createInstance(): ValueOperatorSpy
{
$parser = new ExpressionParser();
$parser->setType('mock', MockOperator::class);
$operator = new ValueOperatorSpy();
$operator->setParser($parser);

return $operator;
}
}
Loading

0 comments on commit 0486a3d

Please sign in to comment.