From 127c1705f0e6fff460f351da9366004fe1fd122a Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Wed, 19 Jun 2019 13:01:59 +0100 Subject: [PATCH] Support EOL `$` on path routing (#1) * gitignore * support EOL ($) on routing * only convert strings to regex * add circleci * increase php require * circle >=php72 --- .circleci/config.yml | 42 +++++++++++++++++++++++++++++++++ .gitignore | 2 ++ composer.json | 2 +- src/RequestCondition.php | 13 +++++++--- tests/RequestConstraintTest.php | 32 ++++++++++++++++++++++++- 5 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 .circleci/config.yml create mode 100644 .gitignore diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..dec3891 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,42 @@ +defaults: &defaults + steps: + # common php steps + - run: echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories + - run: echo "date.timezone = UTC" >> $(php --ini |grep Scan |awk '{print $NF}')/timezone.ini + - run: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer + + # pre-checkout steps + + # checkout + - checkout + + # post-checkout steps + + # run tests + - run: composer install -n --prefer-dist + - run: php vendor/phpunit/phpunit/phpunit -c phpunit.xml --log-junit /tmp/test-results/phpunit/junit.xml + - store_test_results: + path: /tmp/test-results + +version: 2 +jobs: + build-php72: + <<: *defaults + docker: + - image: php:7.2-alpine + build-php73: + <<: *defaults + docker: + - image: php:7.3-alpine + build-phpRC: + <<: *defaults + docker: + - image: php:rc-alpine + +workflows: + version: 2 + build: + jobs: + - build-php72 + - build-php73 + - build-phpRC diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fbb073 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +/composer.lock diff --git a/composer.json b/composer.json index 34b7913..f92ec3b 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require": { - "php": ">=7.1", + "php": ">=7.2", "packaged/context": "~1.0", "packaged/helpers": "~2.0" }, diff --git a/src/RequestCondition.php b/src/RequestCondition.php index 35aa50b..f7c6918 100644 --- a/src/RequestCondition.php +++ b/src/RequestCondition.php @@ -122,7 +122,7 @@ public function match(Context $context): bool { $matchType = self::TYPE_START; } - else + else if(is_string($matchWith)) { $matchWith = $this->_convertPathToRegex($matchWith, $matchType); $matchType = self::TYPE_REGEX; @@ -139,13 +139,20 @@ public function match(Context $context): bool protected function _convertPathToRegex($path, $type) { + if(substr($path, -1) === '$') + { + $path = substr($path, 0, -1); + $type = self::TYPE_EXACT; + } + if(empty($path)) { $path = $this->_routedPath; } - else if($path[0] !== '/') + + if($path && $path[0] !== '/') { - $path = rtrim($this->_routedPath, '/') . '/' . $path; + $path = rtrim($this->_routedPath, '/') . ($path ? '/' . $path : ''); } if(strpos($path, '{') !== false) diff --git a/tests/RequestConstraintTest.php b/tests/RequestConstraintTest.php index e20cea8..65c2018 100644 --- a/tests/RequestConstraintTest.php +++ b/tests/RequestConstraintTest.php @@ -3,8 +3,8 @@ namespace Packaged\Tests\Routing; use Packaged\Context\Context; -use Packaged\Routing\RequestCondition; use Packaged\Http\Request; +use Packaged\Routing\RequestCondition; use PHPUnit\Framework\TestCase; class RequestConstraintTest extends TestCase @@ -130,4 +130,34 @@ public function testRegexInConstraintVariable() $ctx = new Context($request); RequestCondition::i()->path('/{test#test}')->match($ctx); } + + public function testMatchedPath() + { + $request = Request::create('http://www.test.com:8080/one/two/three', 'POST'); + $ctx = new Context($request); + + // fail to match EOL + $ctx->meta()->set(RequestCondition::META_ROUTED_PATH, '/one/two'); + $constraint = RequestCondition::i()->path('$'); + $this->assertFalse($constraint->match($ctx)); + $this->assertEquals('/one/two', $ctx->meta()->get(RequestCondition::META_ROUTED_PATH)); + + // match `` + $constraint = RequestCondition::i()->path(''); + $this->assertTrue($constraint->match($ctx)); + $constraint->complete($ctx); + $this->assertEquals('/one/two', $ctx->meta()->get(RequestCondition::META_ROUTED_PATH)); + + // match `three` + $constraint = RequestCondition::i()->path('three'); + $this->assertTrue($constraint->match($ctx)); + $constraint->complete($ctx); + $this->assertEquals('/one/two/three', $ctx->meta()->get(RequestCondition::META_ROUTED_PATH)); + + // match EOL + $constraint = RequestCondition::i()->path('$'); + $this->assertTrue($constraint->match($ctx)); + $constraint->complete($ctx); + $this->assertEquals('/one/two/three', $ctx->meta()->get(RequestCondition::META_ROUTED_PATH)); + } }