Skip to content

Allow @...@ to match none or more elements #97

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 2 commits into from
Jul 20, 2017
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
9 changes: 9 additions & 0 deletions src/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ private function iterateMatch(array $values, array $patterns, $parentPath = "")
private function isPatternValid(array $pattern, array $values, $parentPath)
{
if (is_array($pattern)) {
$skipPattern = static::UNBOUNDED_PATTERN;

$pattern = array_filter(
$pattern,
function ($item) use ($skipPattern) {
return $item !== $skipPattern;
}
);

$notExistingKeys = array_diff_key($pattern, $values);

if (count($notExistingKeys) > 0) {
Expand Down
39 changes: 36 additions & 3 deletions tests/Matcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static function positiveMatchData()
'firstName' => '@string@',
'lastName' => '@string@'
),
'@...@'
Matcher\ArrayMatcher::UNBOUNDED_PATTERN
),
true,
false,
Expand All @@ -163,7 +163,25 @@ public static function positiveMatchData()
array(array('foo' => null), array('foo' => "@null@")),
array(array('key' => 'val'), array('key' => 'val')),
array(array(1), array(1)),
array(array('roles' => array('ROLE_ADMIN', 'ROLE_DEVELOPER')), array('roles' => '@wildcard@'))
array(
array('roles' => array('ROLE_ADMIN', 'ROLE_DEVELOPER')),
array('roles' => '@wildcard@'),
),
'unbound array should match one or none elements' => array(
array(
'users' => array(
array(
'firstName' => 'Norbert',
'lastName' => 'Foobar',
),
),
true,
false,
1,
6.66,
),
$simpleArrPattern,
),
);
}

Expand Down Expand Up @@ -210,7 +228,22 @@ public static function negativeMatchData()
array(array('key' => 'val'), array('key' => 'val2')),
array(array(1), array(2)),
array(array('foo', 1, 3), array('foo', 2, 3)),
array(array(), array('foo' => 'bar'))
array(array(), array('foo' => 'bar')),
'unbound array should match one or none elements' => array(
array(
'users' => array(
array(
'firstName' => 'Norbert',
'lastName' => 'Foobar',
),
),
true,
false,
1,
6.66,
),
$simpleDiff,
),
);
}
}
156 changes: 106 additions & 50 deletions tests/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Parser;
use Coduo\PHPMatcher\PHPMatcher;

class MatcherTest extends \PHPUnit_Framework_TestCase
Expand All @@ -28,14 +27,14 @@ public function test_matcher_with_array_value()
'id' => 1,
'firstName' => 'Norbert',
'lastName' => 'Orzechowicz',
'enabled' => true
'enabled' => true,
),
array(
'id' => 2,
'firstName' => 'Michał',
'lastName' => 'Dąbrowski',
'enabled' => true,
)
),
),
'readyToUse' => true,
'data' => new \stdClass(),
Expand All @@ -47,14 +46,14 @@ public function test_matcher_with_array_value()
'id' => '@integer@',
'firstName' => '@string@',
'lastName' => 'Orzechowicz',
'enabled' => '@boolean@'
'enabled' => '@boolean@',
),
array(
'id' => '@integer@',
'firstName' => '@string@',
'lastName' => 'Dąbrowski',
'enabled' => '@boolean@',
)
),
),
'readyToUse' => true,
'data' => '@wildcard@',
Expand All @@ -73,55 +72,112 @@ public function test_matcher_with_scalar_values($value, $pattern)
$this->assertTrue(PHPMatcher::match($value, $pattern));
}

public function test_matcher_with_json()
/**
* @dataProvider jsonDataProvider
*/
public function test_matcher_with_json($json, $jsonPattern)
{
$json = '
{
"users":[
{
"id": 131,
"firstName": "Norbert",
"lastName": "Orzechowicz",
"enabled": true,
"roles": ["ROLE_DEVELOPER"]
},
{
"id": 132,
"firstName": "Michał",
"lastName": "Dąbrowski",
"enabled": false,
"roles": ["ROLE_DEVELOPER"]
}
],
"prevPage": "http:\/\/example.com\/api\/users\/1?limit=2",
"nextPage": "http:\/\/example.com\/api\/users\/3?limit=2"
}';
$jsonPattern = '
{
"users":[
{
"id": "@integer@",
"firstName":"Norbert",
"lastName":"Orzechowicz",
"enabled": "@boolean@",
"roles": "@array@"
},
{
"id": "@integer@",
"firstName": "Michał",
"lastName": "Dąbrowski",
"enabled": "expr(value == false)",
"roles": "@array@"
}
],
"prevPage": "@string@",
"nextPage": "@string@"
}';

$this->assertTrue($this->matcher->match($json, $jsonPattern));
$this->assertTrue(PHPMatcher::match($json, $jsonPattern));
}

public function jsonDataProvider()
{
return array(
'matches exactly' => array(
/** @lang JSON */
'{
"users":[
{
"id": 131,
"firstName": "Norbert",
"lastName": "Orzechowicz",
"enabled": true,
"roles": ["ROLE_DEVELOPER"]
},
{
"id": 132,
"firstName": "Michał",
"lastName": "Dąbrowski",
"enabled": false,
"roles": ["ROLE_DEVELOPER"]
}
],
"prevPage": "http:\/\/example.com\/api\/users\/1?limit=2",
"nextPage": "http:\/\/example.com\/api\/users\/3?limit=2"
}',
/** @lang JSON */
'{
"users":[
{
"id": "@integer@",
"firstName":"Norbert",
"lastName":"Orzechowicz",
"enabled": "@boolean@",
"roles": "@array@"
},
{
"id": "@integer@",
"firstName": "Michał",
"lastName": "Dąbrowski",
"enabled": "expr(value == false)",
"roles": "@array@"
}
],
"prevPage": "@string@",
"nextPage": "@string@"
}',
),
'matches none elements - empty array' => array(
/** @lang JSON */
'{
"users":[],
"prevPage": "http:\/\/example.com\/api\/users\/1?limit=2",
"nextPage": "http:\/\/example.com\/api\/users\/3?limit=2"
}',
/** @lang JSON */
'{
"users":[
"@...@"
],
"prevPage": "@string@",
"nextPage": "@string@"
}',
),
'matches one element' => array(
/** @lang JSON */
'{
"users":[
{
"id": 131,
"firstName": "Norbert",
"lastName": "Orzechowicz",
"enabled": true,
"roles": ["ROLE_DEVELOPER"]
}
],
"prevPage": "http:\/\/example.com\/api\/users\/1?limit=2",
"nextPage": "http:\/\/example.com\/api\/users\/3?limit=2"
}',
/** @lang JSON */
'{
"users":[
{
"id": "@integer@",
"firstName":"Norbert",
"lastName":"Orzechowicz",
"enabled": "@boolean@",
"roles": "@array@"
},
"@...@"
],
"prevPage": "@string@",
"nextPage": "@string@"
}',
),
);
}

public function test_matcher_with_xml()
{
$xml = <<<XML
Expand Down Expand Up @@ -284,7 +340,7 @@ public static function nullExamples()
'{"proformaInvoiceLink":null}', '{"proformaInvoiceLink":null}',
'{"proformaInvoiceLink":null, "test":"test"}', '{"proformaInvoiceLink":null, "test":"@string@"}',
'{"proformaInvoiceLink":null, "test":"test"}', '{"proformaInvoiceLink":@null@, "test":"@string@"}',
)
),
);
}
}