From e8446e72326dd4f0abcee4d8432dbb877d1cab9d Mon Sep 17 00:00:00 2001 From: norzechowicz Date: Sat, 23 Nov 2019 15:34:22 +0100 Subject: [PATCH 1/3] Replaced PHPMatcher facade with implementation that makes possible to access backtrace --- README.md | 50 +++++++++++++++++----- UPGRADE.md | 26 +++++++++++ src/Backtrace.php | 5 +++ src/Factory.php | 2 +- src/Factory/MatcherFactory.php | 4 +- src/Factory/SimpleFactory.php | 3 +- src/PHPMatcher.php | 42 ++++++++++++------ src/PHPUnit/PHPMatcherConstraint.php | 20 +++------ tests/BacktraceTest.php | 8 ++-- tests/EmptyPatternsTest.php | 8 +--- tests/ExpandersTest.php | 10 ++--- tests/Factory/SimpleFactoryTest.php | 3 +- tests/MatcherTest.php | 15 ------- tests/OrMatcherTest.php | 8 +--- tests/PHPUnit/PHPMatcherAssertionsTest.php | 44 +++++++++++++++---- tests/PHPUnit/PHPMatcherConstraintTest.php | 2 +- tests/PHPUnit/PHPMatcherTestCaseTest.php | 9 ++-- tests/ParserSyntaxErrorTest.php | 17 ++++---- 18 files changed, 172 insertions(+), 104 deletions(-) create mode 100644 UPGRADE.md diff --git a/README.md b/README.md index d9644a03..3caa7ed2 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,12 @@ Library created for testing all kinds of JSON/XML/TXT/Scalar values against patterns. +API: + ```php -PHPMatcher::match($value = '{"foo": "bar"}', $pattern = '{"foo": "@string@"}'); +PHPMatcher::match($value = '{"foo": "bar"}', $pattern = '{"foo": "@string@"}') : bool; +PHPMatcher::backtrace() : Backtrace; +PHPMatcher::error() : ?string; ``` It was built to simplify API's functional testing. @@ -31,32 +35,56 @@ composer require --dev "coduo/php-matcher" ## Basic usage -### Using facade +### Direct PHPMatcher usage ```php match("lorem ipsum dolor", "@string@"); + +if (!$match) { + echo "Error: " . $matcher->error(); + echo "Backtrace: \n"; + echo (string) $matcher->backtrace(); } +``` + +### PHPUnit extending PHPMatcherTestCase + +```php +assertMatchesPattern('{"name": "Norbert"}', '{"name": "@string@"}'); + } +} ``` -### Using Factory +### PHPUnit using PHPMatcherAssertions trait ```php createMatcher(); +class MatcherTest extends TestCase +{ + use PHPMatcherAssertions; -$match = $matcher->match("lorem ipsum dolor", "@string@"); -// $match === true -$matcher->getError(); // returns null or error message + public function test_matcher_that_value_matches_pattern() + { + $this->assertMatchesPattern('{"name": "Norbert"}', '{"name": "@string@"}'); + } +} ``` ### Available patterns diff --git a/UPGRADE.md b/UPGRADE.md new file mode 100644 index 00000000..3673f69c --- /dev/null +++ b/UPGRADE.md @@ -0,0 +1,26 @@ +# 3.x -> 4.0 + +Below you can find list of changes between 3.x and 4.0 versions of PHPMatcher. + +### Creating Matcher + +```diff +-$factory = new MatcherFactory(); +-$this->matcher = $factory->createMatcher(); +``` + +```diff ++$this->matcher = new PHPMatcher(); +``` + +### Simple Matching + +```diff +-PHPMatcher::match($value, $pattern, $error) +``` + +```diff ++$matcher = new PHPMatcher(); ++$matcher->match($value, $pattern); ++echo $matcher->error(); +``` \ No newline at end of file diff --git a/src/Backtrace.php b/src/Backtrace.php index f5fffcc8..f21814b8 100644 --- a/src/Backtrace.php +++ b/src/Backtrace.php @@ -104,6 +104,11 @@ public function expanderFailed(string $name, $value, string $error) : void ); } + public function isEmpty() : bool + { + return \count($this->trace) === 0; + } + public function __toString() : string { return \implode("\n", $this->trace); diff --git a/src/Factory.php b/src/Factory.php index 50e0a38a..1f8c8ee6 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -6,5 +6,5 @@ interface Factory { - public function createMatcher(Backtrace $backtrace = null) : Matcher; + public function createMatcher() : Matcher; } diff --git a/src/Factory/MatcherFactory.php b/src/Factory/MatcherFactory.php index c5ecff70..bc65b75b 100644 --- a/src/Factory/MatcherFactory.php +++ b/src/Factory/MatcherFactory.php @@ -12,9 +12,9 @@ final class MatcherFactory implements Factory { - public function createMatcher(Backtrace $backtrace = null) : Matcher + public function createMatcher() : Matcher { - $matcherBacktrace = $backtrace ? $backtrace : new Backtrace(); + $matcherBacktrace = new Backtrace(); return new Matcher($this->buildMatchers($this->buildParser($matcherBacktrace), $matcherBacktrace), $matcherBacktrace); } diff --git a/src/Factory/SimpleFactory.php b/src/Factory/SimpleFactory.php index b3b7c675..5248030e 100644 --- a/src/Factory/SimpleFactory.php +++ b/src/Factory/SimpleFactory.php @@ -4,7 +4,6 @@ namespace Coduo\PHPMatcher\Factory; -use Coduo\PHPMatcher\Backtrace; use Coduo\PHPMatcher\Factory; use Coduo\PHPMatcher\Matcher; @@ -13,7 +12,7 @@ */ class SimpleFactory implements Factory { - public function createMatcher(Backtrace $backtrace = null) : Matcher + public function createMatcher() : Matcher { return (new MatcherFactory())->createMatcher(); } diff --git a/src/PHPMatcher.php b/src/PHPMatcher.php index cc2dab42..155a3df9 100644 --- a/src/PHPMatcher.php +++ b/src/PHPMatcher.php @@ -8,27 +8,43 @@ final class PHPMatcher { - public static function match($value, $pattern, string &$error = null) : bool + private $matcher; + + public function match($value, $pattern) : bool { - $matcher = (new MatcherFactory())->createMatcher(); + $this->matcher = null; - if (!$matcher->match($value, $pattern)) { - $error = $matcher->getError(); - return false; - } + return $this->getMatcher()->match($value, $pattern); + } - return true; + /** + * Returns backtrace from last matching. + * When called before PHPMatcher::match() function it will return instance where Backtrace::isEmpty() will return true + * + * @return Backtrace + */ + public function backtrace() : Backtrace + { + return $this->getMatcher()->backtrace(); } - public static function matchBacktrace($value, $pattern, Backtrace $backtrace, string &$error = null) : bool + /** + * Returns error from last matching. + * If last matching was successful this function will return null. + * + * @return string|null + */ + public function error() : ?string { - $matcher = (new MatcherFactory())->createMatcher($backtrace); + return $this->getMatcher()->getError(); + } - if (!$matcher->match($value, $pattern)) { - $error = $matcher->getError(); - return false; + private function getMatcher() : Matcher + { + if (null === $this->matcher) { + $this->matcher = (new MatcherFactory())->createMatcher(); } - return true; + return $this->matcher; } } diff --git a/src/PHPUnit/PHPMatcherConstraint.php b/src/PHPUnit/PHPMatcherConstraint.php index b24e7097..4178b275 100644 --- a/src/PHPUnit/PHPMatcherConstraint.php +++ b/src/PHPUnit/PHPMatcherConstraint.php @@ -4,9 +4,7 @@ namespace Coduo\PHPMatcher\PHPUnit; -use Coduo\PHPMatcher\Backtrace; -use Coduo\PHPMatcher\Factory\MatcherFactory; -use Coduo\PHPMatcher\Matcher; +use Coduo\PHPMatcher\PHPMatcher; use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Util\Json; use SebastianBergmann\Comparator\ComparisonFailure; @@ -15,7 +13,6 @@ final class PHPMatcherConstraint extends Constraint { private $pattern; private $matcher; - private $backtrace; private $lastValue; public function __construct($pattern) @@ -33,8 +30,7 @@ public function __construct($pattern) } $this->pattern = $pattern; - $this->backtrace = new Backtrace(); - $this->matcher = $this->createMatcher(); + $this->matcher = new PHPMatcher(); } /** @@ -42,12 +38,15 @@ public function __construct($pattern) */ public function toString() : string { - return 'matches the pattern'; + return 'matches given pattern.'; } protected function failureDescription($other): string { - return parent::failureDescription($other) . ".\nError: " . $this->matcher->getError(); + return parent::failureDescription($other) + . "\nPattern: " . $this->exporter()->export($this->pattern) + . "\nError: " . $this->matcher->error() + . "\nBacktrace: \n" . $this->matcher->backtrace(); } protected function matches($value) : bool @@ -55,11 +54,6 @@ protected function matches($value) : bool return $this->matcher->match($this->lastValue = $value, $this->pattern); } - private function createMatcher() : Matcher - { - return (new MatcherFactory())->createMatcher($this->backtrace); - } - /** * {@inheritdoc} */ diff --git a/tests/BacktraceTest.php b/tests/BacktraceTest.php index 377fa67a..358415d6 100644 --- a/tests/BacktraceTest.php +++ b/tests/BacktraceTest.php @@ -4,21 +4,19 @@ namespace Coduo\PHPMatcher\Tests; -use Coduo\PHPMatcher\Factory\MatcherFactory; -use Coduo\PHPMatcher\Matcher; +use Coduo\PHPMatcher\PHPMatcher; use PHPUnit\Framework\TestCase; final class BacktraceTest extends TestCase { /** - * @var Matcher + * @var PHPMatcher */ protected $matcher; public function setUp() : void { - $factory = new MatcherFactory(); - $this->matcher = $factory->createMatcher(); + $this->matcher = new PHPMatcher(); } public function test_backtrace_in_failed_simple_matching() diff --git a/tests/EmptyPatternsTest.php b/tests/EmptyPatternsTest.php index dc652874..a2f0ca71 100644 --- a/tests/EmptyPatternsTest.php +++ b/tests/EmptyPatternsTest.php @@ -4,22 +4,19 @@ namespace Coduo\PHPMatcher\Tests; -use Coduo\PHPMatcher\Factory\MatcherFactory; -use Coduo\PHPMatcher\Matcher; use Coduo\PHPMatcher\PHPMatcher; use PHPUnit\Framework\TestCase; final class EmptyPatternsTest extends TestCase { /** - * @var Matcher + * @var PHPMatcher */ protected $matcher; public function setUp() : void { - $factory = new MatcherFactory(); - $this->matcher = $factory->createMatcher(); + $this->matcher = new PHPMatcher(); } /** @@ -29,7 +26,6 @@ public function test_empty_pattern_in_the_json($value, $pattern, $expectedResult { $match = $this->matcher->match($value, $pattern); $this->assertSame($expectedResult, $match); - $this->assertSame($expectedResult, PHPMatcher::match($value, $pattern)); } public static function emptyPatternString() diff --git a/tests/ExpandersTest.php b/tests/ExpandersTest.php index 376cc881..c6aa0ee3 100644 --- a/tests/ExpandersTest.php +++ b/tests/ExpandersTest.php @@ -4,22 +4,19 @@ namespace Coduo\PHPMatcher\Tests; -use Coduo\PHPMatcher\Factory\MatcherFactory; -use Coduo\PHPMatcher\Matcher; use Coduo\PHPMatcher\PHPMatcher; use PHPUnit\Framework\TestCase; final class ExpandersTest extends TestCase { /** - * @var Matcher + * @var PHPMatcher */ protected $matcher; public function setUp() : void { - $factory = new MatcherFactory(); - $this->matcher = $factory->createMatcher(); + $this->matcher = new PHPMatcher(); } /** @@ -27,8 +24,7 @@ public function setUp() : void */ public function test_expanders($value, $pattern, $expectedResult) { - $this->assertSame($expectedResult, $this->matcher->match($value, $pattern), (string) $this->matcher->getError()); - $this->assertSame($expectedResult, PHPMatcher::match($value, $pattern)); + $this->assertSame($expectedResult, $this->matcher->match($value, $pattern), (string) $this->matcher->error()); } public static function expanderExamples() diff --git a/tests/Factory/SimpleFactoryTest.php b/tests/Factory/SimpleFactoryTest.php index 5b526023..394396e2 100644 --- a/tests/Factory/SimpleFactoryTest.php +++ b/tests/Factory/SimpleFactoryTest.php @@ -5,6 +5,7 @@ namespace Coduo\PHPMatcher\Tests; use Coduo\PHPMatcher\Factory\SimpleFactory; +use Coduo\PHPMatcher\Matcher; use PHPUnit\Framework\TestCase; class SimpleFactoryTest extends TestCase @@ -12,6 +13,6 @@ class SimpleFactoryTest extends TestCase public function test_creating_matcher() { $factory = new SimpleFactory(); - $this->assertInstanceOf('Coduo\PHPMatcher\Matcher', $factory->createMatcher()); + $this->assertInstanceOf(Matcher::class, $factory->createMatcher()); } } diff --git a/tests/MatcherTest.php b/tests/MatcherTest.php index 9c5d0aa6..ab3af29b 100644 --- a/tests/MatcherTest.php +++ b/tests/MatcherTest.php @@ -4,7 +4,6 @@ namespace Coduo\PHPMatcher\Tests; -use Coduo\PHPMatcher\PHPMatcher; use Coduo\PHPMatcher\PHPUnit\PHPMatcherTestCase; class MatcherTest extends PHPMatcherTestCase @@ -15,7 +14,6 @@ class MatcherTest extends PHPMatcherTestCase public function test_matcher_with_scalar_values($value, $pattern) { $this->assertMatchesPattern($pattern, $value); - $this->assertTrue(PHPMatcher::match($value, $pattern)); } public function test_matcher_with_array_value() @@ -59,7 +57,6 @@ public function test_matcher_with_array_value() ]; $this->assertMatchesPattern($pattern, $value); - $this->assertTrue(PHPMatcher::match($value, $pattern, $error), (string) $error); } /** @@ -68,7 +65,6 @@ public function test_matcher_with_array_value() public function test_matcher_with_json($value, $pattern) { $this->assertMatchesPattern($pattern, $value); - $this->assertTrue(PHPMatcher::match($value, $pattern)); } public function test_matcher_with_xml() @@ -105,7 +101,6 @@ public function test_matcher_with_xml() XML; $this->assertMatchesPattern($pattern, $value); - $this->assertTrue(PHPMatcher::match($value, $pattern)); } public function test_matcher_with_xml_including_optional_node() @@ -143,7 +138,6 @@ public function test_matcher_with_xml_including_optional_node() XML; $this->assertMatchesPattern($pattern, $value); - $this->assertTrue(PHPMatcher::match($value, $pattern)); } public function test_full_text_matcher() @@ -151,7 +145,6 @@ public function test_full_text_matcher() $value = 'lorem ipsum 1234 random text'; $pattern = "@string@.startsWith('lo') ipsum @number@.greaterThan(10) random text"; $this->assertMatchesPattern($pattern, $value); - $this->assertTrue(PHPMatcher::match($value, $pattern)); } public function test_matcher_with_callback() @@ -162,20 +155,12 @@ function ($value) { }, 'test' ); - $this->assertTrue(PHPMatcher::match('test', function ($value) { - return $value === 'test'; - })); - $this->assertFalse(PHPMatcher::match('test', function ($value) { - return $value !== 'test'; - })); } public function test_matcher_with_wildcard() { $this->assertMatchesPattern('@*@', 'test'); - $this->assertTrue(PHPMatcher::match('test', '@*@')); $this->assertMatchesPattern('@wildcard@', 'test'); - $this->assertTrue(PHPMatcher::match('test', '@wildcard@')); } /** diff --git a/tests/OrMatcherTest.php b/tests/OrMatcherTest.php index 5b7b1cd3..7dec1e5b 100644 --- a/tests/OrMatcherTest.php +++ b/tests/OrMatcherTest.php @@ -4,22 +4,19 @@ namespace Coduo\PHPMatcher\Tests; -use Coduo\PHPMatcher\Factory\MatcherFactory; -use Coduo\PHPMatcher\Matcher; use Coduo\PHPMatcher\PHPMatcher; use PHPUnit\Framework\TestCase; final class OrMatcherTest extends TestCase { /** - * @var Matcher + * @var PHPMatcher */ protected $matcher; public function setUp() : void { - $factory = new MatcherFactory(); - $this->matcher = $factory->createMatcher(); + $this->matcher = new PHPMatcher(); } /** @@ -28,7 +25,6 @@ public function setUp() : void public function test_matcher_with_or($value, $pattern, $expectedResult) { $this->assertSame($expectedResult, $this->matcher->match($value, $pattern)); - $this->assertSame($expectedResult, PHPMatcher::match($value, $pattern)); } public static function orExamples() diff --git a/tests/PHPUnit/PHPMatcherAssertionsTest.php b/tests/PHPUnit/PHPMatcherAssertionsTest.php index 0818bab2..b285ce2a 100644 --- a/tests/PHPUnit/PHPMatcherAssertionsTest.php +++ b/tests/PHPUnit/PHPMatcherAssertionsTest.php @@ -5,6 +5,7 @@ namespace Coduo\PHPMatcher\Tests\PHPUnit; use Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions; +use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\TestCase; class PHPMatcherAssertionsTest extends TestCase @@ -18,16 +19,46 @@ public function test_it_asserts_if_a_value_matches_the_pattern() public function test_it_throws_an_expectation_failed_exception_if_a_value_does_not_match_the_pattern() { - $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); - $this->expectExceptionMessage("Failed asserting that '{\"foo\":\"bar\"}' matches the pattern"); + $this->expectException(AssertionFailedError::class); + + /** + * Expected console output: + * + * Failed asserting that '{"foo":"bar"}' matches given pattern. + * Pattern: '{"foo": "@integer@"}' + * Error: Value {"foo":"bar"} does not match pattern {"foo":"@integer@"} + * Backtrace: + * #1 Matcher Coduo\PHPMatcher\Matcher matching value "{"foo":"bar"}" with "{"foo":"@integer@"}" pattern + * #2 Matcher Coduo\PHPMatcher\Matcher\ChainMatcher (all) matching value "{"foo":"bar"}" with "{"foo":"@integer@"}" pattern + * #3 Matcher Coduo\PHPMatcher\Matcher\ChainMatcher (scalars) can match pattern "{"foo":"@integer@"}" + * #... + * #66 Matcher Coduo\PHPMatcher\Matcher error: Value {"foo":"bar"} does not match pattern {"foo":"@integer@"}. + */ + $this->expectExceptionMessageMatches("/Failed asserting that '{\"foo\":\"bar\"}' matches given pattern.\nPattern: '{\"foo\": \"@integer@\"}'\nError: Value {\"foo\":\"bar\"} does not match pattern {\"foo\":\"@integer@\"}\nBacktrace: \n(.*)/"); $this->assertMatchesPattern('{"foo": "@integer@"}', \json_encode(['foo' => 'bar'])); } public function test_it_creates_a_constraint_for_stubs() { - $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); - $this->expectExceptionMessage('Failed asserting that 42 matches the pattern.'); + $this->expectException(AssertionFailedError::class); + + /** + * Expected console output: + * + * Expectation failed for method name is "getTitle" when invoked zero or more time s + * Parameter 0 for invocation stdClass::getTitle(42) does not match expected value. + * Failed asserting that 42 matches given pattern. + * Pattern: '@string@' + * Error: integer "42" is not a valid string. + * Backtrace: + * #1 Matcher Coduo\PHPMatcher\Matcher matching value "42" with "@string@" pattern + * #2 Matcher Coduo\PHPMatcher\Matcher\ChainMatcher (all) matching value "42" with "@string@" pattern + * #3 Matcher Coduo\PHPMatcher\Matcher\ChainMatcher (scalars) can match pattern "@string@" + * #... + * #35 Matcher Coduo\PHPMatcher\Matcher error: integer "42" is not a valid string. + */ + $this->expectExceptionMessageMatches("/Expectation failed for method name is \"getTitle\" when invoked zero or more times\nParameter 0 for invocation stdClass::getTitle\(42\) does not match expected value.\nFailed asserting that 42 matches given pattern.\nPattern: '@string@'\nError: integer \"42\" is not a valid string.\nBacktrace: \n(.*)/"); $mock = $this->getMockBuilder('stdClass') ->setMethods(['getTitle']) @@ -39,9 +70,4 @@ public function test_it_creates_a_constraint_for_stubs() $mock->getTitle(42); } - - public function test_it_asserts_if_a_value_matches_the_array_pattern() - { - $this->assertMatchesPattern(['foo' => '@string@'], ['foo' => 'bar']); - } } diff --git a/tests/PHPUnit/PHPMatcherConstraintTest.php b/tests/PHPUnit/PHPMatcherConstraintTest.php index 4cf2288b..0b140a23 100644 --- a/tests/PHPUnit/PHPMatcherConstraintTest.php +++ b/tests/PHPUnit/PHPMatcherConstraintTest.php @@ -33,7 +33,7 @@ public function test_it_returns_false_if_a_value_does_not_match_the_pattern() public function test_it_sets_a_failure_description_if_not_given() { $this->expectException(AssertionFailedError::class); - $this->expectExceptionMessage('Failed asserting that 42 matches the pattern'); + $this->expectExceptionMessageMatches('/Failed asserting that 42 matches given pattern(.*)/'); $constraint = new PHPMatcherConstraint('@string@'); diff --git a/tests/PHPUnit/PHPMatcherTestCaseTest.php b/tests/PHPUnit/PHPMatcherTestCaseTest.php index 89f08f2c..97e1431d 100644 --- a/tests/PHPUnit/PHPMatcherTestCaseTest.php +++ b/tests/PHPUnit/PHPMatcherTestCaseTest.php @@ -5,6 +5,7 @@ namespace Coduo\PHPMatcher\Tests\PHPUnit; use Coduo\PHPMatcher\PHPUnit\PHPMatcherTestCase; +use PHPUnit\Framework\AssertionFailedError; class PHPMatcherTestCaseTest extends PHPMatcherTestCase { @@ -15,16 +16,16 @@ public function test_it_asserts_if_a_value_matches_the_pattern() public function test_it_throws_an_expectation_failed_exception_if_a_value_does_not_match_the_pattern() { - $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); - $this->expectExceptionMessage("Failed asserting that '{\"foo\":\"bar\"}' matches the pattern"); + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessageMatches("/Failed asserting that '{\"foo\":\"bar\"}' matches given pattern(.*)/"); $this->assertMatchesPattern('{"foo": "@integer@"}', \json_encode(['foo' => 'bar'])); } public function test_it_creates_a_constraint_for_stubs() { - $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); - $this->expectExceptionMessage('Failed asserting that 42 matches the pattern.'); + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessageMatches('/Failed asserting that 42 matches given pattern(.*)/'); $mock = $this->getMockBuilder('stdClass') ->setMethods(['getTitle']) diff --git a/tests/ParserSyntaxErrorTest.php b/tests/ParserSyntaxErrorTest.php index 5690e94f..dfc2b96e 100644 --- a/tests/ParserSyntaxErrorTest.php +++ b/tests/ParserSyntaxErrorTest.php @@ -5,6 +5,7 @@ namespace Coduo\PHPMatcher\Tests; use Coduo\PHPMatcher\Backtrace; +use Coduo\PHPMatcher\Exception\PatternException; use Coduo\PHPMatcher\Lexer; use Coduo\PHPMatcher\Parser; use PHPUnit\Framework\TestCase; @@ -23,7 +24,7 @@ public function setUp() : void public function test_unexpected_statement_at_type_position() { - $this->expectException(\Coduo\PHPMatcher\Exception\PatternException::class); + $this->expectException(PatternException::class); $this->expectExceptionMessage('[Syntax Error] line 0, col 0: Error: Expected "@type@ pattern", got "not"'); $this->parser->getAST('not_valid_type'); @@ -31,7 +32,7 @@ public function test_unexpected_statement_at_type_position() public function test_unexpected_statement_instead_of_expander() { - $this->expectException(\Coduo\PHPMatcher\Exception\PatternException::class); + $this->expectException(PatternException::class); $this->expectExceptionMessage('[Syntax Error] line 0, col 6: Error: Expected ".expanderName(args) definition", got "anything"'); $this->parser->getAST('@type@anything'); @@ -39,7 +40,7 @@ public function test_unexpected_statement_instead_of_expander() public function test_end_of_string_after_opening_parenthesis() { - $this->expectException(\Coduo\PHPMatcher\Exception\PatternException::class); + $this->expectException(PatternException::class); $this->expectExceptionMessage('[Syntax Error] line 0, col 14: Error: Expected ")", got end of string.end of string'); $this->parser->getAST('@type@.expander('); @@ -47,7 +48,7 @@ public function test_end_of_string_after_opening_parenthesis() public function test_not_argument_after_opening_parenthesis() { - $this->expectException(\Coduo\PHPMatcher\Exception\PatternException::class); + $this->expectException(PatternException::class); $this->expectExceptionMessage('[Syntax Error] line 0, col 16: Error: Expected "string, number, boolean or null argument", got "not"'); $this->parser->getAST('@type@.expander(not_argument'); @@ -55,7 +56,7 @@ public function test_not_argument_after_opening_parenthesis() public function test_missing_close_parenthesis_after_single_argument() { - $this->expectException(\Coduo\PHPMatcher\Exception\PatternException::class); + $this->expectException(PatternException::class); $this->expectExceptionMessage('[Syntax Error] line 0, col 22: Error: Expected ")", got end of string.end of string'); $this->parser->getAST("@type@.expander('string'"); @@ -63,7 +64,7 @@ public function test_missing_close_parenthesis_after_single_argument() public function test_missing_close_parenthesis_after_multiple_arguments() { - $this->expectException(\Coduo\PHPMatcher\Exception\PatternException::class); + $this->expectException(PatternException::class); $this->expectExceptionMessage('[Syntax Error] line 0, col 26: Error: Expected ")", got end of string.end of string'); $this->parser->getAST("@type@.expander('string',1"); @@ -71,7 +72,7 @@ public function test_missing_close_parenthesis_after_multiple_arguments() public function test_missing_argument_after_comma() { - $this->expectException(\Coduo\PHPMatcher\Exception\PatternException::class); + $this->expectException(PatternException::class); $this->expectExceptionMessage('[Syntax Error] line 0, col 25: Error: Expected "string, number, boolean or null argument", got ")"'); $this->parser->getAST("@type@.expander('string',)"); @@ -79,7 +80,7 @@ public function test_missing_argument_after_comma() public function test_not_argument_after_comma() { - $this->expectException(\Coduo\PHPMatcher\Exception\PatternException::class); + $this->expectException(PatternException::class); $this->expectExceptionMessage('[Syntax Error] line 0, col 25: Error: Expected "string, number, boolean or null argument", got "not"'); $this->parser->getAST("@type@.expander('string',not_argument"); From c4d20eddd9c94617944c6bb75617647df7336c18 Mon Sep 17 00:00:00 2001 From: norzechowicz Date: Sat, 23 Nov 2019 15:38:01 +0100 Subject: [PATCH 2/3] Improved UPGRADE.md file --- UPGRADE.md | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 3673f69c..05140291 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,26 +1,24 @@ # 3.x -> 4.0 -Below you can find list of changes between 3.x and 4.0 versions of PHPMatcher. - -### Creating Matcher +Below you can find list of changes between `3.x` and `4.0` versions of PHPMatcher. +**Creating Matcher:** ```diff -$factory = new MatcherFactory(); --$this->matcher = $factory->createMatcher(); -``` - -```diff -+$this->matcher = new PHPMatcher(); +-$matcher = $factory->createMatcher(); ++$matcher = new PHPMatcher(); ``` -### Simple Matching - +**Using Matcher** ```diff -PHPMatcher::match($value, $pattern, $error) ++$matcher = (new PHPMatcher())->match($value, $pattern);; ``` +**Accessing last error/backtrace** ```diff +$matcher = new PHPMatcher(); +$matcher->match($value, $pattern); +echo $matcher->error(); ++echo $matcher->backtrace(); ``` \ No newline at end of file From 31a85775f1a1d141a912c9628062f73bbddffd86 Mon Sep 17 00:00:00 2001 From: norzechowicz Date: Sat, 23 Nov 2019 15:47:45 +0100 Subject: [PATCH 3/3] Bumped phpunit dependency --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e695e552..2c165a59 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "openlss/lib-array2xml": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^7.0|^8.0", + "phpunit/phpunit": "^8.4", "friendsofphp/php-cs-fixer": "^2.4" }, "autoload": {