Skip to content

Commit

Permalink
Merge pull request #64 from norzechowicz/php-matcher-facade
Browse files Browse the repository at this point in the history
Added PHPMatcher facade in order to simplify developers experience
  • Loading branch information
Norbert Orzechowicz committed Jan 24, 2016
2 parents a11daa5 + 85461e6 commit bfb6e0d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ composer require --dev "coduo/php-matcher"

## Basic usage

### Using facade

```php
<?php

use Coduo\PHPMatcher\PHPMatcher;

if (!PHPMatcher::match("lorem ipsum dolor", "@string@", $error)) {
echo $error; // in case of error message is set on $error variable via reference
}

```


### Using Factory

```php
<?php

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

namespace Coduo\PHPMatcher;

use Coduo\PHPMatcher\Factory\SimpleFactory;

final class PHPMatcher
{
/**
* @param $value
* @param $pattern
* @param null $error
* @return bool
*/
public static function match($value, $pattern, &$error = null)
{
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();

if (!$matcher->match($value, $pattern)) {
$error = $matcher->getError();
return false;
}

return true;
}
}
42 changes: 27 additions & 15 deletions tests/Coduo/PHPMatcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Coduo\PHPMatcher\Lexer;
use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Parser;
use Coduo\PHPMatcher\PHPMatcher;

class MatcherTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -13,8 +14,6 @@ class MatcherTest extends \PHPUnit_Framework_TestCase
*/
protected $matcher;

protected $arrayValue;

public function setUp()
{
$parser = new Parser(new Lexer(), new Parser\ExpanderInitializer());
Expand Down Expand Up @@ -63,7 +62,7 @@ public function test_matcher_with_array_value()
'data' => new \stdClass(),
);

$expecation = array(
$expectation = array(
'users' => array(
array(
'id' => '@integer@',
Expand All @@ -82,25 +81,17 @@ public function test_matcher_with_array_value()
'data' => '@wildcard@',
);

$this->assertTrue($this->matcher->match($value, $expecation), $this->matcher->getError());
$this->assertTrue($this->matcher->match($value, $expectation), $this->matcher->getError());
$this->assertTrue(PHPMatcher::match($value, $expectation, $error), $error);
}

/**
* @dataProvider scalarValues
* @dataProvider scalarValueExamples
*/
public function test_matcher_with_scalar_values($value, $pattern)
{
$this->assertTrue($this->matcher->match($value, $pattern));
}

public function scalarValues()
{
return array(
array('Norbert Orzechowicz', '@string@'),
array(6.66, '@double@'),
array(1, '@integer@'),
array(array('foo'), '@array@')
);
$this->assertTrue(PHPMatcher::match($value, $pattern));
}

public function test_matcher_with_json()
Expand Down Expand Up @@ -149,6 +140,7 @@ public function test_matcher_with_json()
}';

$this->assertTrue($this->matcher->match($json, $jsonPattern));
$this->assertTrue(PHPMatcher::match($json, $jsonPattern));
}

public function test_matcher_with_xml()
Expand Down Expand Up @@ -185,13 +177,15 @@ public function test_matcher_with_xml()
XML;

$this->assertTrue($this->matcher->match($xml, $xmlPattern));
$this->assertTrue(PHPMatcher::match($xml, $xmlPattern));
}

public function test_text_matcher()
{
$value = "lorem ipsum 1234 random text";
$pattern = "@[email protected]('lo') ipsum @[email protected](10) random text";
$this->assertTrue($this->matcher->match($value, $pattern));
$this->assertTrue(PHPMatcher::match($value, $pattern));
}


Expand All @@ -202,18 +196,25 @@ public function test_error_when_json_value_does_not_match_json_pattern()

$this->assertFalse($this->matcher->match($value, $pattern));
$this->assertSame('"5" does not match "4".', $this->matcher->getError());

$this->assertFalse(PHPMatcher::match($value, $pattern, $error));
$this->assertSame('"5" does not match "4".', $error);
}

public function test_matcher_with_callback()
{
$this->assertTrue($this->matcher->match('test', function($value) { return $value === 'test';}));
$this->assertTrue(PHPMatcher::match('test', function($value) { return $value === 'test';}));
$this->assertFalse($this->matcher->match('test', function($value) { return $value !== 'test';}));
$this->assertFalse(PHPMatcher::match('test', function($value) { return $value !== 'test';}));
}

public function test_matcher_with_wildcard()
{
$this->assertTrue($this->matcher->match('test', '@*@'));
$this->assertTrue(PHPMatcher::match('test', '@*@'));
$this->assertTrue($this->matcher->match('test', '@wildcard@'));
$this->assertTrue(PHPMatcher::match('test', '@wildcard@'));
}

/**
Expand All @@ -222,8 +223,19 @@ public function test_matcher_with_wildcard()
public function test_expanders($value, $pattern, $expectedResult)
{
$this->assertSame($expectedResult, $this->matcher->match($value, $pattern));
$this->assertSame($expectedResult, PHPMatcher::match($value, $pattern));
}

public function scalarValueExamples()
{
return array(
array('Norbert Orzechowicz', '@string@'),
array(6.66, '@double@'),
array(1, '@integer@'),
array(array('foo'), '@array@')
);
}

public static function expanderExamples()
{
return array(
Expand Down

0 comments on commit bfb6e0d

Please sign in to comment.