From 4cb456882f4aea61061dc3578438e16a29736837 Mon Sep 17 00:00:00 2001 From: Michal Dabrowski Date: Thu, 22 May 2014 14:50:01 +0200 Subject: [PATCH 1/2] Removing 2.4 dependency on property accessor --- src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php | 9 +-------- src/Coduo/PHPMatcher/Matcher/CaptureMatcher.php | 1 - tests/Coduo/PHPMatcher/MatcherTest.php | 16 ++++++++++++++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php b/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php index 744bc38e..47c9007e 100644 --- a/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php +++ b/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php @@ -86,13 +86,7 @@ private function iterateMatch(array $value, array $pattern) */ private function hasValue($array, $path) { - try { - $this->getPropertyAccessor()->getValue($array, $path); - } catch (NoSuchIndexException $e) { - return false; - } - - return true; + return null !== $this->getPropertyAccessor()->getValue($array, $path); } /** @@ -115,7 +109,6 @@ private function getPropertyAccessor() } $accessorBuilder = PropertyAccess::createPropertyAccessorBuilder(); - $accessorBuilder->enableExceptionOnInvalidIndex(); $this->accessor = $accessorBuilder->getPropertyAccessor(); return $this->accessor; diff --git a/src/Coduo/PHPMatcher/Matcher/CaptureMatcher.php b/src/Coduo/PHPMatcher/Matcher/CaptureMatcher.php index 9bc7107b..683b2a7c 100644 --- a/src/Coduo/PHPMatcher/Matcher/CaptureMatcher.php +++ b/src/Coduo/PHPMatcher/Matcher/CaptureMatcher.php @@ -14,7 +14,6 @@ class CaptureMatcher extends Matcher implements \ArrayAccess public function match($value, $pattern) { $this->captures[$this->extractPattern($pattern)] = $value; - return true; } diff --git a/tests/Coduo/PHPMatcher/MatcherTest.php b/tests/Coduo/PHPMatcher/MatcherTest.php index f19bbdf1..8ad6501e 100644 --- a/tests/Coduo/PHPMatcher/MatcherTest.php +++ b/tests/Coduo/PHPMatcher/MatcherTest.php @@ -2,6 +2,7 @@ namespace Coduo\PHPMatcher\Tests; use Coduo\PHPMatcher\Matcher\ArrayMatcher; +use Coduo\PHPMatcher\Matcher\CaptureMatcher; use Coduo\PHPMatcher\Matcher\ChainMatcher; use Coduo\PHPMatcher\Matcher\ExpressionMatcher; use Coduo\PHPMatcher\Matcher\JsonMatcher; @@ -16,10 +17,16 @@ class MatcherTest extends \PHPUnit_Framework_TestCase protected $arrayValue; + protected $captureMatcher; + public function setUp() { + $this->captureMatcher = new CaptureMatcher(); + $scalarMatchers = new ChainMatcher(array( new ExpressionMatcher(), + $this->captureMatcher, + new CaptureMatcher(), new TypeMatcher(), new ScalarMatcher(), new WildcardMatcher() @@ -168,4 +175,13 @@ public function test_matcher_with_json() $this->assertTrue($this->matcher->match($json, $jsonPattern)); $this->assertTrue(match($json, $jsonPattern)); } + + public function test_matcher_with_captures() + { + $this->assertTrue($this->matcher->match( + array('foo' => 'bar', 'user' => array('id' => 5)), + array('foo' => 'bar', 'user' => array('id' => ':uid:')) + )); + $this->assertEquals($this->captureMatcher['uid'], 5); + } } From c2a50b8be76fe5684baedd649b172c2d174e46b4 Mon Sep 17 00:00:00 2001 From: Michal Dabrowski Date: Thu, 22 May 2014 14:55:30 +0200 Subject: [PATCH 2/2] Removing unused use statement --- src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php b/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php index 47c9007e..edc6cdec 100644 --- a/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php +++ b/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php @@ -3,7 +3,6 @@ namespace Coduo\PHPMatcher\Matcher; use Symfony\Component\PropertyAccess\PropertyAccess; -use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException; use Symfony\Component\PropertyAccess\PropertyAccessor; class ArrayMatcher extends Matcher