Skip to content

Commit

Permalink
load routes in RoutingMiddleware just if not initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
batopa committed Sep 6, 2017
1 parent fd6ed92 commit 2081daf
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Routing/Middleware/RoutingMiddleware.php
Expand Up @@ -48,7 +48,7 @@ public function __construct(BaseApplication $app = null)
}

/**
* Trigger the application's routes() hook if the application exists.
* Trigger the application's routes() hook if the application exists and Router isn't initialized.
*
* If the middleware is created without an Application, routes will be
* loaded via the automatic route loading that pre-dates the routes() hook.
Expand All @@ -57,7 +57,7 @@ public function __construct(BaseApplication $app = null)
*/
protected function loadRoutes()
{
if ($this->app) {
if ($this->app && !Router::$initialized) {
$builder = Router::createRouteBuilder('/');
$this->app->routes($builder);
// Prevent routes from being loaded again
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase\TestSuite;

use Cake\Core\Configure;
use Cake\Event\EventManager;
use Cake\Http\Response;
use Cake\Routing\DispatcherFactory;
Expand Down Expand Up @@ -189,6 +190,24 @@ public function testGet()
$this->assertEquals('This is a test', $this->_response->getBody());
}

/**
* Test sending get request and using default `test_app/config/routes.php`.
*
* @return void
*/
public function testGetUsingApplicationWithDefaultRoutes()
{
// first clean routes to have Router::$initailized === false
Router::reload();

$this->useHttpServer(true);
$this->configApplication(Configure::read('App.namespace') . '\ApplicationWithDefaultRoutes', null);

$this->get('/some_alias');
$this->assertResponseOk();
$this->assertEquals('5', $this->_getBodyAsString());
}

/**
* Test sending head requests.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/test_app/TestApp/ApplicationWithDefaultRoutes.php
@@ -0,0 +1,48 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.5.2
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace TestApp;

use Cake\Http\BaseApplication;
use Cake\Routing\Middleware\RoutingMiddleware;

/**
* Simple Application class doing nothing that:
*
* - do nothing in bootstrap
* - add just `RoutingMiddleware` to middleware queue
*
* Useful to test app using the default `BaseApplication::routes()` method
*/
class ApplicationWithDefaultRoutes extends BaseApplication
{
/**
* Bootstrap hook.
*
* Nerfed as this is for IntegrationTestCase testing.
*
* @return void
*/
public function bootstrap()
{
// Do nothing.
}

public function middleware($middlewareQueue)
{
$middlewareQueue->add(new RoutingMiddleware($this));

return $middlewareQueue;
}
}
11 changes: 10 additions & 1 deletion tests/test_app/config/routes.php
Expand Up @@ -17,6 +17,15 @@
Router::extensions('json');
Router::scope('/', function ($routes) {
$routes->connect('/', ['controller' => 'pages', 'action' => 'display', 'home']);
$routes->connect('/some_alias', ['controller' => 'tests_apps', 'action' => 'some_method']);
$routes->connect(
'/some_alias',
[
'controller' => 'tests_apps',
'action' => 'some_method'],
[
'_name' => 'some_alias',
'routeClass' => 'InflectedRoute',
]
);
$routes->fallbacks();
});

0 comments on commit 2081daf

Please sign in to comment.