diff --git a/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php b/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php index edc6cdec..82eacb66 100644 --- a/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php +++ b/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php @@ -76,6 +76,16 @@ private function iterateMatch(array $value, array $pattern) 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; + } + } } /** diff --git a/tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php b/tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php index 4074baa3..ef48e0ea 100644 --- a/tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php +++ b/tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php @@ -56,6 +56,12 @@ public function test_error_when_path_does_not_exist() $this->assertEquals($this->matcher->getError(), 'There is no element under path [foo] in pattern array.'); } + 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.'); + } + public function test_error_when_matching_fail() { $this->assertFalse($this->matcher->match(array('foo' => 'foo value'), array('foo' => 'bar value'))); @@ -128,10 +134,12 @@ public static function negativeMatchData() return array( array($simpleArr, $simpleDiff), + array(array("status" => "ok", "data" => array(array('foo'))), array("status" => "ok", "data" => array())), array(array(1), array()), array(array('key' => 'val'), array('key' => 'val2')), array(array(1), array(2)), - array(array('foo', 1, 3), array('foo', 2, 3)) + array(array('foo', 1, 3), array('foo', 2, 3)), + array(array(), array('foo' => 'bar')) ); } diff --git a/tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php b/tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php index bbd92151..fe36dbb7 100644 --- a/tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php +++ b/tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php @@ -129,6 +129,14 @@ public static function negativeMatches() '{this_is_not_valid_json', '{"users":["MichaƂ","@string@"]}' ), + array( + '{"status":"ok","data":[]}', + '{"status":"ok","data":[{"id": 4,"code":"123987","name":"Anvill","short_description":"ACME Anvill","url":"http://test-store.example.com/p/123987","image":{"url":"http://test-store.example.com/i/123987-0.jpg","description":"ACME Anvill"},"price":95,"promotion_description":"Anvills sale"},{"id": 5,"code":"123988","name":"Red Anvill","short_description":"Red ACME Anvill","url":"http://test-store.example.com/p/123988","image":{"url":"http://test-store.example.com/i/123988-0.jpg","description":"ACME Anvill"},"price":44.99,"promotion_description":"Red is cheap"}]}' + ), + array( + '{"foo":"foo val","bar":"bar val"}', + '{"foo":"foo val"}' + ), array( array(), '[]'