Skip to content

Commit 5fc8221

Browse files
author
Norbert Orzechowicz
committed
Merge pull request #73 from blazarecki/allow-null
Allow to use null with matcher
2 parents 914c184 + 5692d38 commit 5fc8221

File tree

8 files changed

+37
-3
lines changed

8 files changed

+37
-3
lines changed

src/Matcher/ArrayMatcher.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function __construct(ValueMatcher $propertyMatcher, Parser $parser)
4040
*/
4141
public function match($value, $pattern)
4242
{
43+
if (parent::match($value, $pattern)) {
44+
return true;
45+
}
46+
4347
if (!is_array($value)) {
4448
$this->error = sprintf("%s \"%s\" is not a valid array.", gettype($value), new StringConverter($value));
4549
return false;

src/Matcher/JsonMatcher.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public function __construct(ValueMatcher $matcher)
2424
*/
2525
public function match($value, $pattern)
2626
{
27+
if (parent::match($value, $pattern)) {
28+
return true;
29+
}
30+
2731
if (!Json::isValid($value)) {
2832
return false;
2933
}

src/Matcher/Matcher.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@ public function getError()
1616
{
1717
return $this->error;
1818
}
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function match($value, $pattern)
24+
{
25+
if ($value === $pattern) {
26+
return true;
27+
}
28+
}
1929
}

src/Matcher/NullMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public function match($value, $pattern)
2626
*/
2727
public function canMatch($pattern)
2828
{
29-
return is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern);
29+
return $pattern === null || (is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern));
3030
}
3131
}

src/Matcher/XmlMatcher.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function __construct(ValueMatcher $matcher)
2525
*/
2626
public function match($value, $pattern)
2727
{
28+
if (parent::match($value, $pattern)) {
29+
return true;
30+
}
31+
2832
if (!Xml::isValid($value) || !Xml::isValid($pattern)) {
2933
return false;
3034
}

tests/Matcher/ArrayMatcherTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function test_negative_match_when_cant_find_matcher_that_can_match_array_
5858
$parser = new Parser(new Lexer(), new Parser\ExpanderInitializer())
5959
);
6060

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

6464
public function test_error_when_path_in_pattern_does_not_exist()
@@ -159,6 +159,8 @@ public static function positiveMatchData()
159159
array($simpleArr, $simpleArr),
160160
array($simpleArr, $simpleArrPattern),
161161
array(array(), array()),
162+
array(array('foo' => null), array('foo' => null)),
163+
array(array('foo' => null), array('foo' => "@null@")),
162164
array(array('key' => 'val'), array('key' => 'val')),
163165
array(array(1), array(1)),
164166
array(array('roles' => array('ROLE_ADMIN', 'ROLE_DEVELOPER')), array('roles' => '@wildcard@'))

tests/Matcher/JsonMatcherTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ public static function positiveMatches()
157157
'{"null":[null]}',
158158
'{"null":[@null@]}'
159159
),
160+
array(
161+
'{"null":null}',
162+
'{"null":@null@}'
163+
),
164+
array(
165+
'{"null":null}',
166+
'{"null":null}'
167+
),
160168
array(
161169
'{"users":["Norbert","Michał",[]]}',
162170
'{"users":["Norbert","@string@",@...@]}'

tests/Matcher/NullMatcherTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ public function test_negative_match_description($value, $pattern, $error)
5454
public static function positiveCanMatchData()
5555
{
5656
return array(
57-
array("@null@")
57+
array("@null@"),
58+
array(null)
5859
);
5960
}
6061

6162
public static function positiveMatchData()
6263
{
6364
return array(
6465
array(null, "@null@"),
66+
array(null, null),
6567
);
6668
}
6769

0 commit comments

Comments
 (0)