Skip to content

match global function #3

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
Apr 17, 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
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"license": "MIT",
"require": {
"php": ">=5.3.0",
"symfony/property-access": "~2.3"
"symfony/property-access": "~2.3",
"symfony/expression-language": "~2.4"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
Expand All @@ -14,7 +15,8 @@
"psr-0": {
"JsonMatcher": "src/",
"JsonMatcher\\Tests": "tests/"
}
},
"files": ["match.php"]
},
"config": {
"bin-dir": "bin"
Expand Down
49 changes: 49 additions & 0 deletions match.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use JsonMatcher\Matcher\ArrayMatcher;
use JsonMatcher\Matcher\ChainMatcher;
use JsonMatcher\Matcher\ExpressionMatcher;
use JsonMatcher\Matcher\JsonMatcher;
use JsonMatcher\Matcher\ScalarMatcher;
use JsonMatcher\Matcher\TypeMatcher;
use JsonMatcher\Matcher\WildcardMatcher;
use JsonMatcher\Matcher;

if (is_dir($vendor = __DIR__ . '/../vendor')) {
require_once($vendor . '/autoload.php');
} elseif (is_dir($vendor = __DIR__ . '/../../../vendor')) {
require_once($vendor . '/autoload.php');
} elseif (is_dir($vendor = __DIR__ . '/vendor')) {
require_once($vendor . '/autoload.php');
} else {
die(
'You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
}

if (!function_exists('match')) {
/**
* @param mixed $value
* @param mixed $pattern
* @return boolean
*/
function match($value, $pattern)
{
$scalarMatchers = new ChainMatcher(array(
new ExpressionMatcher(),
new TypeMatcher(),
new ScalarMatcher(),
new WildcardMatcher()
));
$arrayMatcher = new ArrayMatcher($scalarMatchers);
$matcher = new Matcher(new ChainMatcher(array(
$scalarMatchers,
$arrayMatcher,
new JsonMatcher($arrayMatcher)
)));

return $matcher->match($value, $pattern);
}
}
26 changes: 26 additions & 0 deletions src/JsonMatcher/Matcher/ExpressionMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace JsonMatcher\Matcher;

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

class ExpressionMatcher implements PropertyMatcher
{
/**
* {@inheritDoc}
*/
public function match($value, $pattern)
{
$language = new ExpressionLanguage();
preg_match("/^expr\((.*?)\)$/", $pattern, $matches);
return $language->evaluate($matches[1], array('value' => $value));
}

/**
* {@inheritDoc}
*/
public function canMatch($pattern)
{
return is_string($pattern) && 0 !== preg_match("/^expr\((.*?)\)$/", $pattern);
}
}
79 changes: 79 additions & 0 deletions tests/JsonMatcher/Matcher/ExpressionMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
namespace JsonMatcher\Tests\Matcher;

use JsonMatcher\Matcher\ExpressionMatcher;

class ExpressionMatcherTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider positiveCanMatchData
*/
public function test_positive_can_matches($pattern)
{
$matcher = new ExpressionMatcher();
$this->assertTrue($matcher->canMatch($pattern));
}

/**
* @dataProvider negativeCanMatchData
*/
public function test_negative_can_matches($pattern)
{
$matcher = new ExpressionMatcher();
$this->assertFalse($matcher->canMatch($pattern));
}

/**
* @dataProvider positiveMatchData
*/
public function test_positive_match($value, $pattern)
{
$matcher = new ExpressionMatcher();
$this->assertTrue($matcher->match($value, $pattern));
}

/**
* @dataProvider negativeMatchData
*/
public function test_negative_match($value, $pattern)
{
$matcher = new ExpressionMatcher();
$this->assertFalse($matcher->match($value, $pattern));
}

public static function positiveCanMatchData()
{
return array(
array("expr(1 > 2)"),
array("expr(value == 'foo')"),
);
}

public static function negativeCanMatchData()
{
return array(
array("@integer"),
array("expr("),
array("@string"),
array(new \stdClass),
array(array("foobar"))
);
}

public static function positiveMatchData()
{
return array(
array(4, "expr(value > 2)"),
array("foo", "expr(value == 'foo')"),
array(new \DateTime('2014-04-01'), "expr(value.format('Y-m-d') == '2014-04-01')")
);
}

public static function negativeMatchData()
{
return array(
array(4, "expr(value < 2)"),
array("foo", "expr(value != 'foo')"),
);
}
}
89 changes: 88 additions & 1 deletion tests/JsonMatcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use JsonMatcher\Matcher\ArrayMatcher;
use JsonMatcher\Matcher\ChainMatcher;
use JsonMatcher\Matcher\ExpressionMatcher;
use JsonMatcher\Matcher\JsonMatcher;
use JsonMatcher\Matcher\ScalarMatcher;
use JsonMatcher\Matcher\TypeMatcher;
use JsonMatcher\Matcher\WildcardMatcher;
Expand All @@ -17,13 +19,18 @@ class MatcherTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$scalarMatchers = new ChainMatcher(array(
new ExpressionMatcher(),
new TypeMatcher(),
new ScalarMatcher(),
new WildcardMatcher()
));

$arrayMatcher = new ArrayMatcher($scalarMatchers);

$this->matcher = new Matcher(new ChainMatcher(array(
$scalarMatchers,
new ArrayMatcher($scalarMatchers)
$arrayMatcher,
new JsonMatcher($arrayMatcher)
)));

$this->arrayValue = array(
Expand Down Expand Up @@ -69,6 +76,28 @@ public function test_matcher_with_array_value()
'data' => '@wildcard@',
)
));

$this->assertTrue(match(
$this->arrayValue,
array(
'users' => array(
array(
'id' => '@integer@',
'firstName' => '@string@',
'lastName' => 'Orzechowicz',
'enabled' => '@boolean@'
),
array(
'id' => '@integer@',
'firstName' => '@string@',
'lastName' => 'Dąbrowski',
'enabled' => '@boolean@',
)
),
'readyToUse' => true,
'data' => '@wildcard@',
)
));
}

public function test_matcher_with_scalar_values()
Expand All @@ -77,9 +106,67 @@ public function test_matcher_with_scalar_values()
'Norbert Orzechowicz',
'@string@'
));
$this->assertTrue(match(
'Norbert Orzechowicz',
'@string@'
));
$this->assertTrue($this->matcher->match(
6.66,
'@double@'
));
$this->assertTrue(match(
6.66,
'@double@'
));
}

public function test_matcher_with_json()
{
$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(match($json, $jsonPattern));
}
}