From 9fa7ad3e9186df276c5b8e246946db21212d22eb Mon Sep 17 00:00:00 2001 From: Stefan Warnat Date: Mon, 4 Oct 2021 01:49:00 +0200 Subject: [PATCH 1/3] implement test Function to test output without reset --- tests/TestRouter.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/TestRouter.php b/tests/TestRouter.php index 1a4cfd7..636c836 100644 --- a/tests/TestRouter.php +++ b/tests/TestRouter.php @@ -48,4 +48,17 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter return $response; } + public static function debugOutputNoReset(string $testUrl, string $testMethod = 'get', bool $reset = true): string + { + $response = null; + + // Route request + ob_start(); + static::debugNoReset($testUrl, $testMethod, $reset); + $response = ob_get_clean(); + + // Return response + return $response; + } + } \ No newline at end of file From 5268a998fff86e20340161ad6117c237f365686d Mon Sep 17 00:00:00 2001 From: Stefan Warnat Date: Mon, 4 Oct 2021 01:49:42 +0200 Subject: [PATCH 2/3] Extend domain filter to support fixed subdomain and domain parameter --- src/Pecee/SimpleRouter/Route/Route.php | 7 ++- tests/Pecee/SimpleRouter/RouterRouteTest.php | 64 ++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index a8a5c37..401b1d7 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -123,7 +123,8 @@ abstract class Route implements IRoute ); // Ensures that host names/domains will work with parameters - $url = '/' . ltrim($url, '/'); + + if($route[0] == '{') $url = '/' . ltrim($url, '/'); $urlRegex = ''; $parameters = []; @@ -131,7 +132,7 @@ abstract class Route implements IRoute $urlRegex = preg_quote($route, '/'); } else { - foreach (preg_split('/((-?\/?){[^}]+})/', $route) as $key => $t) { + foreach (preg_split('/((\.?-?\/?){[^}]+})/', $route) as $key => $t) { $regex = ''; @@ -146,7 +147,7 @@ abstract class Route implements IRoute $regex = $parameterRegex ?? $this->defaultParameterRegex ?? static::PARAMETERS_DEFAULT_REGEX; } - $regex = sprintf('((\/|-)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex); + $regex = sprintf('((\/|-|\.)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex); } $urlRegex .= preg_quote($t, '/') . $regex; diff --git a/tests/Pecee/SimpleRouter/RouterRouteTest.php b/tests/Pecee/SimpleRouter/RouterRouteTest.php index 971676a..da16c56 100644 --- a/tests/Pecee/SimpleRouter/RouterRouteTest.php +++ b/tests/Pecee/SimpleRouter/RouterRouteTest.php @@ -175,6 +175,70 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase TestRouter::debug('/test', 'get'); + $this->assertFalse($result); + + } + + public function testFixedSubdomainDynamicDomain() + { + TestRouter::request()->setHost('other.world.com'); + + $result = false; + + TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) { + TestRouter::get('/test', function ($domain = null) use (&$result) { + + $result = true; + }); + }); + + TestRouter::debug('/test', 'get'); + + $this->assertTrue($result); + + } + + public function testFixedSubdomainDynamicDomainParameter() + { + TestRouter::request()->setHost('other.world.com'); + + $result = false; + + TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) { + TestRouter::get('/test', 'DummyController@param'); + TestRouter::get('/test/{key}', 'DummyController@param'); + }); + + $response = TestRouter::debugOutputnoReset('/test', 'get'); + + $this->assertEquals('world.com', $response); + + $response = TestRouter::debugOutput('/test/unittest', 'get'); + + $this->assertEquals('unittest, world.com', $response); + + } + + public function testWrongFixedSubdomainDynamicDomain() + { + TestRouter::request()->setHost('wrong.world.com'); + + $result = false; + + TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) { + TestRouter::get('/test', function ($domain = null) use (&$result) { + + $result = true; + }); + }); + + try { + TestRouter::debug('/test', 'get'); + } catch(\Exception $e) { + + } + + $this->assertFalse($result); } From 06a63eb0e700e551cdc065835f6f318a7edc26a9 Mon Sep 17 00:00:00 2001 From: Stefan Warnat Date: Mon, 4 Oct 2021 02:02:45 +0200 Subject: [PATCH 3/3] Fix Typo --- tests/Pecee/SimpleRouter/RouterRouteTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Pecee/SimpleRouter/RouterRouteTest.php b/tests/Pecee/SimpleRouter/RouterRouteTest.php index da16c56..18ab4d9 100644 --- a/tests/Pecee/SimpleRouter/RouterRouteTest.php +++ b/tests/Pecee/SimpleRouter/RouterRouteTest.php @@ -209,7 +209,7 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase TestRouter::get('/test/{key}', 'DummyController@param'); }); - $response = TestRouter::debugOutputnoReset('/test', 'get'); + $response = TestRouter::debugOutputNoReset('/test', 'get'); $this->assertEquals('world.com', $response);