diff --git a/src/RequestCondition.php b/src/RequestCondition.php index f7c6918..0386332 100644 --- a/src/RequestCondition.php +++ b/src/RequestCondition.php @@ -116,13 +116,13 @@ public function match(Context $context): bool $this->_routedPath = $context->meta()->get(self::META_ROUTED_PATH, '/'); foreach($this->_conditions as [$matchOn, $matchWith, $matchType]) { - if($matchOn == self::PATH) + if($matchOn === self::PATH) { if($matchWith === '/') { $matchType = self::TYPE_START; } - else if(is_string($matchWith)) + else if(is_string($matchWith) && $matchType !== self::TYPE_REGEX) { $matchWith = $this->_convertPathToRegex($matchWith, $matchType); $matchType = self::TYPE_REGEX; diff --git a/tests/RequestConstraintTest.php b/tests/RequestConstraintTest.php index 65c2018..1d9a4c3 100644 --- a/tests/RequestConstraintTest.php +++ b/tests/RequestConstraintTest.php @@ -131,6 +131,64 @@ public function testRegexInConstraintVariable() RequestCondition::i()->path('/{test#test}')->match($ctx); } + /** + * @dataProvider regexDataProvider + */ + public function testRegexMatch($url, $regex, $expected) + { + $request = Request::create($url); + $ctx = new Context($request); + $this->assertEquals($expected, RequestCondition::i()->path($regex, RequestCondition::TYPE_REGEX)->match($ctx)); + } + + public static function regexDataProvider() + { + return [ + [ + 'http://www.test.com:8080/lander-1', + '/-1$/', + true + ], + [ + 'http://www.test.com:8080/lander-1', + '/-2$/', + false + ], + [ + 'http://www.test.com:8080/lander-1', + '/lander/', + true + ], + [ + 'http://www.test.com:8080/lander-2', + '/.*lander-[1-9]/', + true + ], + [ + 'http://www.test.com:8080/lander-2', + '/.*lander-[^1-9]/', + false + ], + [ + 'http://www.test.com:8080/lander-2', + '/.*lander-\d{2}/', + false + ], + [ + 'http://www.test.com:8080/lander-21', + '/.*lander-\d{2}/', + true + ], + [ + 'http://www.test.com:8080/lander-2', + '/^lander$/', + false + ], + ]; + } + + + public function testMatchedPath() { $request = Request::create('http://www.test.com:8080/one/two/three', 'POST');