mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-15 18:23:26 +03:00
Development
- Fixed updatae causing middlewares to sometimes load on wrong routes. - Converted project to PSR/2. - Updated InputCollection class and added get method for easy access to values. - Complete refactor of RouterBase. - Added findRoute method to RouterBase. - It's now possible to change parameter modifiers and symbol by overwriting properties on RouterBase. - Added RouterUrlTest unit-test for testing route-urls. - Added IRestController that can be easily implemented in custom ResourceController-classes. - It's now possible to use "-" instead of "_" when using getHeader method in Request class. - Added PHPDocs. - Fixed "/" route sometimes returning "//" as url. - Optimisations and bugfixes.
This commit is contained in:
@@ -1,18 +1,25 @@
|
||||
<?php
|
||||
|
||||
class DummyController {
|
||||
class DummyController
|
||||
{
|
||||
public function start()
|
||||
{
|
||||
echo static::class . '@' . 'start() OK';
|
||||
}
|
||||
|
||||
public function start() {
|
||||
echo static::class . '@' .'start() OK';
|
||||
}
|
||||
public function param($params = null)
|
||||
{
|
||||
$params = func_get_args();
|
||||
echo 'Params: ' . join(', ', $params);
|
||||
}
|
||||
|
||||
public function param($params = null) {
|
||||
$params = func_get_args();
|
||||
echo 'Params: ' . join(', ', $params);
|
||||
}
|
||||
public function notFound()
|
||||
{
|
||||
echo 'not found';
|
||||
}
|
||||
|
||||
public function notFound() {
|
||||
echo 'not found';
|
||||
}
|
||||
public function silent() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
<?php
|
||||
|
||||
require_once 'Exceptions/MiddlewareLoadedException.php';
|
||||
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class DummyMiddleware implements IMiddleware {
|
||||
|
||||
public function handle(Request $request, \Pecee\SimpleRouter\RouterEntry &$route = null) {
|
||||
throw new MiddlewareLoadedException('Middleware loaded!');
|
||||
}
|
||||
class DummyMiddleware implements IMiddleware
|
||||
{
|
||||
public function handle(Request $request, \Pecee\SimpleRouter\RouterEntry &$route = null)
|
||||
{
|
||||
throw new MiddlewareLoadedException('Middleware loaded!');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,2 +1,4 @@
|
||||
<?php
|
||||
class MiddlewareLoadedException extends \Exception {}
|
||||
class MiddlewareLoadedException extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
<?php
|
||||
class ExceptionHandler implements \Pecee\Handler\IExceptionHandler {
|
||||
|
||||
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\RouterEntry &$route = null, \Exception $error){
|
||||
throw $error;
|
||||
}
|
||||
class ExceptionHandler implements \Pecee\Handler\IExceptionHandler
|
||||
{
|
||||
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\RouterEntry &$route = null, \Exception $error)
|
||||
{
|
||||
throw $error;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,79 +3,90 @@
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
|
||||
class GroupTest extends PHPUnit_Framework_TestCase {
|
||||
use Pecee\SimpleRouter\SimpleRouter as SimpleRouter;
|
||||
|
||||
protected $result;
|
||||
class GroupTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $result;
|
||||
|
||||
public function testGroupLoad() {
|
||||
public function testGroupLoad()
|
||||
{
|
||||
$this->result = false;
|
||||
|
||||
$this->result = false;
|
||||
SimpleRouter::group(['prefix' => '/group'], function () {
|
||||
$this->result = true;
|
||||
});
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/group'], function() {
|
||||
$this->result = true;
|
||||
});
|
||||
try {
|
||||
SimpleRouter::start();
|
||||
} catch (Exception $e) {
|
||||
// ignore RouteNotFound exception
|
||||
}
|
||||
|
||||
try {
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
} catch(Exception $e) {
|
||||
// ignore RouteNotFound exception
|
||||
}
|
||||
$this->assertTrue($this->result);
|
||||
}
|
||||
|
||||
$this->assertTrue($this->result);
|
||||
}
|
||||
public function testNestedGroup()
|
||||
{
|
||||
|
||||
public function testNestedGroup() {
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setUri('/api/v1/test');
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/api/v1/test');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::group(['prefix' => '/api'], function () {
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/api'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/v1'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/test', 'DummyController@start');
|
||||
});
|
||||
});
|
||||
SimpleRouter::group(['prefix' => '/v1'], function () {
|
||||
SimpleRouter::get('/test', 'DummyController@start');
|
||||
});
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
});
|
||||
|
||||
public function testManyRoutes() {
|
||||
SimpleRouter::start();
|
||||
}
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/match');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
public function testManyRoutes()
|
||||
{
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/api'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/v1'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/test', 'DummyController@start');
|
||||
});
|
||||
});
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setUri('/my/match');
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/match', 'DummyController@start');
|
||||
SimpleRouter::group(['prefix' => '/api'], function () {
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/service'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/v1'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/no-match', 'DummyController@start');
|
||||
});
|
||||
});
|
||||
SimpleRouter::group(['prefix' => '/v1'], function () {
|
||||
SimpleRouter::get('/test', 'DummyController@start');
|
||||
});
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
});
|
||||
|
||||
public function testUrls() {
|
||||
SimpleRouter::get('/my/match', 'DummyController@start');
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/fancy/url/1');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::group(['prefix' => '/service'], function () {
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']);
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setAlias('fancy2');
|
||||
SimpleRouter::group(['prefix' => '/v1'], function () {
|
||||
SimpleRouter::get('/no-match', 'DummyController@start');
|
||||
});
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
});
|
||||
|
||||
$this->assertTrue((\Pecee\SimpleRouter\SimpleRouter::getRoute('fancy1') === '/my/fancy/url/1/'));
|
||||
$this->assertTrue((\Pecee\SimpleRouter\SimpleRouter::getRoute('fancy2') === '/my/fancy/url/2/'));
|
||||
SimpleRouter::start();
|
||||
}
|
||||
|
||||
}
|
||||
public function testUrls()
|
||||
{
|
||||
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setUri('/my/fancy/url/1');
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
|
||||
SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']);
|
||||
SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setAlias('fancy2');
|
||||
|
||||
SimpleRouter::start();
|
||||
|
||||
$this->assertTrue((SimpleRouter::getRoute('fancy1') === '/my/fancy/url/1/'));
|
||||
$this->assertTrue((SimpleRouter::getRoute('fancy2') === '/my/fancy/url/2/'));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,28 +4,29 @@ require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
|
||||
class MiddlewareTest extends PHPUnit_Framework_TestCase {
|
||||
use Pecee\SimpleRouter\SimpleRouter as SimpleRouter;
|
||||
|
||||
public function testMiddlewareFound() {
|
||||
class MiddlewareTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testMiddlewareFound()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::request()->setUri('/my/test/url');
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function () {
|
||||
SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']);
|
||||
});
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']);
|
||||
});
|
||||
$found = false;
|
||||
|
||||
$found = false;
|
||||
try {
|
||||
SimpleRouter::start();
|
||||
} catch (\Exception $e) {
|
||||
$found = ($e instanceof MiddlewareLoadedException);
|
||||
}
|
||||
|
||||
try {
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}catch(\Exception $e) {
|
||||
$found = ($e instanceof MiddlewareLoadedException);
|
||||
}
|
||||
|
||||
$this->assertTrue($found);
|
||||
|
||||
}
|
||||
$this->assertTrue($found);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,135 +4,137 @@ require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
|
||||
class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
use Pecee\SimpleRouter\SimpleRouter as SimpleRouter;
|
||||
|
||||
protected $result = false;
|
||||
class RouterRouteTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $result = false;
|
||||
|
||||
public function testNotFound() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/test-param1-param2');
|
||||
public function testNotFound()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::request()->setUri('/test-param1-param2');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/non-existing-path', 'DummyController@start');
|
||||
});
|
||||
SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function () {
|
||||
SimpleRouter::get('/non-existing-path', 'DummyController@start');
|
||||
});
|
||||
|
||||
$found = false;
|
||||
$found = false;
|
||||
|
||||
try {
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}catch(\Exception $e) {
|
||||
$found = ($e instanceof \Pecee\Exception\RouterException && $e->getCode() == 404);
|
||||
}
|
||||
try {
|
||||
SimpleRouter::start();
|
||||
} catch (\Exception $e) {
|
||||
$found = ($e instanceof \Pecee\Exception\RouterException && $e->getCode() == 404);
|
||||
}
|
||||
|
||||
$this->assertTrue($found);
|
||||
$this->assertTrue($found);
|
||||
}
|
||||
|
||||
}
|
||||
public function testGet()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setUri('/my/test/url');
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
|
||||
public function testGet() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::get('/my/test/url', 'DummyController@start');
|
||||
SimpleRouter::start();
|
||||
}
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
public function testPost()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setUri('/my/test/url');
|
||||
SimpleRouter::request()->setMethod('post');
|
||||
|
||||
public function testPost() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('post');
|
||||
SimpleRouter::post('/my/test/url', 'DummyController@start');
|
||||
SimpleRouter::start();
|
||||
}
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::post('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
public function testPut()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setUri('/my/test/url');
|
||||
SimpleRouter::request()->setMethod('put');
|
||||
|
||||
public function testPut() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('put');
|
||||
SimpleRouter::put('/my/test/url', 'DummyController@start');
|
||||
SimpleRouter::start();
|
||||
}
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::put('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
public function testDelete()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setUri('/my/test/url');
|
||||
SimpleRouter::request()->setMethod('delete');
|
||||
|
||||
public function testDelete() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('delete');
|
||||
SimpleRouter::delete('/my/test/url', 'DummyController@start');
|
||||
SimpleRouter::start();
|
||||
}
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::delete('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
public function testMethodNotAllowed()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setUri('/my/test/url');
|
||||
SimpleRouter::request()->setMethod('post');
|
||||
|
||||
}
|
||||
SimpleRouter::get('/my/test/url', 'DummyController@start');
|
||||
|
||||
public function testMethodNotAllowed() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('post');
|
||||
try {
|
||||
SimpleRouter::start();
|
||||
} catch (\Exception $e) {
|
||||
$this->assertEquals(403, $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start');
|
||||
public function testSimpleParam()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::request()->setUri('/test-param1');
|
||||
|
||||
try {
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
} catch(\Exception $e) {
|
||||
$this->assertEquals(403, $e->getCode());
|
||||
}
|
||||
SimpleRouter::get('/test-{param1}', 'DummyController@param');
|
||||
SimpleRouter::start();
|
||||
}
|
||||
|
||||
}
|
||||
public function testMultiParam()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::request()->setUri('/test-param1-param2');
|
||||
|
||||
public function testSimpleParam() {
|
||||
SimpleRouter::get('/test-{param1}-{param2}', 'DummyController@param');
|
||||
SimpleRouter::start();
|
||||
}
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/test-param1');
|
||||
public function testPathParamRegex()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::request()->setUri('/test/path/123123');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/test-{param1}', 'DummyController@param');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
SimpleRouter::get('/test/path/{myParam}', 'DummyController@param', ['where' => ['myParam' => '([0-9]+)']]);
|
||||
SimpleRouter::start();
|
||||
}
|
||||
|
||||
}
|
||||
public function testDomainRoute()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::request()->setUri('/test');
|
||||
SimpleRouter::request()->setHost('hello.world.com');
|
||||
|
||||
public function testMultiParam() {
|
||||
$this->result = false;
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/test-param1-param2');
|
||||
SimpleRouter::group(['domain' => '{subdomain}.world.com'], function () {
|
||||
SimpleRouter::get('test', function ($subdomain = null) {
|
||||
$this->result = ($subdomain === 'hello');
|
||||
});
|
||||
});
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/test-{param1}-{param2}', 'DummyController@param');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
SimpleRouter::start();
|
||||
|
||||
}
|
||||
$this->assertTrue($this->result);
|
||||
|
||||
public function testPathParamRegex() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/test/path/123123');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/test/path/{myParam}', 'DummyController@param', ['where' => ['myParam' => '([0-9]+)']]);
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
|
||||
}
|
||||
|
||||
public function testDomainRoute() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/test');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setHost('hello.world.com');
|
||||
|
||||
$this->result = false;
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['domain' => '{subdomain}.world.com'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('test', function($subdomain = null) {
|
||||
$this->result = ($subdomain === 'hello');
|
||||
});
|
||||
});
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
|
||||
$this->assertTrue($this->result);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
83
test/RouterUrlTest.php
Normal file
83
test/RouterUrlTest.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
|
||||
use Pecee\SimpleRouter\SimpleRouter as SimpleRouter;
|
||||
|
||||
class RouterUrlTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $result = false;
|
||||
|
||||
protected function getUrl($controller = null, $parameters = null, $getParams = null) {
|
||||
return SimpleRouter::getRoute($controller, $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']);
|
||||
|
||||
SimpleRouter::group(['prefix' => '/admin'], function() {
|
||||
|
||||
// Match route with prefix on alias
|
||||
SimpleRouter::get('/{id?}', 'DummyController@start', ['as' => 'admin.home']);
|
||||
|
||||
// Match controller with prefix and alias
|
||||
SimpleRouter::controller('/users', 'DummyController', ['as' => 'admin.users']);
|
||||
|
||||
// Match controller with prefix and NO alias
|
||||
SimpleRouter::controller('/pages', 'DummyController');
|
||||
|
||||
});
|
||||
|
||||
// Match controller with no prefix and no alias
|
||||
SimpleRouter::controller('/cats', 'CatsController');
|
||||
|
||||
// Pretend to load page
|
||||
SimpleRouter::start();
|
||||
|
||||
// Should match /
|
||||
$this->assertEquals($this->getUrl('home'), '/');
|
||||
|
||||
// Should match /admin/
|
||||
$this->assertEquals($this->getUrl('DummyController@start'), '/admin/');
|
||||
|
||||
// Should match /admin/
|
||||
$this->assertEquals($this->getUrl('admin.home'), '/admin/');
|
||||
|
||||
// Should match /admin/2/
|
||||
$this->assertEquals($this->getUrl('admin.home', ['id' => 2]), '/admin/2/');
|
||||
|
||||
// Should match /admin/users/
|
||||
$this->assertEquals($this->getUrl('admin.users'), '/admin/users/');
|
||||
|
||||
// Should match /admin/users/home/
|
||||
$this->assertEquals($this->getUrl('admin.users@home'), '/admin/users/home/');
|
||||
|
||||
// Should match /cats/
|
||||
$this->assertEquals($this->getUrl('CatsController'), '/cats/');
|
||||
|
||||
// Should match /cats/view/
|
||||
$this->assertEquals($this->getUrl('CatsController', 'view'), '/cats/view/');
|
||||
|
||||
// Should match /cats/view/
|
||||
$this->assertEquals($this->getUrl('CatsController', ['view']), '/cats/view/');
|
||||
|
||||
// Should match /cats/view/666
|
||||
$this->assertEquals($this->getUrl('CatsController@view', ['666']), '/cats/view/666/');
|
||||
|
||||
// Should match /funny/man/
|
||||
$this->assertEquals($this->getUrl('/funny/man'), '/funny/man/');
|
||||
|
||||
// Should match /?jackdaniels=true
|
||||
$this->assertEquals($this->getUrl('home', null, ['jackdaniels' => 'true']), '/?jackdaniels=true');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user