Skip to content
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

Added UUID matcher. #89

Merged
merged 1 commit into from
Oct 20, 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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ $matcher->getError(); // returns null or error message
* ``@null@``
* ``@*@`` || ``@wildcard@``
* ``expr(expression)``
* ``@uuid@``

### Available pattern expanders

Expand Down Expand Up @@ -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
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;

$factory = new SimpleFactory();
$matcher = $factory->createMatcher();

$matcher->match('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@');
```

### Array matching

```php
Expand Down
3 changes: 2 additions & 1 deletion src/Factory/SimpleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
}

Expand Down
45 changes: 45 additions & 0 deletions src/Matcher/UuidMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Coduo\PHPMatcher\Matcher;

use Coduo\ToString\StringConverter;

final class UuidMatcher extends Matcher
{
const UUID_PATTERN = '/^@uuid@$/';
const UUID_FORMAT_PATTERN = '|^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$|';

/**
* {@inheritDoc}
*/
public function match($value, $pattern)
{
if (!is_string($value)) {
$this->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);
}
}
108 changes: 108 additions & 0 deletions tests/Matcher/UuidMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Coduo\PHPMatcher\Tests\Matcher;

use Coduo\PHPMatcher\Matcher\UuidMatcher;

class UuidMatcherTest extends \PHPUnit_Framework_TestCase
{
/**
* @var UuidMatcher
*/
private $matcher;

public function setUp()
{
$this->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."),
);
}
}
4 changes: 2 additions & 2 deletions tests/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}';
Expand Down Expand Up @@ -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@'),
);
}

Expand Down