Skip to content

Commit

Permalink
Merge pull request #75 from partikus/bugfix/nullable-value-matching
Browse files Browse the repository at this point in the history
#71 Null value matching problem
  • Loading branch information
Norbert Orzechowicz committed Mar 17, 2016
2 parents 5fc8221 + d7fdd51 commit 87ff316
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
33 changes: 30 additions & 3 deletions src/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
use Coduo\ToString\StringConverter;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\PropertyPath;

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

/**
* @var PropertyMatcher
* @var ValueMatcher
*/
private $propertyMatcher;

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

if (count($notExistingKeys) > 0) {
$keyNames = array_keys($notExistingKeys);
$path = $this->formatFullPath($parentPath, $this->formatAccessPath($keyNames[0]));
$path = $this->formatFullPath($parentPath, $this->formatAccessPath($keyNames[0]));
$this->setMissingElementInError('value', $path);
return false;
}
Expand Down Expand Up @@ -180,7 +181,33 @@ private function valueMatchPattern($value, $pattern)
*/
private function valueExist($path, array $haystack)
{
return null !== $this->getPropertyAccessor()->getValue($haystack, $path);
$propertyPath = new PropertyPath($path);
$length = $propertyPath->getLength();
$valueExist = true;
for ($i = 0; $i < $length; ++$i) {
$property = $propertyPath->getElement($i);
$isIndex = $propertyPath->isIndex($i);
$propertyExist = $this->arrayPropertyExists($property, $haystack);

if ($isIndex && !$propertyExist) {
$valueExist = false;
break;
}
}

unset($propertyPath);
return $valueExist;
}

/**
* @param string $property
* @param array $objectOrArray
* @return bool
*/
private function arrayPropertyExists($property, array $objectOrArray)
{
return ($objectOrArray instanceof \ArrayAccess && isset($objectOrArray[$property])) ||
(is_array($objectOrArray) && array_key_exists($property, $objectOrArray));
}

/**
Expand Down
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 $pattern === null || (is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern));
return is_null($pattern) || (is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern));
}
}
10 changes: 5 additions & 5 deletions tests/Matcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function test_error_when_path_in_nested_pattern_does_not_exist()
$array = array('foo' => array('bar' => array('baz' => 'bar value')));
$pattern = array('foo' => array('bar' => array('faz' => 'faz value')));

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

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

public static function positiveMatchData()
{
$simpleArr = array(
$simpleArr = array(
'users' => array(
array(
'firstName' => 'Norbert',
Expand All @@ -141,7 +141,7 @@ public static function positiveMatchData()
6.66
);

$simpleArrPattern = array(
$simpleArrPattern = array(
'users' => array(
array(
'firstName' => '@string@',
Expand Down Expand Up @@ -169,7 +169,7 @@ public static function positiveMatchData()

public static function negativeMatchData()
{
$simpleArr = array(
$simpleArr = array(
'users' => array(
array(
'firstName' => 'Norbert',
Expand All @@ -186,7 +186,7 @@ public static function negativeMatchData()
6.66
);

$simpleDiff = array(
$simpleDiff = array(
'users' => array(
array(
'firstName' => 'Norbert',
Expand Down
6 changes: 5 additions & 1 deletion tests/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public static function positiveMatches()
'{"null":null}',
'{"null":@null@}'
),
array(
'{"username":null,"some_data":"test"}',
'{"username":null, "some_data": @string@}'
),
array(
'{"null":null}',
'{"null":null}'
Expand All @@ -176,7 +180,7 @@ public static function positiveMatches()
array(
'[{"name": "Norbert"},{"name":"Michał"},{"name":"Bob"},{"name":"Martin"}]',
'[{"name": "Norbert"},@...@]'
),
)
);
}

Expand Down

0 comments on commit 87ff316

Please sign in to comment.