Skip to content

Added count pattern expander #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2016
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
54 changes: 54 additions & 0 deletions src/Matcher/Pattern/Expander/Count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
use Coduo\ToString\StringConverter;

final class Count implements PatternExpander
{
/**
* @var null|string
*/
private $error;

/**
* @var
*/
private $value;

/**
* @param $value
*/
public function __construct($value)
{
$this->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;
}
}
1 change: 1 addition & 0 deletions src/Parser/ExpanderInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down
44 changes: 44 additions & 0 deletions tests/Matcher/Pattern/Expander/CountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Matcher\Pattern\Expander\Count;

class CountTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider examplesProvider
*/
public function test_matching_values($needle, $haystack, $expectedResult)
{
$expander = new Count($needle);
$this->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\"."),
);
}
}
2 changes: 2 additions & 0 deletions tests/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ public static function expanderExamples()
array(array("foo", "bar"), "@[email protected](\"bar\")", true),
array(array(), "@[email protected]()", true),
array(array('foo'), "@[email protected]()", false),
array(array(1, 2, 3), "@[email protected](3)", true),
array(array(1, 2, 3), "@[email protected](4)", false),
array("lorem ipsum", "@[email protected](contains(\"lorem\"), contains(\"test\"))", true),
array("lorem ipsum", "@[email protected](contains(\"lorem\"), contains(\"test\")).endsWith(\"ipsum\")", true),
array("lorem ipsum", "@[email protected](\"/^lorem \\w+$/\")", true),
Expand Down