Skip to content

Commit d7fdd51

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

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

src/Matcher/ArrayMatcher.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
use Coduo\ToString\StringConverter;
77
use Symfony\Component\PropertyAccess\PropertyAccess;
88
use Symfony\Component\PropertyAccess\PropertyAccessor;
9+
use Symfony\Component\PropertyAccess\PropertyPath;
910

1011
final class ArrayMatcher extends Matcher
1112
{
1213
const UNBOUNDED_PATTERN = '@...@';
1314

1415
/**
15-
* @var PropertyMatcher
16+
* @var ValueMatcher
1617
*/
1718
private $propertyMatcher;
1819

@@ -147,7 +148,7 @@ private function isPatternValid(array $pattern, array $values, $parentPath)
147148

148149
if (count($notExistingKeys) > 0) {
149150
$keyNames = array_keys($notExistingKeys);
150-
$path = $this->formatFullPath($parentPath, $this->formatAccessPath($keyNames[0]));
151+
$path = $this->formatFullPath($parentPath, $this->formatAccessPath($keyNames[0]));
151152
$this->setMissingElementInError('value', $path);
152153
return false;
153154
}
@@ -180,7 +181,33 @@ private function valueMatchPattern($value, $pattern)
180181
*/
181182
private function valueExist($path, array $haystack)
182183
{
183-
return null !== $this->getPropertyAccessor()->getValue($haystack, $path);
184+
$propertyPath = new PropertyPath($path);
185+
$length = $propertyPath->getLength();
186+
$valueExist = true;
187+
for ($i = 0; $i < $length; ++$i) {
188+
$property = $propertyPath->getElement($i);
189+
$isIndex = $propertyPath->isIndex($i);
190+
$propertyExist = $this->arrayPropertyExists($property, $haystack);
191+
192+
if ($isIndex && !$propertyExist) {
193+
$valueExist = false;
194+
break;
195+
}
196+
}
197+
198+
unset($propertyPath);
199+
return $valueExist;
200+
}
201+
202+
/**
203+
* @param string $property
204+
* @param array $objectOrArray
205+
* @return bool
206+
*/
207+
private function arrayPropertyExists($property, array $objectOrArray)
208+
{
209+
return ($objectOrArray instanceof \ArrayAccess && isset($objectOrArray[$property])) ||
210+
(is_array($objectOrArray) && array_key_exists($property, $objectOrArray));
184211
}
185212

186213
/**

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 $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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 5 additions & 1 deletion
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)