mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-16 10:40:18 +03:00
- Updated `helpers.php` and helpers example in documentation. - MalformedUrlException is now handled properly by Router to avoid phpStorm syntax highlights in routes. - Added `getUrlCopy` to `Request` class, used to clone the current route (to keep domain etc.) - `setUrl` in `Request` are now strict and requires `Url` object and no longer accepts strings. - Renamed `hasRewrite` property to `hasPendingRewrite` in `Request` class. - Renamed `hasRewrite` and `setHasRewrite` methods to `hasPendingRewrite` and `setHasPendingRewrite` in `Request` class. - Added better usage of `Url` class. When calling `url` you can now use the methods on the `Url` class to filter params, get relative/absolute url etc. See documentation for more info. - Renamed `get` method to `getValue` in `InputHandler` class. - Renamed `getObject` to `get` and removed `$defaultValue` argument in `InputHandler` class. - Optimized `InputHandler` class. - Fixed issue with `$token` not being proper string in `BaseCsrfVerifier` when token is not found. - Added php.ini configuration settings to `setcookie` in `CookieTokenProvider` for improved security. - Added `$router` parameter to `boot` method in `IRouterBootManager` which allows for further manipulation of the router within the bootmanager. - Renamed `$processingRoute` property to `$isProcessingRoute` in `Router` class. - Fixed `reset` method not resetting CSRF-verifier in `Router` class. - Moved `arrayToParams` helper-method from `Router` to `Url` class. - Began to add Event-functionality to router. - Added `addEventHandler` method to `SimpleRouter` class. - Moved `Pecee\SimpleRouter\Handler\CallbackExceptionHandler` to `Pecee\SimpleRouter\Handlers\CallbackExceptionHandler`. - Moved `Pecee\SimpleRouter\Handler\IExceptionHandler` to `Pecee\SimpleRouter\Handlers\IExceptionHandler`. - Added Events section to documentation. - Added more information on url-handling in documentation. - Optimisations.
170 lines
4.6 KiB
PHP
170 lines
4.6 KiB
PHP
<?php
|
|
|
|
require_once 'Dummy/DummyMiddleware.php';
|
|
require_once 'Dummy/DummyController.php';
|
|
require_once 'Dummy/Exception/ExceptionHandlerException.php';
|
|
|
|
class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
protected $result = false;
|
|
|
|
public function testMultiParam()
|
|
{
|
|
TestRouter::get('/test-{param1}-{param2}', function ($param1, $param2) {
|
|
|
|
if ($param1 === 'param1' && $param2 === 'param2') {
|
|
$this->result = true;
|
|
}
|
|
|
|
});
|
|
|
|
TestRouter::debug('/test-param1-param2', 'get');
|
|
|
|
$this->assertTrue($this->result);
|
|
|
|
}
|
|
|
|
public function testNotFound()
|
|
{
|
|
$this->expectException('\Pecee\SimpleRouter\Exceptions\NotFoundHttpException');
|
|
TestRouter::get('/non-existing-path', 'DummyController@method1');
|
|
TestRouter::debug('/test-param1-param2', 'post');
|
|
}
|
|
|
|
public function testGet()
|
|
{
|
|
TestRouter::get('/my/test/url', 'DummyController@method1');
|
|
TestRouter::debug('/my/test/url', 'get');
|
|
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testPost()
|
|
{
|
|
TestRouter::post('/my/test/url', 'DummyController@method1');
|
|
TestRouter::debug('/my/test/url', 'post');
|
|
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testPut()
|
|
{
|
|
TestRouter::put('/my/test/url', 'DummyController@method1');
|
|
TestRouter::debug('/my/test/url', 'put');
|
|
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testDelete()
|
|
{
|
|
TestRouter::delete('/my/test/url', 'DummyController@method1');
|
|
TestRouter::debug('/my/test/url', 'delete');
|
|
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testMethodNotAllowed()
|
|
{
|
|
TestRouter::get('/my/test/url', 'DummyController@method1');
|
|
|
|
try {
|
|
TestRouter::debug('/my/test/url', 'post');
|
|
} catch (\Exception $e) {
|
|
$this->assertEquals(403, $e->getCode());
|
|
}
|
|
}
|
|
|
|
public function testSimpleParam()
|
|
{
|
|
TestRouter::get('/test-{param1}', 'DummyController@param');
|
|
$response = TestRouter::debugOutput('/test-param1', 'get');
|
|
|
|
$this->assertEquals('param1', $response);
|
|
}
|
|
|
|
public function testPathParamRegex()
|
|
{
|
|
TestRouter::get('/{lang}/productscategories/{name}', 'DummyController@param', ['where' => ['lang' => '[a-z]+', 'name' => '[A-Za-z0-9\-]+']]);
|
|
$response = TestRouter::debugOutput('/it/productscategories/system', 'get');
|
|
|
|
$this->assertEquals('it, system', $response);
|
|
}
|
|
|
|
public function testDomainAllowedRoute()
|
|
{
|
|
$this->result = false;
|
|
TestRouter::request()->setHost('hello.world.com');
|
|
|
|
TestRouter::group(['domain' => '{subdomain}.world.com'], function () {
|
|
TestRouter::get('/test', function ($subdomain = null) {
|
|
$this->result = ($subdomain === 'hello');
|
|
});
|
|
});
|
|
|
|
|
|
TestRouter::debug('/test', 'get');
|
|
|
|
$this->assertTrue($this->result);
|
|
|
|
}
|
|
|
|
public function testDomainNotAllowedRoute()
|
|
{
|
|
TestRouter::request()->setHost('other.world.com');
|
|
|
|
$this->result = false;
|
|
|
|
TestRouter::group(['domain' => '{subdomain}.world.com'], function () {
|
|
TestRouter::get('/test', function ($subdomain = null) {
|
|
$this->result = ($subdomain === 'hello');
|
|
});
|
|
});
|
|
|
|
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');
|
|
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testParameterDefaultValue() {
|
|
|
|
$defaultVariable = null;
|
|
|
|
TestRouter::get('/my/{path?}', function($path = 'working') use(&$defaultVariable) {
|
|
$defaultVariable = $path;
|
|
});
|
|
|
|
TestRouter::debug('/my/');
|
|
|
|
$this->assertEquals('working', $defaultVariable);
|
|
|
|
}
|
|
|
|
public function testDefaultParameterRegex()
|
|
{
|
|
TestRouter::get('/my/{path}', 'DummyController@param', ['defaultParameterRegex' => '[\w\-]+']);
|
|
$output = TestRouter::debugOutput('/my/custom-regex', 'get');
|
|
|
|
$this->assertEquals('custom-regex', $output);
|
|
}
|
|
|
|
public function testDefaultParameterRegexGroup()
|
|
{
|
|
TestRouter::group(['defaultParameterRegex' => '[\w\-]+'], function() {
|
|
TestRouter::get('/my/{path}', 'DummyController@param');
|
|
});
|
|
|
|
$output = TestRouter::debugOutput('/my/custom-regex', 'get');
|
|
|
|
$this->assertEquals('custom-regex', $output);
|
|
}
|
|
|
|
} |