-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
324 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.