|
|
|
@ -6,39 +6,37 @@
|
|
|
|
|
* @license MIT, http://flightphp.com/license
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use flight\core\Dispatcher;
|
|
|
|
|
use flight\net\Request;
|
|
|
|
|
use flight\net\Router;
|
|
|
|
|
|
|
|
|
|
require_once 'vendor/autoload.php';
|
|
|
|
|
require_once __DIR__.'/../flight/autoload.php';
|
|
|
|
|
require_once __DIR__ . '/../flight/autoload.php';
|
|
|
|
|
|
|
|
|
|
class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
class RouterTest extends PHPUnit\Framework\TestCase
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var \flight\net\Router
|
|
|
|
|
*/
|
|
|
|
|
private $router;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var \flight\net\Request
|
|
|
|
|
*/
|
|
|
|
|
private $request;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var \flight\core\Dispatcher
|
|
|
|
|
*/
|
|
|
|
|
private $dispatcher;
|
|
|
|
|
|
|
|
|
|
function setUp(){
|
|
|
|
|
$this->router = new \flight\net\Router();
|
|
|
|
|
$this->request = new \flight\net\Request();
|
|
|
|
|
$this->dispatcher = new \flight\core\Dispatcher();
|
|
|
|
|
private Router $router;
|
|
|
|
|
|
|
|
|
|
private Request $request;
|
|
|
|
|
|
|
|
|
|
private Dispatcher $dispatcher;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
$this->router = new Router();
|
|
|
|
|
$this->request = new Request();
|
|
|
|
|
$this->dispatcher = new Dispatcher();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Simple output
|
|
|
|
|
function ok(){
|
|
|
|
|
public function ok()
|
|
|
|
|
{
|
|
|
|
|
echo 'OK';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checks if a route was matched with a given output
|
|
|
|
|
function check($str = '') {
|
|
|
|
|
public function check($str = '')
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
$route = $this->router->route($this->request);
|
|
|
|
|
|
|
|
|
@ -53,7 +51,8 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
$this->expectOutputString($str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function routeRequest() {
|
|
|
|
|
public function routeRequest()
|
|
|
|
|
{
|
|
|
|
|
$dispatched = false;
|
|
|
|
|
|
|
|
|
|
while ($route = $this->router->route($this->request)) {
|
|
|
|
@ -70,7 +69,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
|
|
|
|
|
$dispatched = true;
|
|
|
|
|
|
|
|
|
|
if (!$continue) break;
|
|
|
|
|
if (!$continue) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->router->next();
|
|
|
|
|
|
|
|
|
@ -83,24 +84,27 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Default route
|
|
|
|
|
function testDefaultRoute(){
|
|
|
|
|
$this->router->map('/', array($this, 'ok'));
|
|
|
|
|
public function testDefaultRoute()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/', [$this, 'ok']);
|
|
|
|
|
$this->request->url = '/';
|
|
|
|
|
|
|
|
|
|
$this->check('OK');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Simple path
|
|
|
|
|
function testPathRoute(){
|
|
|
|
|
$this->router->map('/path', array($this, 'ok'));
|
|
|
|
|
public function testPathRoute()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/path', [$this, 'ok']);
|
|
|
|
|
$this->request->url = '/path';
|
|
|
|
|
|
|
|
|
|
$this->check('OK');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST route
|
|
|
|
|
function testPostRoute(){
|
|
|
|
|
$this->router->map('POST /', array($this, 'ok'));
|
|
|
|
|
public function testPostRoute()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('POST /', [$this, 'ok']);
|
|
|
|
|
$this->request->url = '/';
|
|
|
|
|
$this->request->method = 'POST';
|
|
|
|
|
|
|
|
|
@ -108,8 +112,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Either GET or POST route
|
|
|
|
|
function testGetPostRoute(){
|
|
|
|
|
$this->router->map('GET|POST /', array($this, 'ok'));
|
|
|
|
|
public function testGetPostRoute()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('GET|POST /', [$this, 'ok']);
|
|
|
|
|
$this->request->url = '/';
|
|
|
|
|
$this->request->method = 'GET';
|
|
|
|
|
|
|
|
|
@ -117,16 +122,18 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test regular expression matching
|
|
|
|
|
function testRegEx(){
|
|
|
|
|
$this->router->map('/num/[0-9]+', array($this, 'ok'));
|
|
|
|
|
public function testRegEx()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/num/[0-9]+', [$this, 'ok']);
|
|
|
|
|
$this->request->url = '/num/1234';
|
|
|
|
|
|
|
|
|
|
$this->check('OK');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Passing URL parameters
|
|
|
|
|
function testUrlParameters(){
|
|
|
|
|
$this->router->map('/user/@id', function($id){
|
|
|
|
|
public function testUrlParameters()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/user/@id', function ($id) {
|
|
|
|
|
echo $id;
|
|
|
|
|
});
|
|
|
|
|
$this->request->url = '/user/123';
|
|
|
|
@ -135,8 +142,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Passing URL parameters matched with regular expression
|
|
|
|
|
function testRegExParameters(){
|
|
|
|
|
$this->router->map('/test/@name:[a-z]+', function($name){
|
|
|
|
|
public function testRegExParameters()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/test/@name:[a-z]+', function ($name) {
|
|
|
|
|
echo $name;
|
|
|
|
|
});
|
|
|
|
|
$this->request->url = '/test/abc';
|
|
|
|
@ -145,8 +153,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Optional parameters
|
|
|
|
|
function testOptionalParameters(){
|
|
|
|
|
$this->router->map('/blog(/@year(/@month(/@day)))', function($year, $month, $day){
|
|
|
|
|
public function testOptionalParameters()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/blog(/@year(/@month(/@day)))', function ($year, $month, $day) {
|
|
|
|
|
echo "$year,$month,$day";
|
|
|
|
|
});
|
|
|
|
|
$this->request->url = '/blog/2000';
|
|
|
|
@ -155,8 +164,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Regex in optional parameters
|
|
|
|
|
function testRegexOptionalParameters(){
|
|
|
|
|
$this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){
|
|
|
|
|
public function testRegexOptionalParameters()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/@controller/@method(/@id:[0-9]+)', function ($controller, $method, $id) {
|
|
|
|
|
echo "$controller,$method,$id";
|
|
|
|
|
});
|
|
|
|
|
$this->request->url = '/user/delete/123';
|
|
|
|
@ -165,8 +175,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Regex in optional parameters
|
|
|
|
|
function testRegexEmptyOptionalParameters(){
|
|
|
|
|
$this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){
|
|
|
|
|
public function testRegexEmptyOptionalParameters()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/@controller/@method(/@id:[0-9]+)', function ($controller, $method, $id) {
|
|
|
|
|
echo "$controller,$method,$id";
|
|
|
|
|
});
|
|
|
|
|
$this->request->url = '/user/delete/';
|
|
|
|
@ -175,39 +186,42 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wildcard matching
|
|
|
|
|
function testWildcard(){
|
|
|
|
|
$this->router->map('/account/*', array($this, 'ok'));
|
|
|
|
|
public function testWildcard()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/account/*', [$this, 'ok']);
|
|
|
|
|
$this->request->url = '/account/123/abc/xyz';
|
|
|
|
|
|
|
|
|
|
$this->check('OK');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if route object was passed
|
|
|
|
|
function testRouteObjectPassing(){
|
|
|
|
|
$this->router->map('/yes_route', function($route){
|
|
|
|
|
$this->assertTrue(is_object($route));
|
|
|
|
|
$this->assertTrue(is_array($route->methods));
|
|
|
|
|
$this->assertTrue(is_array($route->params));
|
|
|
|
|
$this->assertEquals(sizeof($route->params), 0);
|
|
|
|
|
$this->assertEquals($route->regex, null);
|
|
|
|
|
$this->assertEquals($route->splat, '');
|
|
|
|
|
public function testRouteObjectPassing()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/yes_route', function ($route) {
|
|
|
|
|
$this->assertIsObject($route);
|
|
|
|
|
$this->assertIsArray($route->methods);
|
|
|
|
|
$this->assertIsArray($route->params);
|
|
|
|
|
$this->assertCount(0, $route->params);
|
|
|
|
|
$this->assertNull($route->regex);
|
|
|
|
|
$this->assertEquals('', $route->splat);
|
|
|
|
|
$this->assertTrue($route->pass);
|
|
|
|
|
}, true);
|
|
|
|
|
$this->request->url = '/yes_route';
|
|
|
|
|
|
|
|
|
|
$this->check();
|
|
|
|
|
|
|
|
|
|
$this->router->map('/no_route', function($route = null){
|
|
|
|
|
$this->assertTrue(is_null($route));
|
|
|
|
|
$this->router->map('/no_route', function ($route = null) {
|
|
|
|
|
$this->assertNull($route);
|
|
|
|
|
}, false);
|
|
|
|
|
$this->request->url = '/no_route';
|
|
|
|
|
|
|
|
|
|
$this->check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testRouteWithParameters() {
|
|
|
|
|
$this->router->map('/@one/@two', function($one, $two, $route){
|
|
|
|
|
$this->assertEquals(sizeof($route->params), 2);
|
|
|
|
|
public function testRouteWithParameters()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/@one/@two', function ($one, $two, $route) {
|
|
|
|
|
$this->assertCount(2, $route->params);
|
|
|
|
|
$this->assertEquals($route->params['one'], $one);
|
|
|
|
|
$this->assertEquals($route->params['two'], $two);
|
|
|
|
|
}, true);
|
|
|
|
@ -217,8 +231,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test splat
|
|
|
|
|
function testSplatWildcard(){
|
|
|
|
|
$this->router->map('/account/*', function($route){
|
|
|
|
|
public function testSplatWildcard()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/account/*', function ($route) {
|
|
|
|
|
echo $route->splat;
|
|
|
|
|
}, true);
|
|
|
|
|
$this->request->url = '/account/456/def/xyz';
|
|
|
|
@ -227,8 +242,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test splat without trailing slash
|
|
|
|
|
function testSplatWildcardTrailingSlash(){
|
|
|
|
|
$this->router->map('/account/*', function($route){
|
|
|
|
|
public function testSplatWildcardTrailingSlash()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/account/*', function ($route) {
|
|
|
|
|
echo $route->splat;
|
|
|
|
|
}, true);
|
|
|
|
|
$this->request->url = '/account';
|
|
|
|
@ -237,42 +253,44 @@ class RouterTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test splat with named parameters
|
|
|
|
|
function testSplatNamedPlusWildcard(){
|
|
|
|
|
$this->router->map('/account/@name/*', function($name, $route){
|
|
|
|
|
echo $route->splat;
|
|
|
|
|
$this->assertEquals('abc', $name);
|
|
|
|
|
}, true);
|
|
|
|
|
public function testSplatNamedPlusWildcard()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/account/@name/*', function ($name, $route) {
|
|
|
|
|
echo $route->splat;
|
|
|
|
|
$this->assertEquals('abc', $name);
|
|
|
|
|
}, true);
|
|
|
|
|
$this->request->url = '/account/abc/456/def/xyz';
|
|
|
|
|
|
|
|
|
|
$this->check('456/def/xyz');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test not found
|
|
|
|
|
function testNotFound() {
|
|
|
|
|
$this->router->map('/does_exist', array($this, 'ok'));
|
|
|
|
|
public function testNotFound()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/does_exist', [$this, 'ok']);
|
|
|
|
|
$this->request->url = '/does_not_exist';
|
|
|
|
|
|
|
|
|
|
$this->check('404');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test case sensitivity
|
|
|
|
|
function testCaseSensitivity() {
|
|
|
|
|
$this->router->map('/hello', array($this, 'ok'));
|
|
|
|
|
public function testCaseSensitivity()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/hello', [$this, 'ok']);
|
|
|
|
|
$this->request->url = '/HELLO';
|
|
|
|
|
$this->router->case_sensitive = true;
|
|
|
|
|
|
|
|
|
|
$this->check('404');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Passing URL parameters matched with regular expression for a URL containing Cyrillic letters:
|
|
|
|
|
function testRegExParametersCyrillic(){
|
|
|
|
|
$this->router->map('/категория/@name:[абвгдеёжзийклмнопрстуфхцчшщъыьэюя]+', function($name){
|
|
|
|
|
public function testRegExParametersCyrillic()
|
|
|
|
|
{
|
|
|
|
|
$this->router->map('/категория/@name:[абвгдеёжзийклмнопрстуфхцчшщъыьэюя]+', function ($name) {
|
|
|
|
|
echo $name;
|
|
|
|
|
});
|
|
|
|
|
$this->request->url = urlencode('/категория/цветя');
|
|
|
|
|
|
|
|
|
|
$this->check('цветя');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|