Skip to content

Commit 0486a3d

Browse files
committed
added rest of tests
1 parent 55396a8 commit 0486a3d

File tree

11 files changed

+324
-13
lines changed

11 files changed

+324
-13
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Array Expression Engine
22

3+
[![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)
4+
35
This is an array expression parser which can be used to parse values using
46
configuration specified in PHP arrays. These arrays can be loaded from anywhere,
57
like from JSON string, PHP files, etc.
@@ -290,10 +292,4 @@ $result = $evaluator->run($test, ['sentence' => 'Hello this is cat.']); // Retur
290292

291293
## Tests
292294

293-
Run `composer test` to run tests.
294-
295-
## Test Coverage
296-
297-
Ideally, goal test coverage is 100%, to check the test coverage run `composer coverage`
298-
299-
Current coverage: 100%
295+
Run `composer test` to run tests.

src/Operators/BetweenOperator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@
1919
class BetweenOperator extends BaseOperator
2020
{
2121
/**
22+
* Operator result which will be checked.
23+
*
2224
* @var Operator
2325
*/
2426
public $operandA;
2527

2628
/**
29+
* Operator result which will be used for minimum.
30+
*
2731
* @var Operator
2832
*/
2933
public $operandB;
3034

3135
/**
36+
* Operator result which will be used for maximum.
37+
*
3238
* @var Operator
3339
*/
3440
public $operandC;
@@ -41,7 +47,7 @@ public function configure(array $config)
4147
$this->setName($config[0] ?? 'unknown');
4248

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

4753
$this->assertIsExpression($config[1]);

src/Operators/GetOperator.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class GetOperator extends BaseOperator
2525
*
2626
* @var string
2727
*/
28-
public $name;
28+
public $key;
2929

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

50-
$this->name = $config[1] ?? '';
50+
if (count($config) < 1) {
51+
throw new \InvalidArgumentException("Minimum format must be satisfied: ['{$this->getName()}']");
52+
}
53+
54+
$this->key = $config[1] ?? '';
5155
$this->default = $config['default'] ?? null;
5256
}
5357

@@ -59,7 +63,7 @@ public function configure(array $config)
5963
*/
6064
public function evaluate(ValueParser $value)
6165
{
62-
return $value->getValue($this->name, $this->default);
66+
return $value->getValue($this->key, $this->default);
6367
}
6468

6569
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* @author Aleksandar Panic
4+
* @license http://www.apache.org/licenses/LICENSE-2.0
5+
* @since 1.0.0
6+
**/
7+
8+
namespace tests\Operators;
9+
10+
11+
use ArekX\ArrayExpression\ExpressionParser;
12+
use ArekX\ArrayExpression\Operators\ValueOperator;
13+
use ArekX\ArrayExpression\ValueParsers\ArrayValueParser;
14+
use ArekX\ArrayExpression\ValueParsers\SingleValueParser;
15+
use tests\Mocks\MockOperator;
16+
use tests\Spies\BetweenOperatorSpy;
17+
use tests\Spies\GetOperatorSpy;
18+
use tests\TestCase;
19+
20+
/**
21+
* Class BetweenOperatorTest
22+
* @package tests\Operators
23+
*
24+
*/
25+
class BetweenOperatorTest extends TestCase
26+
{
27+
public function testParserIsSet()
28+
{
29+
$i = $this->createInstance();
30+
$parser = new ExpressionParser();
31+
$i->setParser($parser);
32+
$this->assertSame($parser, $i->getParser());
33+
}
34+
35+
public function testNameIsSet()
36+
{
37+
$i = $this->createInstance();
38+
$i->configure(['between', ['value', 1], ['value', 1], ['value', 1]]);
39+
$this->assertSame('between', $i->getName());
40+
$i->configure([-1 => 'nope', 1 => ['value', 1], 2 => ['value', 1], 3 => ['value', 1]]);
41+
$this->assertSame('unknown', $i->getName());
42+
}
43+
44+
public function testThrowsErrorIfNoParams()
45+
{
46+
$i = $this->createInstance();
47+
$this->expectException(\InvalidArgumentException::class);
48+
$i->configure(['between']);
49+
}
50+
51+
public function testThrowsErrorIfOnlyOneParam()
52+
{
53+
$i = $this->createInstance();
54+
$this->expectException(\InvalidArgumentException::class);
55+
$i->configure(['between', ['value', 1]]);
56+
}
57+
58+
public function testThrowsErrorIfOnlyTwoParams()
59+
{
60+
$i = $this->createInstance();
61+
$this->expectException(\InvalidArgumentException::class);
62+
$i->configure(['between', ['value', 1], ['value', 1]]);
63+
}
64+
65+
66+
public function testBetweenTests()
67+
{
68+
$i = $this->createInstance();
69+
70+
$tests = [
71+
[['between', ['value', 1], ['value', 0], ['value', 2]], true],
72+
[['between', ['value', 22], ['value', 0], ['value', 2]], false],
73+
[['between', ['value', -200], ['value', 0], ['value', 2]], false],
74+
[['between', ['value', 0], ['value', 0], ['value', 2]], true],
75+
[['between', ['value', 2], ['value', 0], ['value', 2]], true],
76+
];
77+
78+
$value = SingleValueParser::from("");
79+
foreach ($tests as $test) {
80+
$i->configure($test[0]);
81+
$this->assertSame($test[1], $i->evaluate($value));
82+
}
83+
}
84+
85+
protected function createInstance(): BetweenOperatorSpy
86+
{
87+
$parser = new ExpressionParser();
88+
$parser->setType('mock', MockOperator::class);
89+
$parser->setType('value', ValueOperator::class);
90+
$operator = new BetweenOperatorSpy();
91+
$operator->setParser($parser);
92+
93+
return $operator;
94+
}
95+
}

tests/Operators/CompareOperatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use tests\TestCase;
1919

2020
/**
21-
* Class OrOperatorTest
21+
* Class CompareOperatorTest
2222
* @package tests\Operators
2323
*
2424
*/
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* @author Aleksandar Panic
4+
* @license http://www.apache.org/licenses/LICENSE-2.0
5+
* @since 1.0.0
6+
**/
7+
8+
namespace tests\Operators;
9+
10+
11+
use ArekX\ArrayExpression\ExpressionParser;
12+
use ArekX\ArrayExpression\ValueParsers\ArrayValueParser;
13+
use tests\Mocks\MockOperator;
14+
use tests\Spies\GetOperatorSpy;
15+
use tests\TestCase;
16+
17+
/**
18+
* Class GetOperatorTest
19+
* @package tests\Operators
20+
*
21+
*/
22+
class GetOperatorTest extends TestCase
23+
{
24+
public function testParserIsSet()
25+
{
26+
$i = $this->createInstance();
27+
$parser = new ExpressionParser();
28+
$i->setParser($parser);
29+
$this->assertSame($parser, $i->getParser());
30+
}
31+
32+
public function testNameIsSet()
33+
{
34+
$i = $this->createInstance();
35+
$i->configure(['get', 'name']);
36+
$this->assertSame('get', $i->getName());
37+
$i->configure([1 => 'get', 2 => 'name']);
38+
$this->assertSame('unknown', $i->getName());
39+
}
40+
41+
public function testAlwaysReturnAValue()
42+
{
43+
$i = $this->createInstance();
44+
$i->configure(['get', 'name']);
45+
$this->assertSame('this is a name', $i->evaluate(ArrayValueParser::from(['name' => 'this is a name'])));
46+
}
47+
48+
public function testThrowsErrorIfNotValidSyntax()
49+
{
50+
$i = $this->createInstance();
51+
$this->expectException(\InvalidArgumentException::class);
52+
$i->configure([]);
53+
}
54+
55+
protected function createInstance(): GetOperatorSpy
56+
{
57+
$parser = new ExpressionParser();
58+
$parser->setType('mock', MockOperator::class);
59+
$operator = new GetOperatorSpy();
60+
$operator->setParser($parser);
61+
62+
return $operator;
63+
}
64+
}

tests/Operators/RegexOperatorTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
namespace tests\Operators;
99

1010

11+
use ArekX\ArrayExpression\Exceptions\InvalidEvaluationResultType;
1112
use ArekX\ArrayExpression\ExpressionParser;
1213
use ArekX\ArrayExpression\Operators\GetOperator;
14+
use ArekX\ArrayExpression\Operators\ValueOperator;
1315
use ArekX\ArrayExpression\ValueParsers\ArrayValueParser;
1416
use ArekX\ArrayExpression\ValueParsers\SingleValueParser;
1517
use tests\Mocks\MockOperator;
1618
use tests\Spies\RegexOperatorSpy;
1719
use tests\TestCase;
1820

1921
/**
20-
* Class OrOperatorTest
22+
* Class RegexOperatorTest
2123
* @package tests\Operators
2224
*
2325
*/
@@ -55,6 +57,29 @@ public function testChecksByByName()
5557
$this->assertFalse($i->evaluate(ArrayValueParser::from(['name' => 'value'])));
5658
}
5759

