Skip to content

Commit

Permalink
Merge pull request #15 from pskt/array-matching-fix
Browse files Browse the repository at this point in the history
Fixed array matching when key does not exist in value array
  • Loading branch information
norberttech committed May 29, 2014
2 parents e2de5f3 + ebfc0e4 commit 3e320d6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

/**
Expand Down
10 changes: 9 additions & 1 deletion tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
Expand Down Expand Up @@ -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'))
);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
'[]'
Expand Down

0 comments on commit 3e320d6

Please sign in to comment.