From ff3f1bdcdd23af74ba749d25f5902bb9cabeb8e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sun, 5 Mar 2017 17:09:46 +0100 Subject: [PATCH] Development - Fixed issue #227 causing custom resource-routes not to be loaded after latest update. - Optimized `RouteResource` class. - Renamed `IRestController` to `IResourceController`. - Added unit-tests for RouterResource. - Simplified unit-tests with the `TestRouter` custom router class. --- ...Controller.php => IResourceController.php} | 2 +- .../SimpleRouter/Route/RouteResource.php | 21 +-- test/Dummy/DummyController.php | 21 +-- test/Dummy/Exceptions/ResponseException.php | 18 +++ ...lerFirst.php => ExceptionHandlerFirst.php} | 5 +- ...rSecond.php => ExceptionHandlerSecond.php} | 5 +- test/Dummy/Handler/ExceptionHandlerThird.php | 13 ++ .../Handler/TestExceptionHandlerThird.php | 12 -- test/Dummy/ResourceController.php | 39 +++++ test/GroupTest.php | 64 +++----- test/Helpers/TestRouter.php | 41 +++++ test/MiddlewareTest.php | 31 ++-- test/RouterResourceTest.php | 70 ++++++++ test/RouterRewriteTest.php | 60 +++++++ test/RouterRouteTest.php | 150 +++++++----------- test/RouterUrlTest.php | 77 ++++----- 16 files changed, 386 insertions(+), 243 deletions(-) rename src/Pecee/Controllers/{IRestController.php => IResourceController.php} (95%) create mode 100644 test/Dummy/Exceptions/ResponseException.php rename test/Dummy/Handler/{TestExceptionHandlerFirst.php => ExceptionHandlerFirst.php} (52%) rename test/Dummy/Handler/{TestExceptionHandlerSecond.php => ExceptionHandlerSecond.php} (53%) create mode 100644 test/Dummy/Handler/ExceptionHandlerThird.php delete mode 100644 test/Dummy/Handler/TestExceptionHandlerThird.php create mode 100644 test/Dummy/ResourceController.php create mode 100644 test/Helpers/TestRouter.php create mode 100644 test/RouterResourceTest.php create mode 100644 test/RouterRewriteTest.php diff --git a/src/Pecee/Controllers/IRestController.php b/src/Pecee/Controllers/IResourceController.php similarity index 95% rename from src/Pecee/Controllers/IRestController.php rename to src/Pecee/Controllers/IResourceController.php index aa91c22..502017f 100644 --- a/src/Pecee/Controllers/IRestController.php +++ b/src/Pecee/Controllers/IResourceController.php @@ -1,7 +1,7 @@ matchRegex($request, $url); - if ($regexMatch === false || stripos($url, $this->url) !== 0 || strtolower($url) !== strtolower($this->url)) { + if ($regexMatch === false) { return false; } $route = rtrim($this->url, '/') . '/{id?}/{action?}'; - $parameters = $this->parseParameters($route, $url); - if ($parameters === null) { + $this->parameters = $this->parseParameters($route, $url); + if ($this->parameters === null) { return false; } - $this->parameters = (array)$parameters; + $action = strtolower(trim($this->parameters['action'])); + $id = $this->parameters['id']; - $action = isset($this->parameters['action']) ? $this->parameters['action'] : null; + // Remove action parameter unset($this->parameters['action']); $method = $request->getMethod(); // Delete - if ($method === static::REQUEST_TYPE_DELETE && isset($this->parameters['id'])) { + if ($method === static::REQUEST_TYPE_DELETE && $id !== null) { return $this->call($this->methodNames['destroy']); } // Update - if (isset($this->parameters['id']) && in_array($method, [static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT], false)) { + if ($id !== null && in_array($method, [static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT], false) === true) { return $this->call($this->methodNames['update']); } // Edit - if ($method === static::REQUEST_TYPE_GET && isset($this->parameters['id']) && strtolower($action) === 'edit') { + if ($method === static::REQUEST_TYPE_GET && $id !== null && $action === 'edit') { return $this->call($this->methodNames['edit']); } // Create - if ($method === static::REQUEST_TYPE_GET && strtolower($action) === 'create') { + if ($method === static::REQUEST_TYPE_GET && $id === 'create') { return $this->call($this->methodNames['create']); } @@ -128,7 +129,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute } // Show - if ($method === static::REQUEST_TYPE_GET && isset($this->parameters['id'])) { + if ($method === static::REQUEST_TYPE_GET && $id !== null) { return $this->call($this->methodNames['show']); } diff --git a/test/Dummy/DummyController.php b/test/Dummy/DummyController.php index 77cc02b..34c0b8e 100644 --- a/test/Dummy/DummyController.php +++ b/test/Dummy/DummyController.php @@ -2,24 +2,19 @@ class DummyController { - public function start() + public function method1() { - echo static::class . '@' . 'start() OK'; + } + public function method2() + { + + } + public function param($params = null) { - $params = func_get_args(); - echo 'Params: ' . join(', ', $params); - } - - public function notFound() - { - echo 'not found'; - } - - public function silent() { - + echo join(', ', func_get_args()); } } \ No newline at end of file diff --git a/test/Dummy/Exceptions/ResponseException.php b/test/Dummy/Exceptions/ResponseException.php new file mode 100644 index 0000000..f1894b0 --- /dev/null +++ b/test/Dummy/Exceptions/ResponseException.php @@ -0,0 +1,18 @@ +response = $response; + parent::__construct('', 0); + } + + public function getResponse() + { + return $this->response; + } + +} \ No newline at end of file diff --git a/test/Dummy/Handler/TestExceptionHandlerFirst.php b/test/Dummy/Handler/ExceptionHandlerFirst.php similarity index 52% rename from test/Dummy/Handler/TestExceptionHandlerFirst.php rename to test/Dummy/Handler/ExceptionHandlerFirst.php index 2b84e33..87e4761 100644 --- a/test/Dummy/Handler/TestExceptionHandlerFirst.php +++ b/test/Dummy/Handler/ExceptionHandlerFirst.php @@ -1,10 +1,11 @@ setUri('/'); return $request; diff --git a/test/Dummy/Handler/TestExceptionHandlerSecond.php b/test/Dummy/Handler/ExceptionHandlerSecond.php similarity index 53% rename from test/Dummy/Handler/TestExceptionHandlerSecond.php rename to test/Dummy/Handler/ExceptionHandlerSecond.php index df7024c..90c18ae 100644 --- a/test/Dummy/Handler/TestExceptionHandlerSecond.php +++ b/test/Dummy/Handler/ExceptionHandlerSecond.php @@ -1,10 +1,11 @@ setUri('/'); return $request; diff --git a/test/Dummy/Handler/ExceptionHandlerThird.php b/test/Dummy/Handler/ExceptionHandlerThird.php new file mode 100644 index 0000000..15923ec --- /dev/null +++ b/test/Dummy/Handler/ExceptionHandlerThird.php @@ -0,0 +1,13 @@ +result = false; - SimpleRouter::group(['prefix' => '/group'], function () { + TestRouter::group(['prefix' => '/group'], function () { $this->result = true; }); try { - SimpleRouter::start(); - } catch (Exception $e) { - // ignore RouteNotFound exception - } + TestRouter::debug('/', 'get'); + } catch(\Exception $e) { + } $this->assertTrue($this->result); } public function testNestedGroup() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setUri('/api/v1/test'); - SimpleRouter::request()->setMethod('get'); + TestRouter::group(['prefix' => '/api'], function () { - SimpleRouter::group(['prefix' => '/api'], function () { - - SimpleRouter::group(['prefix' => '/v1'], function () { - SimpleRouter::get('/test', 'DummyController@start'); + TestRouter::group(['prefix' => '/v1'], function () { + TestRouter::get('/test', 'DummyController@method1'); }); }); - SimpleRouter::start(); + TestRouter::debug('/api/v1/test', 'get'); + } - public function testManyRoutes() + public function testMultipleRoutes() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setUri('/my/match'); - SimpleRouter::request()->setMethod('get'); + TestRouter::group(['prefix' => '/api'], function () { - SimpleRouter::group(['prefix' => '/api'], function () { - - SimpleRouter::group(['prefix' => '/v1'], function () { - SimpleRouter::get('/test', 'DummyController@start'); + TestRouter::group(['prefix' => '/v1'], function () { + TestRouter::get('/test', 'DummyController@method1'); }); }); - SimpleRouter::get('/my/match', 'DummyController@start'); + TestRouter::get('/my/match', 'DummyController@method1'); - SimpleRouter::group(['prefix' => '/service'], function () { + TestRouter::group(['prefix' => '/service'], function () { - SimpleRouter::group(['prefix' => '/v1'], function () { - SimpleRouter::get('/no-match', 'DummyController@start'); + TestRouter::group(['prefix' => '/v1'], function () { + TestRouter::get('/no-match', 'DummyController@method1'); }); }); - SimpleRouter::start(); + TestRouter::debug('/my/match', 'get'); } public function testUrls() { - - SimpleRouter::router()->reset(); - SimpleRouter::request()->setUri('/my/fancy/url/1'); - SimpleRouter::request()->setMethod('get'); - // Test array name - SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']); + TestRouter::get('/my/fancy/url/1', 'DummyController@method1', ['as' => 'fancy1']); // Test method name - SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setName('fancy2'); + TestRouter::get('/my/fancy/url/2', 'DummyController@method1')->setName('fancy2'); - SimpleRouter::start(); + TestRouter::debugNoReset('/my/fancy/url/1'); - $this->assertEquals('/my/fancy/url/1/', SimpleRouter::getUrl('fancy1')); - $this->assertEquals('/my/fancy/url/2/', SimpleRouter::getUrl('fancy2')); + $this->assertEquals('/my/fancy/url/1/', TestRouter::getUrl('fancy1')); + $this->assertEquals('/my/fancy/url/2/', TestRouter::getUrl('fancy2')); + + TestRouter::router()->reset(); } diff --git a/test/Helpers/TestRouter.php b/test/Helpers/TestRouter.php new file mode 100644 index 0000000..5db693b --- /dev/null +++ b/test/Helpers/TestRouter.php @@ -0,0 +1,41 @@ +setUri($testUri); + static::request()->setMethod($testMethod); + + static::start(); + } + + public static function debug($testUri, $testMethod = 'get') + { + try { + static::debugNoReset($testUri, $testMethod); + } catch(\Exception $e) { + static::router()->reset(); + throw $e; + } + + static::router()->reset(); + + } + + public static function debugOutput($testUri, $testMethod = 'get') + { + $response = null; + + // Route request + ob_start(); + static::debug($testUri, $testMethod); + $response = ob_get_contents(); + ob_end_clean(); + + // Return response + return $response; + } + +} \ No newline at end of file diff --git a/test/MiddlewareTest.php b/test/MiddlewareTest.php index 3b53270..d3c4dba 100644 --- a/test/MiddlewareTest.php +++ b/test/MiddlewareTest.php @@ -3,39 +3,32 @@ require_once 'Dummy/DummyMiddleware.php'; require_once 'Dummy/DummyController.php'; require_once 'Dummy/Handler/ExceptionHandler.php'; - -use Pecee\SimpleRouter\SimpleRouter as SimpleRouter; +require_once 'Helpers/TestRouter.php'; class MiddlewareTest extends PHPUnit_Framework_TestCase { public function testMiddlewareFound() { - $this->setExpectedException('MiddlewareLoadedException'); + $this->setExpectedException(MiddlewareLoadedException::class); - SimpleRouter::router()->reset(); - SimpleRouter::request()->setMethod('get'); - SimpleRouter::request()->setUri('/my/test/url'); - - SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function () { - SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']); + TestRouter::group(['exceptionHandler' => 'ExceptionHandler'], function () { + TestRouter::get('/my/test/url', 'DummyController@method1', ['middleware' => 'DummyMiddleware']); }); - SimpleRouter::start(); + TestRouter::debug('/my/test/url', 'get'); + } - public function testNestedMiddlewareLoad() + public function testNestedMiddlewareDontLoad() { - $this->setExpectedException('MiddlewareLoadedException'); - SimpleRouter::router()->reset(); - SimpleRouter::request()->setMethod('get'); - SimpleRouter::request()->setUri('/my/test/url'); - - SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler', 'middleware' => 'DummyMiddleware'], function () { - SimpleRouter::get('/my/test/url', 'DummyController@start'); + TestRouter::group(['exceptionHandler' => 'ExceptionHandler', 'middleware' => 'DummyMiddleware'], function () { + TestRouter::get('/middleware', 'DummyController@method1'); }); - SimpleRouter::start(); + TestRouter::get('/my/test/url', 'DummyController@method1'); + + TestRouter::debug('/my/test/url', 'get'); } } \ No newline at end of file diff --git a/test/RouterResourceTest.php b/test/RouterResourceTest.php new file mode 100644 index 0000000..976f071 --- /dev/null +++ b/test/RouterResourceTest.php @@ -0,0 +1,70 @@ +assertEquals('store', $response); + } + + public function testResourceCreate() + { + TestRouter::resource('/resource', 'ResourceController'); + $response = TestRouter::debugOutput('/resource/create', 'get'); + + $this->assertEquals('create', $response); + + } + + public function testResourceIndex() + { + TestRouter::resource('/resource', 'ResourceController'); + $response = TestRouter::debugOutput('/resource', 'get'); + + $this->assertEquals('index', $response); + } + + public function testResourceDestroy() + { + TestRouter::resource('/resource', 'ResourceController'); + $response = TestRouter::debugOutput('/resource/38', 'delete'); + + $this->assertEquals('destroy 38', $response); + } + + + public function testResourceEdit() + { + TestRouter::resource('/resource', 'ResourceController'); + $response = TestRouter::debugOutput('/resource/38/edit', 'get'); + + $this->assertEquals('edit 38', $response); + + } + + public function testResourceUpdate() + { + TestRouter::resource('/resource', 'ResourceController'); + $response = TestRouter::debugOutput('/resource/38', 'put'); + + $this->assertEquals('update 38', $response); + + } + + public function testResourceGet() + { + TestRouter::resource('/resource', 'ResourceController'); + $response = TestRouter::debugOutput('/resource/38', 'get'); + + $this->assertEquals('show 38', $response); + + } + +} \ No newline at end of file diff --git a/test/RouterRewriteTest.php b/test/RouterRewriteTest.php new file mode 100644 index 0000000..176ad39 --- /dev/null +++ b/test/RouterRewriteTest.php @@ -0,0 +1,60 @@ + [ExceptionHandlerFirst::class, ExceptionHandlerSecond::class]], function () { + + TestRouter::group(['exceptionHandler' => ExceptionHandlerThird::class], function () { + + TestRouter::get('/my-path', 'DummyController@method1'); + + }); + }); + + try { + TestRouter::debug('/my-non-existing-path', 'get'); + } catch(\ResponseException $e) { + + } + + $expectedStack = [ + ExceptionHandlerFirst::class, + ExceptionHandlerSecond::class, + ExceptionHandlerThird::class, + ]; + + $this->assertEquals($expectedStack, $stack); + + } + +} \ No newline at end of file diff --git a/test/RouterRouteTest.php b/test/RouterRouteTest.php index 5637f46..ae045ee 100644 --- a/test/RouterRouteTest.php +++ b/test/RouterRouteTest.php @@ -3,12 +3,7 @@ require_once 'Dummy/DummyMiddleware.php'; require_once 'Dummy/DummyController.php'; require_once 'Dummy/Exceptions/ExceptionHandlerException.php'; -require_once 'Dummy/Handler/TestExceptionHandlerFirst.php'; -require_once 'Dummy/Handler/TestExceptionHandlerSecond.php'; -require_once 'Dummy/Handler/TestExceptionHandlerThird.php'; - -use Pecee\SimpleRouter\Exceptions\NotFoundHttpException as NotFoundHttpException; -use Pecee\SimpleRouter\SimpleRouter as SimpleRouter; +require_once 'Helpers/TestRouter.php'; class RouterRouteTest extends PHPUnit_Framework_TestCase { @@ -16,113 +11,57 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase public function testMultiParam() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setMethod('get'); - SimpleRouter::request()->setUri('/test-param1-param2'); + TestRouter::get('/test-{param1}-{param2}', function ($param1, $param2) { - SimpleRouter::get('/test-{param1}-{param2}', function($param1, $param2) { - - if($param1 === 'param1' && $param2 === 'param2') { + if ($param1 === 'param1' && $param2 === 'param2') { $this->result = true; } }); - SimpleRouter::start(); + TestRouter::debug('/test-param1-param2', 'get'); $this->assertTrue($this->result); } - /** - * Redirects to another route through 3 exception handlers. - * - * You will see "ExceptionHandler 1 loaded" 2 times. This happen because - * the exceptionhandler is asking the router to reload. - * - * That means that the exceptionhandler is loaded again, but this time - * the router ignores the same rewrite-route to avoid loop - loads - * the second which have same behavior and is also ignored before - * throwing the final Exception in ExceptionHandler 3. - * - * So this tests: - * 1. If ExceptionHandlers loads - * 2. If ExceptionHandlers load in the correct order - * 3. If ExceptionHandlers can rewrite the page on error - * 4. If the router can avoid redirect-loop due to developer has started loop. - * 5. And finally if we reaches the last exception-handler and that the correct - * exception-type is being thrown. - */ public function testNotFound() { - $this->setExpectedException('ExceptionHandlerException'); - - SimpleRouter::router()->reset(); - SimpleRouter::request()->setMethod('get'); - SimpleRouter::request()->setUri('/test-param1-param2'); - - SimpleRouter::group(['exceptionHandler' => ['TestExceptionHandlerFirst', 'TestExceptionHandlerSecond']], function () { - - SimpleRouter::group(['exceptionHandler' => 'TestExceptionHandlerThird'], function () { - - SimpleRouter::get('/non-existing-path', 'DummyController@start'); - - }); - }); - - SimpleRouter::start(); + $this->setExpectedException('\Pecee\SimpleRouter\Exceptions\NotFoundHttpException'); + TestRouter::get('/non-existing-path', 'DummyController@method1'); + TestRouter::debug('/test-param1-param2', 'post'); } public function testGet() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setUri('/my/test/url'); - SimpleRouter::request()->setMethod('get'); - - SimpleRouter::get('/my/test/url', 'DummyController@start'); - SimpleRouter::start(); + TestRouter::get('/my/test/url', 'DummyController@method1'); + TestRouter::debug('/my/test/url', 'get'); } public function testPost() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setUri('/my/test/url'); - SimpleRouter::request()->setMethod('post'); - - SimpleRouter::post('/my/test/url', 'DummyController@start'); - SimpleRouter::start(); + TestRouter::post('/my/test/url', 'DummyController@method1'); + TestRouter::debug('/my/test/url', 'post'); } public function testPut() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setUri('/my/test/url'); - SimpleRouter::request()->setMethod('put'); - - SimpleRouter::put('/my/test/url', 'DummyController@start'); - SimpleRouter::start(); + TestRouter::put('/my/test/url', 'DummyController@method1'); + TestRouter::debug('/my/test/url', 'put'); } public function testDelete() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setUri('/my/test/url'); - SimpleRouter::request()->setMethod('delete'); - - SimpleRouter::delete('/my/test/url', 'DummyController@start'); - SimpleRouter::start(); + TestRouter::delete('/my/test/url', 'DummyController@method1'); + TestRouter::debug('/my/test/url', 'delete'); } public function testMethodNotAllowed() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setUri('/my/test/url'); - SimpleRouter::request()->setMethod('post'); - - SimpleRouter::get('/my/test/url', 'DummyController@start'); + TestRouter::get('/my/test/url', 'DummyController@method1'); try { - SimpleRouter::start(); + TestRouter::debug('/my/test/url', 'post'); } catch (\Exception $e) { $this->assertEquals(403, $e->getCode()); } @@ -130,43 +69,60 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase public function testSimpleParam() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setMethod('get'); - SimpleRouter::request()->setUri('/test-param1'); + TestRouter::get('/test-{param1}', 'DummyController@param'); + $response = TestRouter::debugOutput('/test-param1', 'get'); - SimpleRouter::get('/test-{param1}', 'DummyController@param'); - SimpleRouter::start(); + $this->assertEquals('param1', $response); } public function testPathParamRegex() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setMethod('get'); - SimpleRouter::request()->setUri('/test/path/123123'); + TestRouter::get('/test/path/{myParam}', 'DummyController@param', ['where' => ['myParam' => '([0-9]+)']]); + $response = TestRouter::debugOutput('/test/path/123123', 'get'); - SimpleRouter::get('/test/path/{myParam}', 'DummyController@param', ['where' => ['myParam' => '([0-9]+)']]); - SimpleRouter::start(); + $this->assertEquals('123123', $response); } - public function testDomainRoute() + public function testDomainAllowedRoute() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setMethod('get'); - SimpleRouter::request()->setUri('/test'); - SimpleRouter::request()->setHost('hello.world.com'); - $this->result = false; - SimpleRouter::group(['domain' => '{subdomain}.world.com'], function () { - SimpleRouter::get('/test', function ($subdomain = null) { + TestRouter::group(['domain' => '{subdomain}.world.com'], function () { + TestRouter::get('/test', function ($subdomain = null) { $this->result = ($subdomain === 'hello'); }); }); - SimpleRouter::start(); + TestRouter::request()->setHost('hello.world.com'); + TestRouter::debug('/test', 'get'); $this->assertTrue($this->result); } + public function testDomainNotAllowedRoute() + { + $this->result = false; + + TestRouter::group(['domain' => '{subdomain}.world.com'], function () { + TestRouter::get('/test', function ($subdomain = null) { + $this->result = ($subdomain === 'hello'); + }); + }); + + TestRouter::request()->setHost('other.world.com'); + + + TestRouter::debug('/test', 'get'); + + $this->assertFalse($this->result); + + } + + public function testRegEx() + { + TestRouter::get('/my/{path}', 'DummyController@method1')->where(['path' => '[a-zA-Z\-]+']); + TestRouter::debug('/my/custom-path', 'get'); + } + } \ No newline at end of file diff --git a/test/RouterUrlTest.php b/test/RouterUrlTest.php index 6c2357f..8d872ce 100644 --- a/test/RouterUrlTest.php +++ b/test/RouterUrlTest.php @@ -3,111 +3,90 @@ require_once 'Dummy/DummyMiddleware.php'; require_once 'Dummy/DummyController.php'; require_once 'Dummy/Handler/ExceptionHandler.php'; - -use Pecee\SimpleRouter\SimpleRouter as SimpleRouter; +require_once 'Helpers/TestRouter.php'; class RouterUrlTest extends PHPUnit_Framework_TestCase { - protected $result = false; - - protected function getUrl($name = null, $parameters = null, array $getParams = []) - { - return SimpleRouter::getUrl($name, $parameters, $getParams); - } public function testUrls() { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setMethod('get'); - SimpleRouter::request()->setUri('/'); - // Match normal route on alias - SimpleRouter::get('/', 'DummyController@silent', ['as' => 'home']); + TestRouter::get('/', 'DummyController@method1', ['as' => 'home']); - SimpleRouter::get('/about', 'DummyController@about'); + TestRouter::get('/about', 'DummyController@about'); - SimpleRouter::group(['prefix' => '/admin', 'as' => 'admin'], function () { + TestRouter::group(['prefix' => '/admin', 'as' => 'admin'], function () { // Match route with prefix on alias - SimpleRouter::get('/{id?}', 'DummyController@start', ['as' => 'home']); + TestRouter::get('/{id?}', 'DummyController@method2', ['as' => 'home']); // Match controller with prefix and alias - SimpleRouter::controller('/users', 'DummyController', ['as' => 'users']); + TestRouter::controller('/users', 'DummyController', ['as' => 'users']); // Match controller with prefix and NO alias - SimpleRouter::controller('/pages', 'DummyController'); + TestRouter::controller('/pages', 'DummyController'); }); - SimpleRouter::group(['prefix' => 'api', 'as' => 'api'], function () { + TestRouter::group(['prefix' => 'api', 'as' => 'api'], function () { // Match resource controller - SimpleRouter::resource('phones', 'DummyController'); + TestRouter::resource('phones', 'DummyController'); }); - SimpleRouter::controller('gadgets', 'DummyController', ['names' => ['getIphoneInfo' => 'iphone']]); + TestRouter::controller('gadgets', 'DummyController', ['names' => ['getIphoneInfo' => 'iphone']]); // Match controller with no prefix and no alias - SimpleRouter::controller('/cats', 'CatsController'); + TestRouter::controller('/cats', 'CatsController'); // Pretend to load page - SimpleRouter::start(); + TestRouter::debugNoReset('/', 'get'); - $this->assertEquals('/gadgets/iphoneinfo/', $this->getUrl('gadgets.iphone')); + $this->assertEquals('/gadgets/iphoneinfo/', TestRouter::getUrl('gadgets.iphone')); - $this->assertEquals('/api/phones/create/', $this->getUrl('api.phones.create')); + $this->assertEquals('/api/phones/create/', TestRouter::getUrl('api.phones.create')); // Should match / - $this->assertEquals('/', $this->getUrl('home')); + $this->assertEquals('/', TestRouter::getUrl('home')); // Should match /about/ - $this->assertEquals('/about/', $this->getUrl('DummyController@about')); + $this->assertEquals('/about/', TestRouter::getUrl('DummyController@about')); // Should match /admin/ - $this->assertEquals('/admin/', $this->getUrl('DummyController@start')); + $this->assertEquals('/admin/', TestRouter::getUrl('DummyController@method2')); // Should match /admin/ - $this->assertEquals('/admin/', $this->getUrl('admin.home')); + $this->assertEquals('/admin/', TestRouter::getUrl('admin.home')); // Should match /admin/2/ - $this->assertEquals('/admin/2/', $this->getUrl('admin.home', ['id' => 2])); + $this->assertEquals('/admin/2/', TestRouter::getUrl('admin.home', ['id' => 2])); // Should match /admin/users/ - $this->assertEquals('/admin/users/', $this->getUrl('admin.users')); + $this->assertEquals('/admin/users/', TestRouter::getUrl('admin.users')); // Should match /admin/users/home/ - $this->assertEquals('/admin/users/home/', $this->getUrl('admin.users@home')); + $this->assertEquals('/admin/users/home/', TestRouter::getUrl('admin.users@home')); // Should match /cats/ - $this->assertEquals('/cats/', $this->getUrl('CatsController')); + $this->assertEquals('/cats/', TestRouter::getUrl('CatsController')); // Should match /cats/view/ - $this->assertEquals('/cats/view/', $this->getUrl('CatsController', 'view')); + $this->assertEquals('/cats/view/', TestRouter::getUrl('CatsController', 'view')); // Should match /cats/view/ - //$this->assertEquals('/cats/view/', $this->getUrl('CatsController', ['view'])); + //$this->assertEquals('/cats/view/', TestRouter::getUrl('CatsController', ['view'])); // Should match /cats/view/666 - $this->assertEquals('/cats/view/666/', $this->getUrl('CatsController@getView', ['666'])); + $this->assertEquals('/cats/view/666/', TestRouter::getUrl('CatsController@getView', ['666'])); // Should match /funny/man/ - $this->assertEquals('/funny/man/', $this->getUrl('/funny/man')); + $this->assertEquals('/funny/man/', TestRouter::getUrl('/funny/man')); // Should match /?jackdaniels=true&cola=yeah - $this->assertEquals('/?jackdaniels=true&cola=yeah', $this->getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah'])); + $this->assertEquals('/?jackdaniels=true&cola=yeah', TestRouter::getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah'])); - } - - public function testRegEx() - { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setMethod('get'); - SimpleRouter::request()->setUri('/my/custom-path'); - - SimpleRouter::get('/my/{path}', 'DummyController@start')->where(['path' => '[a-zA-Z\-]+']); - - SimpleRouter::start(); + TestRouter::router()->reset(); }