60+
public function testChecksAgainstExpressionPattern()
61+
{
62+
$i = $this->createInstance();
63+
$i->configure(['regex', ['get', 'name'], ['value', '/^a+$/']]);
64+
$this->assertFalse($i->evaluate(ArrayValueParser::from(['name' => 'value'])));
65+
}
66+
67+
public function testThrowErrorIfNameIsNotString()
68+
{
69+
$i = $this->createInstance();
70+
$this->expectException(InvalidEvaluationResultType::class);
71+
$i->configure(['regex', ['value', false], '/^a+$/']);
72+
$this->assertFalse($i->evaluate(ArrayValueParser::from(['name' => 'value'])));
73+
}
74+
75+
public function testThrowErrorIfPatternIsNotString()
76+
{
77+
$i = $this->createInstance();
78+
$this->expectException(InvalidEvaluationResultType::class);
79+
$i->configure(['regex', ['value', 'test'], ['value', false]]);
80+
$this->assertFalse($i->evaluate(ArrayValueParser::from(['name' => 'value'])));
81+
}
82+
5883

5984
public function testUseDefaultValue()
6085
{
@@ -68,6 +93,7 @@ protected function createInstance(): RegexOperatorSpy
6893
$parser = new ExpressionParser();
6994
$parser->setType('mock', MockOperator::class);
7095
$parser->setType('get', GetOperator::class);
96+
$parser->setType('value', ValueOperator::class);
7197
$operator = new RegexOperatorSpy();
7298
$operator->setParser($parser);
7399

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* @author Aleksandar Panic
4+
* @license http://www.apache.org/licenses/LICENSE-2.0
5+
* @since 1.0.0
6+
**/
7+
8+
namespace tests\Operators;
9+
10+
11+
use ArekX\ArrayExpression\ExpressionParser;
12+
use ArekX\ArrayExpression\ValueParsers\SingleValueParser;
13+
use tests\Mocks\MockOperator;
14+
use tests\Spies\ValueOperatorSpy;
15+
use tests\TestCase;
16+
17+
/**
18+
* Class ValueOperatorTest
19+
* @package tests\Operators
20+
*
21+
*/
22+
class ValueOperatorTest extends TestCase
23+
{
24+
public function testParserIsSet()
25+
{
26+
$i = $this->createInstance();
27+
$parser = new ExpressionParser();
28+
$i->setParser($parser);
29+
$this->assertSame($parser, $i->getParser());
30+
}
31+
32+
public function testNameIsSet()
33+
{
34+
$i = $this->createInstance();
35+
$value = rand(1, 50000);
36+
$i->configure(['value', $value]);
37+
$this->assertSame('value', $i->getName());
38+
$i->configure([1 => 'value', 2 => $value]);
39+
$this->assertSame('unknown', $i->getName());
40+
}
41+
42+
public function testAlwaysReturnAValue()
43+
{
44+
$i = $this->createInstance();
45+
$value = rand(1, 50000);
46+
$i->configure(['value', $value]);
47+
$this->assertSame($value, $i->evaluate(SingleValueParser::from('')));
48+
}
49+
50+
public function testThrowsErrorIfNotValidSyntax()
51+
{
52+
$i = $this->createInstance();
53+
$this->expectException(\InvalidArgumentException::class);
54+
$i->configure(['value']);
55+
}
56+
57+
protected function createInstance(): ValueOperatorSpy
58+
{
59+
$parser = new ExpressionParser();
60+
$parser->setType('mock', MockOperator::class);
61+
$operator = new ValueOperatorSpy();
62+
$operator->setParser($parser);
63+
64+
return $operator;
65+
}
66+
}

0 commit comments

Comments
 (0)