Skip to content

Commit

Permalink
Merge pull request #73 from blazarecki/allow-null
Browse files Browse the repository at this point in the history
Allow to use null with matcher
  • Loading branch information
Norbert Orzechowicz committed Mar 3, 2016
2 parents 914c184 + 5692d38 commit 5fc8221
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function __construct(ValueMatcher $propertyMatcher, Parser $parser)
*/
public function match($value, $pattern)
{
if (parent::match($value, $pattern)) {
return true;
}

if (!is_array($value)) {
$this->error = sprintf("%s \"%s\" is not a valid array.", gettype($value), new StringConverter($value));
return false;
Expand Down
4 changes: 4 additions & 0 deletions src/Matcher/JsonMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function __construct(ValueMatcher $matcher)
*/
public function match($value, $pattern)
{
if (parent::match($value, $pattern)) {
return true;
}

if (!Json::isValid($value)) {
return false;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function match($value, $pattern)
{
if ($value === $pattern) {
return true;
}
}
}
2 changes: 1 addition & 1 deletion src/Matcher/NullMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function match($value, $pattern)
*/
public function canMatch($pattern)
{
return is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern);
return $pattern === null || (is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern));
}
}
4 changes: 4 additions & 0 deletions src/Matcher/XmlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function __construct(ValueMatcher $matcher)
*/
public function match($value, $pattern)
{
if (parent::match($value, $pattern)) {
return true;
}

if (!Xml::isValid($value) || !Xml::isValid($pattern)) {
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Matcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function test_negative_match_when_cant_find_matcher_that_can_match_array_
$parser = new Parser(new Lexer(), new Parser\ExpanderInitializer())
);

$this->assertFalse($matcher->match(array('test' => 1), array('test' => 1)));
$this->assertTrue($matcher->match(array('test' => 1), array('test' => 1)));
}

public function test_error_when_path_in_pattern_does_not_exist()
Expand Down Expand Up @@ -159,6 +159,8 @@ public static function positiveMatchData()
array($simpleArr, $simpleArr),
array($simpleArr, $simpleArrPattern),
array(array(), array()),
array(array('foo' => null), array('foo' => null)),
array(array('foo' => null), array('foo' => "@null@")),
array(array('key' => 'val'), array('key' => 'val')),
array(array(1), array(1)),
array(array('roles' => array('ROLE_ADMIN', 'ROLE_DEVELOPER')), array('roles' => '@wildcard@'))
Expand Down
8 changes: 8 additions & 0 deletions tests/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ public static function positiveMatches()
'{"null":[null]}',
'{"null":[@null@]}'
),
array(
'{"null":null}',
'{"null":@null@}'
),
array(
'{"null":null}',
'{"null":null}'
),
array(
'{"users":["Norbert","Michał",[]]}',
'{"users":["Norbert","@string@",@...@]}'
Expand Down
4 changes: 3 additions & 1 deletion tests/Matcher/NullMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ public function test_negative_match_description($value, $pattern, $error)
public static function positiveCanMatchData()
{
return array(
array("@null@")
array("@null@"),
array(null)
);
}

public static function positiveMatchData()
{
return array(
array(null, "@null@"),
array(null, null),
);
}

Expand Down

0 comments on commit 5fc8221

Please sign in to comment.