From b920d07f9c628432a68bf5ca714780db5f3b060f Mon Sep 17 00:00:00 2001 From: Robin van der Vleuten Date: Thu, 19 Jun 2014 13:38:47 +0200 Subject: [PATCH] A null value can be matched. --- match.php | 2 + src/Coduo/PHPMatcher/Matcher/JsonMatcher.php | 2 +- src/Coduo/PHPMatcher/Matcher/NullMatcher.php | 31 ++++++ .../PHPMatcher/Matcher/JsonMatcherTest.php | 6 ++ .../PHPMatcher/Matcher/NullMatcherTest.php | 95 +++++++++++++++++++ 5 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/Coduo/PHPMatcher/Matcher/NullMatcher.php create mode 100644 tests/Coduo/PHPMatcher/Matcher/NullMatcherTest.php diff --git a/match.php b/match.php index c74df251..a77bb73e 100644 --- a/match.php +++ b/match.php @@ -5,6 +5,7 @@ use Coduo\PHPMatcher\Matcher\ChainMatcher; use Coduo\PHPMatcher\Matcher\ExpressionMatcher; use Coduo\PHPMatcher\Matcher\JsonMatcher; +use Coduo\PHPMatcher\Matcher\NullMatcher; use Coduo\PHPMatcher\Matcher\ScalarMatcher; use Coduo\PHPMatcher\Matcher\TypeMatcher; use Coduo\PHPMatcher\Matcher\WildcardMatcher; @@ -36,6 +37,7 @@ function match($value, $pattern) new CallbackMatcher(), new ExpressionMatcher(), new TypeMatcher(), + new NullMatcher(), new ScalarMatcher(), new WildcardMatcher() )); diff --git a/src/Coduo/PHPMatcher/Matcher/JsonMatcher.php b/src/Coduo/PHPMatcher/Matcher/JsonMatcher.php index 0d8f3205..a8294843 100644 --- a/src/Coduo/PHPMatcher/Matcher/JsonMatcher.php +++ b/src/Coduo/PHPMatcher/Matcher/JsonMatcher.php @@ -4,7 +4,7 @@ class JsonMatcher extends Matcher { - const TRANSFORM_QUOTATION_PATTERN = '/([^"])@(integer|string|array|double|wildcard|boolean)@([^"])/'; + const TRANSFORM_QUOTATION_PATTERN = '/([^"])@(integer|string|array|double|wildcard|boolean|null)@([^"])/'; const TRANSFORM_QUOTATION_REPLACEMENT = '$1"@$2@"$3'; /** diff --git a/src/Coduo/PHPMatcher/Matcher/NullMatcher.php b/src/Coduo/PHPMatcher/Matcher/NullMatcher.php new file mode 100644 index 00000000..293620a5 --- /dev/null +++ b/src/Coduo/PHPMatcher/Matcher/NullMatcher.php @@ -0,0 +1,31 @@ +error = sprintf("%s \"%s\" does not match null.", gettype($value), new String($value)); + return false; + } + + return true; + } + + /** + * {@inheritDoc} + */ + public function canMatch($pattern) + { + return is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern); + } +} diff --git a/tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php b/tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php index 53a03a7c..83e72077 100644 --- a/tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php +++ b/tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php @@ -4,6 +4,7 @@ use Coduo\PHPMatcher\Matcher\ArrayMatcher; use Coduo\PHPMatcher\Matcher\ChainMatcher; use Coduo\PHPMatcher\Matcher\JsonMatcher; +use Coduo\PHPMatcher\Matcher\NullMatcher; use Coduo\PHPMatcher\Matcher\ScalarMatcher; use Coduo\PHPMatcher\Matcher\TypeMatcher; use Coduo\PHPMatcher\Matcher\WildcardMatcher; @@ -20,6 +21,7 @@ public function setUp() $scalarMatchers = new ChainMatcher(array( new TypeMatcher(), new ScalarMatcher(), + new NullMatcher(), new WildcardMatcher() )); $this->matcher = new JsonMatcher(new ChainMatcher(array( @@ -131,6 +133,10 @@ public static function positiveMatches() '{"foobar":[1.22, 2, "hello"]}', '{"foobar":[@double@, @integer@, @string@]}' ), + array( + '{"null":[null]}', + '{"null":[@null@]}' + ), array( '{"users":[{"firstName":"Norbert","lastName":"Orzechowicz","roles":["ROLE_USER", "ROLE_DEVELOPER"]}]}', '{"users":[{"firstName":"Norbert","lastName":"Orzechowicz","roles":"@wildcard@"}]}' diff --git a/tests/Coduo/PHPMatcher/Matcher/NullMatcherTest.php b/tests/Coduo/PHPMatcher/Matcher/NullMatcherTest.php new file mode 100644 index 00000000..0397fd4d --- /dev/null +++ b/tests/Coduo/PHPMatcher/Matcher/NullMatcherTest.php @@ -0,0 +1,95 @@ +assertTrue($matcher->canMatch($pattern)); + } + + /** + * @dataProvider negativeCanMatchData + */ + public function test_negative_can_matches($pattern) + { + $matcher = new NullMatcher(); + $this->assertFalse($matcher->canMatch($pattern)); + } + + /** + * @dataProvider positiveMatchData + */ + public function test_positive_match($value, $pattern) + { + $matcher = new NullMatcher(); + $this->assertTrue($matcher->match($value, $pattern)); + } + + /** + * @dataProvider negativeMatchData + */ + public function test_negative_match($value, $pattern) + { + $matcher = new NullMatcher(); + $this->assertFalse($matcher->match($value, $pattern)); + } + + /** + * @dataProvider negativeMatchDescription + */ + public function test_negative_match_description($value, $pattern, $error) + { + $matcher = new NullMatcher(); + $matcher->match($value, $pattern); + $this->assertEquals($error, $matcher->getError()); + } + + public static function positiveCanMatchData() + { + return array( + array("@null@") + ); + } + + public static function positiveMatchData() + { + return array( + array(null, "@null@"), + ); + } + + public static function negativeCanMatchData() + { + return array( + array("@null"), + array("null"), + array(0) + ); + } + + public static function negativeMatchData() + { + return array( + array("null", "@null@"), + array(0, "@null@") + ); + } + + public static function negativeMatchDescription() + { + return array( + array("test", "@boolean@", "string \"test\" does not match null."), + array(new \stdClass, "@string@", "object \"\\stdClass\" does not match null."), + array(1.1, "@integer@", "double \"1.1\" does not match null."), + array(false, "@double@", "boolean \"false\" does not match null."), + array(1, "@array@", "integer \"1\" does not match null.") + ); + } +}