-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from norzechowicz/php-matcher-facade
Added PHPMatcher facade in order to simplify developers experience
- Loading branch information
Showing
3 changed files
with
70 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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()); | ||
|
@@ -63,7 +62,7 @@ public function test_matcher_with_array_value() | |
'data' => new \stdClass(), | ||
); | ||
|
||
$expecation = array( | ||
$expectation = array( | ||
'users' => array( | ||
array( | ||
'id' => '@integer@', | ||
|
@@ -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() | ||
|
@@ -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() | ||
|
@@ -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)); | ||
} | ||
|
||
|
||
|
@@ -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@')); | ||
} | ||
|
||
/** | ||
|
@@ -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( | ||
|