Skip to content

Commit 3e320d6

Browse files
committed
Merge pull request #15 from pskt/array-matching-fix
Fixed array matching when key does not exist in value array
2 parents e2de5f3 + ebfc0e4 commit 3e320d6

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ private function iterateMatch(array $value, array $pattern)
7676
return false;
7777
}
7878
}
79+
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+
}
7989
}
8090

8191
/**

tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ public function test_error_when_path_does_not_exist()
5656
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo] in pattern array.');
5757
}
5858

59+
public function test_error_when_path_in_value_does_not_exist()
60+
{
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.');
63+
}
64+
5965
public function test_error_when_matching_fail()
6066
{
6167
$this->assertFalse($this->matcher->match(array('foo' => 'foo value'), array('foo' => 'bar value')));
@@ -128,10 +134,12 @@ public static function negativeMatchData()
128134

129135
return array(
130136
array($simpleArr, $simpleDiff),
137+
array(array("status" => "ok", "data" => array(array('foo'))), array("status" => "ok", "data" => array())),
131138
array(array(1), array()),
132139
array(array('key' => 'val'), array('key' => 'val2')),
133140
array(array(1), array(2)),
134-
array(array('foo', 1, 3), array('foo', 2, 3))
141+
array(array('foo', 1, 3), array('foo', 2, 3)),
142+
array(array(), array('foo' => 'bar'))
135143
);
136144
}
137145

tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ public static function negativeMatches()
129129
'{this_is_not_valid_json',
130130
'{"users":["Michał","@string@"]}'
131131
),
132+
array(
133+
'{"status":"ok","data":[]}',
134+
'{"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"}]}'
135+
),
136+
array(
137+
'{"foo":"foo val","bar":"bar val"}',
138+
'{"foo":"foo val"}'
139+
),
132140
array(
133141
array(),
134142
'[]'

0 commit comments

Comments
 (0)