diff --git a/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php b/src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php index 744bc38e..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 @@ -86,13 +85,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 +108,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); + } }