Skip to content

Commit 9518da0

Browse files
author
Michal Szczur
committed
#71 Null value matching problem
1 parent 5fc8221 commit 9518da0

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

src/Matcher/ArrayMatcher.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class ArrayMatcher extends Matcher
1212
const UNBOUNDED_PATTERN = '@...@';
1313

1414
/**
15-
* @var PropertyMatcher
15+
* @var ValueMatcher
1616
*/
1717
private $propertyMatcher;
1818

@@ -180,7 +180,7 @@ private function valueMatchPattern($value, $pattern)
180180
*/
181181
private function valueExist($path, array $haystack)
182182
{
183-
return null !== $this->getPropertyAccessor()->getValue($haystack, $path);
183+
return $this->getPropertyAccessor()->isReadable($haystack, $path);
184184
}
185185

186186
/**
@@ -203,6 +203,7 @@ private function getPropertyAccessor()
203203
}
204204

205205
$accessorBuilder = PropertyAccess::createPropertyAccessorBuilder();
206+
$accessorBuilder->enableExceptionOnInvalidIndex();
206207
$this->accessor = $accessorBuilder->getPropertyAccessor();
207208

208209
return $this->accessor;

src/Matcher/NullMatcher.php

+1-1
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 $pattern === null || (is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern));
29+
return is_null($pattern) || (is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern));
3030
}
3131
}

tests/Matcher/ArrayMatcherTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function test_error_when_path_in_nested_pattern_does_not_exist()
7272
$array = array('foo' => array('bar' => array('baz' => 'bar value')));
7373
$pattern = array('foo' => array('bar' => array('faz' => 'faz value')));
7474

75-
$this->assertFalse($this->matcher->match($array,$pattern));
75+
$this->assertFalse($this->matcher->match($array, $pattern));
7676

7777
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][baz] in pattern.');
7878
}
@@ -124,7 +124,7 @@ public function test_matching_array_to_array_pattern()
124124

125125
public static function positiveMatchData()
126126
{
127-
$simpleArr = array(
127+
$simpleArr = array(
128128
'users' => array(
129129
array(
130130
'firstName' => 'Norbert',
@@ -141,7 +141,7 @@ public static function positiveMatchData()
141141
6.66
142142
);
143143

144-
$simpleArrPattern = array(
144+
$simpleArrPattern = array(
145145
'users' => array(
146146
array(
147147
'firstName' => '@string@',
@@ -169,7 +169,7 @@ public static function positiveMatchData()
169169

170170
public static function negativeMatchData()
171171
{
172-
$simpleArr = array(
172+
$simpleArr = array(
173173
'users' => array(
174174
array(
175175
'firstName' => 'Norbert',
@@ -186,7 +186,7 @@ public static function negativeMatchData()
186186
6.66
187187
);
188188

189-
$simpleDiff = array(
189+
$simpleDiff = array(
190190
'users' => array(
191191
array(
192192
'firstName' => 'Norbert',

tests/Matcher/JsonMatcherTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ public static function positiveMatches()
161161
'{"null":null}',
162162
'{"null":@null@}'
163163
),
164+
array(
165+
'{"username":null,"some_data":"test"}',
166+
'{"username":null, "some_data": @string@}'
167+
),
164168
array(
165169
'{"null":null}',
166170
'{"null":null}'
@@ -176,7 +180,7 @@ public static function positiveMatches()
176180
array(
177181
'[{"name": "Norbert"},{"name":"Michał"},{"name":"Bob"},{"name":"Martin"}]',
178182
'[{"name": "Norbert"},@...@]'
179-
),
183+
)
180184
);
181185
}
182186

0 commit comments

Comments
 (0)