diff --git a/README.md b/README.md index e93a475d..812bc8be 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ $matcher->getError(); // returns null or error message * ``@null@`` * ``@*@`` || ``@wildcard@`` * ``expr(expression)`` +* ``@uuid@`` ### Available pattern expanders @@ -204,6 +205,19 @@ $matcher->match(new \DateTime('2014-04-01'), "expr(value.format('Y-m-d') == '201 $matcher->match("Norbert", "expr(value === 'Norbert')"); ``` +### UUID matching + +```php +createMatcher(); + +$matcher->match('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@'); +``` + ### Array matching ```php diff --git a/src/Factory/SimpleFactory.php b/src/Factory/SimpleFactory.php index cc097610..54cdfc63 100644 --- a/src/Factory/SimpleFactory.php +++ b/src/Factory/SimpleFactory.php @@ -76,7 +76,8 @@ protected function buildScalarMatchers() new Matcher\DoubleMatcher($parser), new Matcher\NumberMatcher(), new Matcher\ScalarMatcher(), - new Matcher\WildcardMatcher() + new Matcher\WildcardMatcher(), + new Matcher\UuidMatcher(), )); } diff --git a/src/Matcher/UuidMatcher.php b/src/Matcher/UuidMatcher.php new file mode 100644 index 00000000..2332659f --- /dev/null +++ b/src/Matcher/UuidMatcher.php @@ -0,0 +1,45 @@ +error = sprintf( + "%s \"%s\" is not a valid UUID: not a string.", + gettype($value), + new StringConverter($value) + ); + return false; + } + + if (1 !== preg_match(self::UUID_FORMAT_PATTERN, $value)) { + $this->error = sprintf( + "%s \"%s\" is not a valid UUID: invalid format.", + gettype($value), + $value + ); + return false; + } + + return true; + } + + /** + * {@inheritDoc} + */ + public function canMatch($pattern) + { + return is_string($pattern) && 0 !== preg_match(self::UUID_PATTERN, $pattern); + } +} diff --git a/tests/Matcher/UuidMatcherTest.php b/tests/Matcher/UuidMatcherTest.php new file mode 100644 index 00000000..cb6a7057 --- /dev/null +++ b/tests/Matcher/UuidMatcherTest.php @@ -0,0 +1,108 @@ +matcher = new UuidMatcher(); + } + /** + * @dataProvider positiveCanMatchData + */ + public function test_positive_can_matches($pattern) + { + $this->assertTrue($this->matcher->canMatch($pattern)); + } + + /** + * @dataProvider negativeCanMatchData + */ + public function test_negative_can_matches($pattern) + { + $this->assertFalse($this->matcher->canMatch($pattern)); + } + + /** + * @dataProvider positiveMatchData + */ + public function test_positive_match($value, $pattern) + { + $this->assertTrue($this->matcher->match($value, $pattern)); + } + + /** + * @dataProvider negativeMatchData + */ + public function test_negative_match($value, $pattern) + { + $this->assertFalse($this->matcher->match($value, $pattern)); + } + + /** + * @dataProvider negativeMatchDescription + */ + public function test_negative_match_description($value, $pattern, $error) + { + $this->matcher->match($value, $pattern); + $this->assertEquals($error, $this->matcher->getError()); + } + + public static function positiveCanMatchData() + { + return array( + array("@uuid@"), + ); + } + + public static function positiveMatchData() + { + return array( + array("9f4db639-0e87-4367-9beb-d64e3f42ae18", "@uuid@"), + ); + } + + public static function negativeCanMatchData() + { + return array( + array("@uuid"), + array("uuid"), + array(1), + ); + } + + public static function negativeMatchData() + { + return array( + array(1, "@uuid@"), + array(0, "@uuid@"), + array("9f4d-b639-0e87-4367-9beb-d64e3f42ae18", "@uuid@"), + array("9f4db639-0e87-4367-9beb-d64e3f42ae1", "@uuid@"), + array("9f4db639-0e87-4367-9beb-d64e3f42ae181", "@uuid@"), + array("9f4db6390e8743679bebd64e3f42ae18", "@uuid@"), + array("9f4db6390e87-4367-9beb-d64e-3f42ae18", "@uuid@"), + array("9f4db639-0e87-4367-9beb-d64e3f42ae1g", "@uuid@"), + array("9f4db639-0e87-0367-9beb-d64e3f42ae18", "@uuid@"), + ); + } + + public static function negativeMatchDescription() + { + return array( + array(new \stdClass, "@uuid@", "object \"\\stdClass\" is not a valid UUID: not a string."), + array(1.1, "@uuid@", "double \"1.1\" is not a valid UUID: not a string."), + array(false, "@uuid@", "boolean \"false\" is not a valid UUID: not a string."), + array(1, "@uuid@", "integer \"1\" is not a valid UUID: not a string."), + array("lorem ipsum", "@uuid@", "string \"lorem ipsum\" is not a valid UUID: invalid format."), + array("9f4db639-0e87-4367-9beb-d64e3f42ae1z", "@uuid@", "string \"9f4db639-0e87-4367-9beb-d64e3f42ae1z\" is not a valid UUID: invalid format."), + ); + } +} diff --git a/tests/MatcherTest.php b/tests/MatcherTest.php index d550cf6d..dc7db940 100644 --- a/tests/MatcherTest.php +++ b/tests/MatcherTest.php @@ -167,7 +167,6 @@ public function test_text_matcher() $this->assertTrue(PHPMatcher::match($value, $pattern)); } - public function test_error_when_json_value_does_not_match_json_pattern() { $pattern = '{"a": @null@, "b": 4}'; @@ -220,7 +219,8 @@ public function scalarValueExamples() array('Norbert Orzechowicz', '@string@'), array(6.66, '@double@'), array(1, '@integer@'), - array(array('foo'), '@array@') + array(array('foo'), '@array@'), + array('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@'), ); }