From 12aacecb80f5a3d08c3c21c3671fc0a6d4993b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Skotnicki?= Date: Thu, 29 May 2014 11:02:42 +0200 Subject: [PATCH 1/3] Fixed array matching when key does not exist in value array --- src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php | 10 ++++++++++ tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php | 9 ++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php b/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php index edc6cdec..cf03e15d 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)) { + $unexistingKeys = array_diff_key($pattern, $value); + + if (count($unexistingKeys) > 0) { + $keyNames = array_keys($unexistingKeys); + $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..bfc5d6ab 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'))); @@ -131,7 +137,8 @@ public static function negativeMatchData() 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')) ); } From d83a6fdd84ddfbbecdb0e35f943cf2fb47b00316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Skotnicki?= Date: Thu, 29 May 2014 11:40:32 +0200 Subject: [PATCH 2/3] Added test cases --- tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php | 1 + tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php b/tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php index bfc5d6ab..ef48e0ea 100644 --- a/tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php +++ b/tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php @@ -134,6 +134,7 @@ 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)), 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(), '[]' From ebfc0e4f665cc4f0cab21ab82d01def774b6941d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Skotnicki?= Date: Thu, 29 May 2014 11:41:22 +0200 Subject: [PATCH 3/3] Fixed $notExistingKeys value name --- src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php b/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php index cf03e15d..82eacb66 100644 --- a/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php +++ b/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php @@ -78,10 +78,10 @@ private function iterateMatch(array $value, array $pattern) } if(is_array($pattern)) { - $unexistingKeys = array_diff_key($pattern, $value); + $notExistingKeys = array_diff_key($pattern, $value); - if (count($unexistingKeys) > 0) { - $keyNames = array_keys($unexistingKeys); + 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; }