diff --git a/README.md b/README.md index 314b0e01..7f25c97e 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,22 @@ composer require --dev "coduo/php-matcher" ## Basic usage +### Using facade + +```php +createMatcher(); + + if (!$matcher->match($value, $pattern)) { + $error = $matcher->getError(); + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/tests/Coduo/PHPMatcher/MatcherTest.php b/tests/Coduo/PHPMatcher/MatcherTest.php index 7e70ff46..fb200e1e 100644 --- a/tests/Coduo/PHPMatcher/MatcherTest.php +++ b/tests/Coduo/PHPMatcher/MatcherTest.php @@ -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,6 +177,7 @@ 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() @@ -192,6 +185,7 @@ public function test_text_matcher() $value = "lorem ipsum 1234 random text"; $pattern = "@string@.startsWith('lo') ipsum @number@.greaterThan(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(