Skip to content

Fixed array matching when key does not exist in value array #15

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
May 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some tests for nested arrays?

);
}

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