Skip to content

Commit

Permalink
Support EOL $ on path routing (#1)
Browse files Browse the repository at this point in the history
* gitignore

* support EOL ($) on routing

* only convert strings to regex

* add circleci

* increase php require

* circle >=php72
  • Loading branch information
TomK committed Jun 19, 2019
1 parent c60d030 commit 127c170
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
42 changes: 42 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/composer.lock
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require": {
"php": ">=7.1",
"php": ">=7.2",
"packaged/context": "~1.0",
"packaged/helpers": "~2.0"
},
Expand Down
13 changes: 10 additions & 3 deletions src/RequestCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
32 changes: 31 additions & 1 deletion tests/RequestConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 127c170

Please sign in to comment.