Skip to content

Possibility to use expanders for multiple matchers #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Factory/SimpleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ protected function buildScalarMatchers() : Matcher\ChainMatcher
new Matcher\NullMatcher(),
new Matcher\StringMatcher($parser),
new Matcher\IntegerMatcher($parser),
new Matcher\BooleanMatcher(),
new Matcher\BooleanMatcher($parser),
new Matcher\DoubleMatcher($parser),
new Matcher\NumberMatcher(),
new Matcher\NumberMatcher($parser),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher(),
new Matcher\UuidMatcher(),
new Matcher\UuidMatcher($parser),
]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

final class ArrayMatcher extends Matcher
{
const PATTERN = 'array';
const UNBOUNDED_PATTERN = '@...@';

private $propertyMatcher;
Expand Down Expand Up @@ -60,7 +61,7 @@ private function isArrayPattern($pattern) : bool
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is('array');
return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
}

private function iterateMatch(array $values, array $patterns, string $parentPath = "") : bool
Expand Down
16 changes: 14 additions & 2 deletions src/Matcher/BooleanMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace Coduo\PHPMatcher\Matcher;

use Coduo\PHPMatcher\Parser;
use Coduo\ToString\StringConverter;

final class BooleanMatcher extends Matcher
{
const BOOLEAN_PATTERN = '/^@boolean@$/';
const PATTERN = 'boolean';

private $parser;

public function __construct(Parser $parser)
{
$this->parser = $parser;
}

public function match($value, $pattern) : bool
{
Expand All @@ -22,6 +30,10 @@ public function match($value, $pattern) : bool

public function canMatch($pattern) : bool
{
return is_string($pattern) && 0 !== preg_match(self::BOOLEAN_PATTERN, $pattern);
if (!is_string($pattern)) {
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
}
}
4 changes: 3 additions & 1 deletion src/Matcher/DoubleMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

final class DoubleMatcher extends Matcher
{
const PATTERN = 'double';

private $parser;

public function __construct(Parser $parser)
Expand Down Expand Up @@ -38,6 +40,6 @@ public function canMatch($pattern) : bool
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is('double');
return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
}
}
4 changes: 3 additions & 1 deletion src/Matcher/IntegerMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

final class IntegerMatcher extends Matcher
{
const PATTERN = 'integer';

private $parser;

public function __construct(Parser $parser)
Expand Down Expand Up @@ -38,6 +40,6 @@ public function canMatch($pattern) : bool
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is('integer');
return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
}
}
6 changes: 6 additions & 0 deletions src/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ abstract class Matcher implements ValueMatcher
{
protected $error;

/**
* @inheritdoc
*/
public function getError()
{
return $this->error;
}

/**
* @inheritdoc
*/
public function match($value, $pattern) : bool
{
if ($value === $pattern) {
Expand Down
16 changes: 14 additions & 2 deletions src/Matcher/NumberMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace Coduo\PHPMatcher\Matcher;

use Coduo\PHPMatcher\Parser;
use Coduo\ToString\StringConverter;

final class NumberMatcher extends Matcher
{
const NUMBER_PATTERN = '/^@number@$/';
const PATTERN = 'number';

private $parser;

public function __construct(Parser $parser)
{
$this->parser = $parser;
}

public function match($value, $pattern) : bool
{
Expand All @@ -22,6 +30,10 @@ public function match($value, $pattern) : bool

public function canMatch($pattern) : bool
{
return is_string($pattern) && 0 !== preg_match(self::NUMBER_PATTERN, $pattern);
if (!is_string($pattern)) {
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
}
}
4 changes: 3 additions & 1 deletion src/Matcher/StringMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

final class StringMatcher extends Matcher
{
const PATTERN = 'string';

private $parser;

public function __construct(Parser $parser)
Expand Down Expand Up @@ -38,6 +40,6 @@ public function canMatch($pattern) : bool
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is('string');
return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
}
}
16 changes: 14 additions & 2 deletions src/Matcher/UuidMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@

namespace Coduo\PHPMatcher\Matcher;

use Coduo\PHPMatcher\Parser;
use Coduo\ToString\StringConverter;

final class UuidMatcher extends Matcher
{
const UUID_PATTERN = '/^@uuid@$/';
const PATTERN = 'uuid';
const UUID_FORMAT_PATTERN = '|^[\da-f]{8}-[\da-f]{4}-[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$|';

private $parser;

public function __construct(Parser $parser)
{
$this->parser = $parser;
}

public function match($value, $pattern) : bool
{
if (!is_string($value)) {
Expand All @@ -36,6 +44,10 @@ public function match($value, $pattern) : bool

public function canMatch($pattern) : bool
{
return is_string($pattern) && 0 !== preg_match(self::UUID_PATTERN, $pattern);
if (!is_string($pattern)) {
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
}
}
4 changes: 2 additions & 2 deletions tests/Matcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function setUp()
new Matcher\NullMatcher(),
new Matcher\StringMatcher($parser),
new Matcher\IntegerMatcher($parser),
new Matcher\BooleanMatcher(),
new Matcher\BooleanMatcher($parser),
new Matcher\DoubleMatcher($parser),
new Matcher\NumberMatcher(),
new Matcher\NumberMatcher($parser),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher(),
]),
Expand Down
29 changes: 18 additions & 11 deletions tests/Matcher/BooleanMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,62 @@

namespace Coduo\PHPMatcher\Tests\Matcher;

use Coduo\PHPMatcher\Lexer;
use Coduo\PHPMatcher\Parser;
use Coduo\PHPMatcher\Matcher\BooleanMatcher;
use PHPUnit\Framework\TestCase;

class BooleanMatcherTest extends TestCase
{
/**
* @var BooleanMatcher
*/
private $matcher;

public function setUp()
{
$this->matcher = new BooleanMatcher(new Parser(new Lexer(), new Parser\ExpanderInitializer()));
}

/**
* @dataProvider positiveCanMatchData
*/
public function test_positive_can_matches($pattern)
{
$matcher = new BooleanMatcher();
$this->assertTrue($matcher->canMatch($pattern));
$this->assertTrue($this->matcher->canMatch($pattern));
}

/**
* @dataProvider negativeCanMatchData
*/
public function test_negative_can_matches($pattern)
{
$matcher = new BooleanMatcher();
$this->assertFalse($matcher->canMatch($pattern));
$this->assertFalse($this->matcher->canMatch($pattern));
}

/**
* @dataProvider positiveMatchData
*/
public function test_positive_match($value, $pattern)
{
$matcher = new BooleanMatcher();
$this->assertTrue($matcher->match($value, $pattern));
$this->assertTrue($this->matcher->match($value, $pattern));
}

/**
* @dataProvider negativeMatchData
*/
public function test_negative_match($value, $pattern)
{
$matcher = new BooleanMatcher();
$this->assertFalse($matcher->match($value, $pattern));
$this->assertFalse($this->matcher->match($value, $pattern));
}

/**
* @dataProvider negativeMatchDescription
*/
public function test_negative_match_description($value, $pattern, $error)
{
$matcher = new BooleanMatcher();
$matcher->match($value, $pattern);
$this->assertEquals($error, $matcher->getError());
$this->matcher->match($value, $pattern);
$this->assertEquals($error, $this->matcher->getError());
}

public static function positiveCanMatchData()
Expand Down
4 changes: 2 additions & 2 deletions tests/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function setUp()
new Matcher\NullMatcher(),
new Matcher\StringMatcher($parser),
new Matcher\IntegerMatcher($parser),
new Matcher\BooleanMatcher(),
new Matcher\BooleanMatcher($parser),
new Matcher\DoubleMatcher($parser),
new Matcher\NumberMatcher(),
new Matcher\NumberMatcher($parser),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher(),
]);
Expand Down
39 changes: 23 additions & 16 deletions tests/Matcher/NullMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,62 @@

namespace Coduo\PHPMatcher\Tests\Matcher;

use Coduo\PHPMatcher\Lexer;
use Coduo\PHPMatcher\Parser;
use Coduo\PHPMatcher\Matcher\NullMatcher;
use PHPUnit\Framework\TestCase;

class NullMatcherTest extends TestCase
{
/**
* @var NullMatcher
*/
private $matcher;

public function setUp()
{
$this->matcher = new NullMatcher(new Parser(new Lexer(), new Parser\ExpanderInitializer()));
}

/**
* @dataProvider positiveCanMatchData
*/
public function test_positive_can_matches($pattern)
{
$matcher = new NullMatcher();
$this->assertTrue($matcher->canMatch($pattern));
$this->assertTrue($this->matcher->canMatch($pattern));
}

/**
* @dataProvider negativeCanMatchData
*/
public function test_negative_can_matches($pattern)
{
$matcher = new NullMatcher();
$this->assertFalse($matcher->canMatch($pattern));
$this->assertFalse($this->matcher->canMatch($pattern));
}

/**
* @dataProvider positiveMatchData
*/
public function test_positive_match($value, $pattern)
{
$matcher = new NullMatcher();
$this->assertTrue($matcher->match($value, $pattern));
$this->assertTrue($this->matcher->match($value, $pattern));
}

/**
* @dataProvider negativeMatchData
*/
public function test_negative_match($value, $pattern)
{
$matcher = new NullMatcher();
$this->assertFalse($matcher->match($value, $pattern));
$this->assertFalse($this->matcher->match($value, $pattern));
}

/**
* @dataProvider negativeMatchDescription
*/
public function test_negative_match_description($value, $pattern, $error)
{
$matcher = new NullMatcher();
$matcher->match($value, $pattern);
$this->assertEquals($error, $matcher->getError());
$this->matcher->match($value, $pattern);
$this->assertEquals($error, $this->matcher->getError());
}

public static function positiveCanMatchData()
Expand Down Expand Up @@ -91,11 +98,11 @@ public static function negativeMatchData()
public static function negativeMatchDescription()
{
return [
["test", "@boolean@", "string \"test\" does not match null."],
[new \stdClass, "@string@", "object \"\\stdClass\" does not match null."],
[1.1, "@integer@", "double \"1.1\" does not match null."],
[false, "@double@", "boolean \"false\" does not match null."],
[1, "@array@", "integer \"1\" does not match null."]
["test", "@null@", "string \"test\" does not match null."],
[new \stdClass, "@null@", "object \"\\stdClass\" does not match null."],
[1.1, "@null@", "double \"1.1\" does not match null."],
[false, "@null@", "boolean \"false\" does not match null."],
[1, "@null@", "integer \"1\" does not match null."]
];
}
}
Loading