Skip to content

Commit

Permalink
Merge pull request #16 from norzechowicz/array-fix
Browse files Browse the repository at this point in the history
Fixed nested arrays error messages
  • Loading branch information
defrag committed May 29, 2014
2 parents 3e320d6 + 3211910 commit 3bb08a8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 17 deletions.
38 changes: 26 additions & 12 deletions src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ public function canMatch($pattern)
/**
* @param array $value
* @param array $pattern
* @param string $parentPath
* @return bool
*/
private function iterateMatch(array $value, array $pattern)
private function iterateMatch(array $value, array $pattern, $parentPath = "")
{
foreach ($value as $key => $element) {
$path = sprintf("[%s]", $key);

if (!$this->hasValue($pattern, $path)) {
$this->error = sprintf('There is no element under path %s in pattern array.', $path);
$this->error = sprintf('There is no element under path %s%s in pattern.', $parentPath, $path);
return false;
}
$elementPattern = $this->getValue($pattern, $path);
Expand All @@ -72,20 +73,12 @@ private function iterateMatch(array $value, array $pattern)
return false;
}

if (false === $this->iterateMatch($element, $elementPattern)) {
if (false === $this->iterateMatch($element, $elementPattern, $parentPath . $path)) {
return false;
}
}

if(is_array($pattern)) {
$notExistingKeys = array_diff_key($pattern, $value);

if (count($notExistingKeys) > 0) {
$keyNames = array_keys($notExistingKeys);
$this->error = sprintf('There is no element under path [%s] in value array.', $keyNames[0]);
return false;
}
}
return $this->checkIfPathsFromPatternExistInValue($value, $pattern, $parentPath);
}

/**
Expand Down Expand Up @@ -122,4 +115,25 @@ private function getPropertyAccessor()

return $this->accessor;
}

/**
* @param array $value
* @param array $pattern
* @param $parentPath
* @return bool
*/
private function checkIfPathsFromPatternExistInValue(array $value, array $pattern, $parentPath)
{
if (is_array($pattern)) {
$notExistingKeys = array_diff_key($pattern, $value);

if (count($notExistingKeys) > 0) {
$keyNames = array_keys($notExistingKeys);
$this->error = sprintf('There is no element under path %s[%s] in value.', $parentPath, $keyNames[0]);
return false;
}
}

return true;
}
}
33 changes: 28 additions & 5 deletions tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,40 @@ public function test_negative_match_when_cant_find_matcher_that_can_match_array_
$this->assertFalse($matcher->match(array('test' => 1), array('test' => 1)));
}

public function test_error_when_path_does_not_exist()
public function test_error_when_path_in_pattern_does_not_exist()
{
$this->assertFalse($this->matcher->match(array('foo' => 'foo value'), array('bar' => 'bar value')));
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo] in pattern array.');
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo] in pattern.');
}

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->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][baz] in pattern.');
}

public function test_error_when_path_in_value_does_not_exist()
{
$this->assertFalse($this->matcher->match(array('foo' => 'foo'), array('foo' => 'foo', 'bar' => 'bar')));
$this->assertEquals($this->matcher->getError(), 'There is no element under path [bar] in value array.');
$array = array('foo' => 'foo');
$pattern = array('foo' => 'foo', 'bar' => 'bar');

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

$this->assertEquals($this->matcher->getError(), 'There is no element under path [bar] in value.');
}

public function test_error_when_path_in_nested_value_does_not_exist()
{
$array = array('foo' => array('bar' => array()));
$pattern = array('foo' => array('bar' => array('faz' => 'faz value')));

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

$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][faz] in value.');
}

public function test_error_when_matching_fail()
Expand Down Expand Up @@ -142,5 +166,4 @@ public static function negativeMatchData()
array(array(), array('foo' => 'bar'))
);
}

}
20 changes: 20 additions & 0 deletions tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ public function test_error_when_matching_fail()
$this->assertEquals($this->matcher->getError(), '"Michał" does not match "@boolean@".');
}

public function test_error_when_path_in_nested_pattern_does_not_exist()
{
$value = json_encode(array('foo' => array('bar' => array('baz' => 'bar value'))));
$pattern = json_encode(array('foo' => array('bar' => array('faz' => 'faz value'))));

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

$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][baz] in pattern.');
}

public function test_error_when_path_in_nested_value_does_not_exist()
{
$value = json_encode(array('foo' => array('bar' => array())));
$pattern = json_encode(array('foo' => array('bar' => array('faz' => 'faz value'))));

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

$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][faz] in value.');
}

public static function positivePatterns()
{
return array(
Expand Down

0 comments on commit 3bb08a8

Please sign in to comment.