mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-15 18:23:26 +03:00
[FEATURE] Added ExceptionHandling functionality + tests.
- Somehow the ExceptionHandling functionality never really get implemented and/or got lost in translation, it's back whup! - Added some basic tests for Middleware + routing.
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
.idea
|
||||
composer.lock
|
||||
composer.lock
|
||||
vendor/
|
||||
23
README.md
23
README.md
@@ -5,7 +5,7 @@ Simple, fast and yet powerful PHP router that is easy to get integrated and in a
|
||||
Add the latest version of Simple PHP Router running this command.
|
||||
|
||||
```
|
||||
composer require pecee/framework
|
||||
composer require pecee/simple-router
|
||||
```
|
||||
|
||||
## Notes
|
||||
@@ -34,7 +34,7 @@ In your ```index.php``` require your ```routes.php``` and call the ```routeReque
|
||||
This is an example of a basic ```index.php``` file:
|
||||
|
||||
```php
|
||||
use \Pecee\SimpleRouter;
|
||||
use \Pecee\SimpleRouter\SimpleRouter;
|
||||
|
||||
require_once 'routes.php'; // change this to whatever makes sense in your project
|
||||
|
||||
@@ -42,7 +42,7 @@ require_once 'routes.php'; // change this to whatever makes sense in your projec
|
||||
$defaultControllerNamespace = 'MyWebsite\\Controller';
|
||||
|
||||
// Do the routing
|
||||
SimpleRouter::init($defaultControllerNamespace);
|
||||
SimpleRouter::start($defaultControllerNamespace);
|
||||
```
|
||||
|
||||
## Adding routes
|
||||
@@ -160,23 +160,6 @@ class Router extends SimpleRouter {
|
||||
parent::start($defaultNamespace);
|
||||
} catch(\Exception $e) {
|
||||
|
||||
$route = RouterBase::getInstance()->getLoadedRoute();
|
||||
|
||||
$exceptionHandler = null;
|
||||
|
||||
// Load and use exception-handler defined on group
|
||||
|
||||
if($route && $route->getGroup()) {
|
||||
$exceptionHandler = $route->getGroup()->getExceptionHandler();
|
||||
|
||||
if($exceptionHandler !== null) {
|
||||
$class = new $exceptionHandler();
|
||||
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise use the fallback default exceptions handler
|
||||
|
||||
if(self::$defaultExceptionHandler !== null) {
|
||||
$class = new self::$defaultExceptionHandler();
|
||||
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
|
||||
|
||||
11
src/Pecee/Handler/ExceptionHandler.php
Normal file
11
src/Pecee/Handler/ExceptionHandler.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace Pecee\Handler;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
|
||||
abstract class ExceptionHandler {
|
||||
|
||||
abstract public function handleError(Request $request, RouterEntry $router = null, \Exception $error);
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
use Pecee\Exception\RouterException;
|
||||
use Pecee\Handler\ExceptionHandler;
|
||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||
use Pecee\Http\Request;
|
||||
|
||||
@@ -158,13 +159,18 @@ class RouterBase {
|
||||
$this->request->loadedRoute = $route;
|
||||
$route->loadMiddleware($this->request);
|
||||
|
||||
$this->request->loadedRoute->renderRoute($this->request);
|
||||
try {
|
||||
$this->request->loadedRoute->renderRoute($this->request);
|
||||
} catch(\Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if($routeNotAllowed) {
|
||||
throw new RouterException('Route or method not allowed', 403);
|
||||
$this->handleException(new RouterException('Route or method not allowed', 403));
|
||||
}
|
||||
|
||||
if(!$this->request->loadedRoute) {
|
||||
@@ -172,6 +178,19 @@ class RouterBase {
|
||||
}
|
||||
}
|
||||
|
||||
protected function handleException(\Exception $e) {
|
||||
if($this->request->loadedRoute !== null && $this->request->loadedRoute->exceptionHandler !== null) {
|
||||
$handler = new $this->request->loadedRoute->exceptionHandler();
|
||||
if(!($handler instanceof ExceptionHandler)) {
|
||||
throw new RouterException('Exception handler must be instanceof ExceptionHandler.');
|
||||
}
|
||||
|
||||
$handler->handleError($this->request, $this->request->loadedRoute, $e);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
||||
9
test/Dummy/DummyController.php
Normal file
9
test/Dummy/DummyController.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class DummyController {
|
||||
|
||||
public function start() {
|
||||
echo static::class . '@' .'start() OK';
|
||||
}
|
||||
|
||||
}
|
||||
14
test/Dummy/DummyMiddleware.php
Normal file
14
test/Dummy/DummyMiddleware.php
Normal file
@@ -0,0 +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) {
|
||||
throw new MiddlewareLoadedException('Middleware loaded!');
|
||||
}
|
||||
|
||||
}
|
||||
2
test/Dummy/Exceptions/MiddlewareLoadedException.php
Normal file
2
test/Dummy/Exceptions/MiddlewareLoadedException.php
Normal file
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
class MiddlewareLoadedException extends \Exception {}
|
||||
39
test/MiddlewareTest.php
Normal file
39
test/MiddlewareTest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
|
||||
class MiddlewareTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function __construct() {
|
||||
// Initial setup
|
||||
$_SERVER['HTTP_HOST'] = 'example.com';
|
||||
$_SERVER['REQUEST_URI'] = '/my/test/url';
|
||||
$_SERVER['REQUEST_METHOD'] = 'get';
|
||||
}
|
||||
|
||||
public function testMiddlewareFound() {
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
|
||||
$router = new \Pecee\SimpleRouter\RouterBase();
|
||||
|
||||
$route = new \Pecee\SimpleRouter\RouterRoute('/my/test/url', 'DummyController@start');
|
||||
$route->setRequestMethods(array(\Pecee\SimpleRouter\RouterRoute::REQUEST_TYPE_GET));
|
||||
$route->addSettings(['middleware' => 'DummyMiddleware']);
|
||||
$router->addRoute($route);
|
||||
|
||||
try {
|
||||
$router->routeRequest();
|
||||
}catch(Exception $e) {
|
||||
$this->assertTrue(($e instanceof MiddlewareLoadedException));
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception('Middleware not loaded');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
82
test/RouterRouteTest.php
Normal file
82
test/RouterRouteTest.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
|
||||
class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function __construct() {
|
||||
// Initial setup
|
||||
$_SERVER['HTTP_HOST'] = 'example.com';
|
||||
$_SERVER['REQUEST_URI'] = '/my/test/url';
|
||||
$_SERVER['REQUEST_METHOD'] = 'get';
|
||||
}
|
||||
|
||||
public function testGet() {
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
|
||||
$router = new \Pecee\SimpleRouter\RouterBase();
|
||||
|
||||
$route = new \Pecee\SimpleRouter\RouterRoute('/my/test/url', 'DummyController@start');
|
||||
$route->setRequestMethods(array(\Pecee\SimpleRouter\RouterRoute::REQUEST_TYPE_GET));
|
||||
|
||||
$router->addRoute($route);
|
||||
$router->routeRequest();
|
||||
}
|
||||
|
||||
public function testPost() {
|
||||
\Pecee\Http\Request::getInstance()->setMethod('post');
|
||||
|
||||
$router = new \Pecee\SimpleRouter\RouterBase();
|
||||
|
||||
$route = new \Pecee\SimpleRouter\RouterRoute('/my/test/url', 'DummyController@start');
|
||||
|
||||
$route->addSettings(array());
|
||||
$route->setRequestMethods(array(\Pecee\SimpleRouter\RouterRoute::REQUEST_TYPE_POST));
|
||||
|
||||
$router->addRoute($route);
|
||||
$router->routeRequest();
|
||||
}
|
||||
|
||||
public function testPut() {
|
||||
\Pecee\Http\Request::getInstance()->setMethod('put');
|
||||
|
||||
$router = new \Pecee\SimpleRouter\RouterBase();
|
||||
|
||||
$route = new \Pecee\SimpleRouter\RouterRoute('/my/test/url', 'DummyController@start');
|
||||
$route->addSettings(array());
|
||||
$route->setRequestMethods(array(\Pecee\SimpleRouter\RouterRoute::REQUEST_TYPE_PUT));
|
||||
|
||||
$router->addRoute($route);
|
||||
$router->routeRequest();
|
||||
|
||||
}
|
||||
|
||||
public function testDelete() {
|
||||
\Pecee\Http\Request::getInstance()->setMethod('delete');
|
||||
|
||||
$router = new \Pecee\SimpleRouter\RouterBase();
|
||||
|
||||
$route = new \Pecee\SimpleRouter\RouterRoute('/my/test/url', 'DummyController@start');
|
||||
$route->addSettings(array());
|
||||
$route->setRequestMethods(array(\Pecee\SimpleRouter\RouterRoute::REQUEST_TYPE_DELETE));
|
||||
|
||||
$router->addRoute($route);
|
||||
$router->routeRequest();
|
||||
|
||||
}
|
||||
|
||||
public function testMethodNotAllowed() {
|
||||
\Pecee\Http\Request::getInstance()->setMethod('post');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start');
|
||||
|
||||
try {
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
} catch(\Exception $e) {
|
||||
$this->assertEquals(403, $e->getCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user