mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
[BUGFIX] BootManager findRoute not working.
- Fixed findRoute not working in BootManager as reported by issue: #448 - Added more comprehensive php-unit tests for bootmanagers including findUrl.
This commit is contained in:
@@ -1495,7 +1495,7 @@ class CustomRouterRules implement IRouterBootManager
|
||||
|
||||
// If the current url matches the rewrite url, we use our custom route
|
||||
|
||||
if($request->getUrl()->getPath() === $url) {
|
||||
if($request->getUrl()->contains($url)) {
|
||||
$request->setRewriteUrl($rule);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,6 +269,13 @@ class Router
|
||||
{
|
||||
$this->debug('Loading routes');
|
||||
|
||||
$this->fireEvents(EventHandler::EVENT_LOAD_ROUTES, [
|
||||
'routes' => $this->routes,
|
||||
]);
|
||||
|
||||
/* Loop through each route-request */
|
||||
$this->processRoutes($this->routes);
|
||||
|
||||
$this->fireEvents(EventHandler::EVENT_BOOT, [
|
||||
'bootmanagers' => $this->bootManagers,
|
||||
]);
|
||||
@@ -291,13 +298,6 @@ class Router
|
||||
$this->debug('Finished rendering bootmanager "%s"', $className);
|
||||
}
|
||||
|
||||
$this->fireEvents(EventHandler::EVENT_LOAD_ROUTES, [
|
||||
'routes' => $this->routes,
|
||||
]);
|
||||
|
||||
/* Loop through each route-request */
|
||||
$this->processRoutes($this->routes);
|
||||
|
||||
$this->debug('Finished loading routes');
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
require_once 'Dummy/Managers/TestBootManager.php';
|
||||
require_once 'Dummy/Managers/FindUrlBootManager.php';
|
||||
|
||||
class BootManagerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testBootManagerRoutes()
|
||||
{
|
||||
$result = false;
|
||||
|
||||
TestRouter::get('/', function () use (&$result) {
|
||||
$result = true;
|
||||
});
|
||||
TestRouter::get('/about', 'DummyController@method2');
|
||||
TestRouter::get('/contact', 'DummyController@method3');
|
||||
|
||||
// Add boot-manager
|
||||
TestRouter::addBootManager(new TestBootManager([
|
||||
'/con' => '/about',
|
||||
'/contact' => '/',
|
||||
]));
|
||||
|
||||
TestRouter::debug('/contact');
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testFindUrlFromBootManager()
|
||||
{
|
||||
TestRouter::get('/', 'DummyController@method1');
|
||||
TestRouter::get('/about', 'DummyController@method2')->name('about');
|
||||
TestRouter::get('/contact', 'DummyController@method3')->name('contact');
|
||||
|
||||
$result = false;
|
||||
|
||||
// Add boot-manager
|
||||
TestRouter::addBootManager(new FindUrlBootManager($result));
|
||||
|
||||
TestRouter::debug('/');
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
class FindUrlBootManager implements \Pecee\SimpleRouter\IRouterBootManager
|
||||
{
|
||||
protected $result;
|
||||
|
||||
public function __construct(&$result)
|
||||
{
|
||||
$this->result = &$result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when router loads it's routes
|
||||
*
|
||||
* @param \Pecee\SimpleRouter\Router $router
|
||||
* @param \Pecee\Http\Request $request
|
||||
*/
|
||||
public function boot(\Pecee\SimpleRouter\Router $router, \Pecee\Http\Request $request): void
|
||||
{
|
||||
$contact = $router->findRoute('contact');
|
||||
|
||||
if($contact !== null) {
|
||||
$this->result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,11 @@
|
||||
class TestBootManager implements \Pecee\SimpleRouter\IRouterBootManager
|
||||
{
|
||||
|
||||
protected $routes;
|
||||
protected $aliasUrl;
|
||||
protected $rewrite;
|
||||
|
||||
public function __construct(array $routes, string $aliasUrl)
|
||||
public function __construct(array $rewrite)
|
||||
{
|
||||
$this->routes = $routes;
|
||||
$this->aliasUrl = $aliasUrl;
|
||||
$this->rewrite = $rewrite;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -20,11 +18,11 @@ class TestBootManager implements \Pecee\SimpleRouter\IRouterBootManager
|
||||
*/
|
||||
public function boot(\Pecee\SimpleRouter\Router $router, \Pecee\Http\Request $request): void
|
||||
{
|
||||
foreach ($this->routes as $url) {
|
||||
foreach ($this->rewrite as $url => $rewrite) {
|
||||
// If the current url matches the rewrite url, we use our custom route
|
||||
|
||||
if ($request->getUrl()->contains($url) === true) {
|
||||
$request->setRewriteUrl($this->aliasUrl);
|
||||
$request->setRewriteUrl($rewrite);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,8 +50,8 @@ class EventHandlerTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
// Add boot-manager
|
||||
TestRouter::addBootManager(new TestBootManager([
|
||||
'/test',
|
||||
], '/'));
|
||||
'/test' => '/',
|
||||
]));
|
||||
|
||||
// Start router
|
||||
TestRouter::debug('/non-existing');
|
||||
@@ -61,7 +61,6 @@ class EventHandlerTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
public function testAllEvent()
|
||||
{
|
||||
|
||||
$status = false;
|
||||
|
||||
$eventHandler = new EventHandler();
|
||||
|
||||
Reference in New Issue
Block a user