diff --git a/src/Matcher/Pattern/Expander/Count.php b/src/Matcher/Pattern/Expander/Count.php new file mode 100644 index 00000000..1698a417 --- /dev/null +++ b/src/Matcher/Pattern/Expander/Count.php @@ -0,0 +1,54 @@ +value = $value; + } + + /** + * @param $value + * @return boolean + */ + public function match($value) + { + if (!is_array($value)) { + $this->error = sprintf("Count expander require \"array\", got \"%s\".", new StringConverter($value)); + return false; + } + + if (count($value) !== $this->value) { + $this->error = sprintf("Expected count of %s is %s.", new StringConverter($value), new StringConverter($this->value)); + return false; + } + + return true; + } + + /** + * @return string|null + */ + public function getError() + { + return $this->error; + } +} diff --git a/src/Parser/ExpanderInitializer.php b/src/Parser/ExpanderInitializer.php index bdc4db49..9f21bcd7 100644 --- a/src/Parser/ExpanderInitializer.php +++ b/src/Parser/ExpanderInitializer.php @@ -25,6 +25,7 @@ final class ExpanderInitializer "lowerThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\LowerThan", "greaterThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\GreaterThan", "inArray" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\InArray", + "count" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Count", "contains" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Contains", "matchRegex" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\MatchRegex", diff --git a/tests/Matcher/Pattern/Expander/CountTest.php b/tests/Matcher/Pattern/Expander/CountTest.php new file mode 100644 index 00000000..cb99fd22 --- /dev/null +++ b/tests/Matcher/Pattern/Expander/CountTest.php @@ -0,0 +1,44 @@ +assertEquals($expectedResult, $expander->match($haystack)); + } + + public static function examplesProvider() + { + return array( + array(1, array("ipsum"), true), + array(2, array("foo", 1), true), + ); + } + + /** + * @dataProvider invalidCasesProvider + */ + public function test_error_when_matching_fail($boundary, $value, $errorMessage) + { + $expander = new Count($boundary); + $this->assertFalse($expander->match($value)); + $this->assertEquals($errorMessage, $expander->getError()); + } + + public static function invalidCasesProvider() + { + return array( + array(2, array(1, 2, 3), "Expected count of Array(3) is 2."), + array(2, new \DateTime(), "Count expander require \"array\", got \"\\DateTime\"."), + ); + } +} diff --git a/tests/MatcherTest.php b/tests/MatcherTest.php index 83ab40f2..d550cf6d 100644 --- a/tests/MatcherTest.php +++ b/tests/MatcherTest.php @@ -244,6 +244,8 @@ public static function expanderExamples() array(array("foo", "bar"), "@array@.inArray(\"bar\")", true), array(array(), "@array@.isEmpty()", true), array(array('foo'), "@array@.isEmpty()", false), + array(array(1, 2, 3), "@array@.count(3)", true), + array(array(1, 2, 3), "@array@.count(4)", false), array("lorem ipsum", "@string@.oneOf(contains(\"lorem\"), contains(\"test\"))", true), array("lorem ipsum", "@string@.oneOf(contains(\"lorem\"), contains(\"test\")).endsWith(\"ipsum\")", true), array("lorem ipsum", "@string@.matchRegex(\"/^lorem \\w+$/\")", true),