Skip to content

Commit 3bb08a8

Browse files
committedMay 29, 2014
Merge pull request #16 from norzechowicz/array-fix
Fixed nested arrays error messages
2 parents 3e320d6 + 3211910 commit 3bb08a8

File tree

3 files changed

+74
-17
lines changed

3 files changed

+74
-17
lines changed
 

‎src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php

+26-12
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ public function canMatch($pattern)
4949
/**
5050
* @param array $value
5151
* @param array $pattern
52+
* @param string $parentPath
5253
* @return bool
5354
*/
54-
private function iterateMatch(array $value, array $pattern)
55+
private function iterateMatch(array $value, array $pattern, $parentPath = "")
5556
{
5657
foreach ($value as $key => $element) {
5758
$path = sprintf("[%s]", $key);
5859

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

75-
if (false === $this->iterateMatch($element, $elementPattern)) {
76+
if (false === $this->iterateMatch($element, $elementPattern, $parentPath . $path)) {
7677
return false;
7778
}
7879
}
7980

80-
if(is_array($pattern)) {
81-
$notExistingKeys = array_diff_key($pattern, $value);
82-
83-
if (count($notExistingKeys) > 0) {
84-
$keyNames = array_keys($notExistingKeys);
85-
$this->error = sprintf('There is no element under path [%s] in value array.', $keyNames[0]);
86-
return false;
87-
}
88-
}
81+
return $this->checkIfPathsFromPatternExistInValue($value, $pattern, $parentPath);
8982
}
9083

9184
/**
@@ -122,4 +115,25 @@ private function getPropertyAccessor()
122115

123116
return $this->accessor;
124117
}
118+
119+
/**
120+
* @param array $value
121+
* @param array $pattern
122+
* @param $parentPath
123+
* @return bool
124+
*/
125+
private function checkIfPathsFromPatternExistInValue(array $value, array $pattern, $parentPath)
126+
{
127+
if (is_array($pattern)) {
128+
$notExistingKeys = array_diff_key($pattern, $value);
129+
130+
if (count($notExistingKeys) > 0) {
131+
$keyNames = array_keys($notExistingKeys);
132+
$this->error = sprintf('There is no element under path %s[%s] in value.', $parentPath, $keyNames[0]);
133+
return false;
134+
}
135+
}
136+
137+
return true;
138+
}
125139
}

‎tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,40 @@ public function test_negative_match_when_cant_find_matcher_that_can_match_array_
5050
$this->assertFalse($matcher->match(array('test' => 1), array('test' => 1)));
5151
}
5252

53-
public function test_error_when_path_does_not_exist()
53+
public function test_error_when_path_in_pattern_does_not_exist()
5454
{
5555
$this->assertFalse($this->matcher->match(array('foo' => 'foo value'), array('bar' => 'bar value')));
56-
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo] in pattern array.');
56+
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo] in pattern.');
57+
}
58+
59+
public function test_error_when_path_in_nested_pattern_does_not_exist()
60+
{
61+
$array = array('foo' => array('bar' => array('baz' => 'bar value')));
62+
$pattern = array('foo' => array('bar' => array('faz' => 'faz value')));
63+
64+
$this->assertFalse($this->matcher->match($array,$pattern));
65+
66+
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][baz] in pattern.');
5767
}
5868

5969
public function test_error_when_path_in_value_does_not_exist()
6070
{
61-
$this->assertFalse($this->matcher->match(array('foo' => 'foo'), array('foo' => 'foo', 'bar' => 'bar')));
62-
$this->assertEquals($this->matcher->getError(), 'There is no element under path [bar] in value array.');
71+
$array = array('foo' => 'foo');
72+
$pattern = array('foo' => 'foo', 'bar' => 'bar');
73+
74+
$this->assertFalse($this->matcher->match($array, $pattern));
75+
76+
$this->assertEquals($this->matcher->getError(), 'There is no element under path [bar] in value.');
77+
}
78+
79+
public function test_error_when_path_in_nested_value_does_not_exist()
80+
{
81+
$array = array('foo' => array('bar' => array()));
82+
$pattern = array('foo' => array('bar' => array('faz' => 'faz value')));
83+
84+
$this->assertFalse($this->matcher->match($array, $pattern));
85+
86+
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][faz] in value.');
6387
}
6488

6589
public function test_error_when_matching_fail()
@@ -142,5 +166,4 @@ public static function negativeMatchData()
142166
array(array(), array('foo' => 'bar'))
143167
);
144168
}
145-
146169
}

‎tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ public function test_error_when_matching_fail()
7979
$this->assertEquals($this->matcher->getError(), '"Michał" does not match "@boolean@".');
8080
}
8181

82+
public function test_error_when_path_in_nested_pattern_does_not_exist()
83+
{
84+
$value = json_encode(array('foo' => array('bar' => array('baz' => 'bar value'))));
85+
$pattern = json_encode(array('foo' => array('bar' => array('faz' => 'faz value'))));
86+
87+
$this->assertFalse($this->matcher->match($value, $pattern));
88+
89+
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][baz] in pattern.');
90+
}
91+
92+
public function test_error_when_path_in_nested_value_does_not_exist()
93+
{
94+
$value = json_encode(array('foo' => array('bar' => array())));
95+
$pattern = json_encode(array('foo' => array('bar' => array('faz' => 'faz value'))));
96+
97+
$this->assertFalse($this->matcher->match($value, $pattern));
98+
99+
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][faz] in value.');
100+
}
101+
82102
public static function positivePatterns()
83103
{
84104
return array(

0 commit comments

Comments
 (0)