Skip to content

#71 Null value matching problem #75

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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