Skip to content

Commit

Permalink
Merge pull request #12695 from cakephp/master-regression-fix-2
Browse files Browse the repository at this point in the history
Fix router reload regression in integration testing.
  • Loading branch information
markstory committed Nov 4, 2018
2 parents ca28b41 + d73dbfe commit aff3831
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/TestSuite/IntegrationTestCase.php
Expand Up @@ -172,6 +172,13 @@ abstract class IntegrationTestCase extends TestCase
*/
protected $_cookieEncryptionKey;

/**
* Allow router reloading to be disabled.
*
* @var bool
*/
protected $_disableRouterReload = false;

/**
* Auto-detect if the HTTP middleware stack should be used.
*
Expand Down Expand Up @@ -207,6 +214,7 @@ public function tearDown()
$this->_csrfToken = false;
$this->_retainFlashMessages = false;
$this->_useHttpServer = false;
$this->_disableRouterReload = true;
}

/**
Expand Down Expand Up @@ -518,7 +526,7 @@ protected function _sendRequest($url, $method, $data = [])
protected function _makeDispatcher()
{
if ($this->_useHttpServer) {
return new MiddlewareDispatcher($this, $this->_appClass, $this->_appArgs);
return new MiddlewareDispatcher($this, $this->_appClass, $this->_appArgs, $this->_disableRouterReload);
}

return new LegacyRequestDispatcher($this);
Expand Down
15 changes: 13 additions & 2 deletions src/TestSuite/MiddlewareDispatcher.php
Expand Up @@ -54,6 +54,13 @@ class MiddlewareDispatcher
*/
protected $_constructorArgs;

/**
* Allow router reloading to be disabled.
*
* @var bool
*/
protected $_disableRouterReload = false;

/**
* The application that is being dispatched.
*
Expand All @@ -68,13 +75,15 @@ class MiddlewareDispatcher
* @param string|null $class The application class name. Defaults to App\Application.
* @param array|null $constructorArgs The constructor arguments for your application class.
* Defaults to `['./config']`
* @param bool $disableRouterReload Disable Router::reload() call.
* @throws \LogicException If it cannot load class for use in integration testing.
*/
public function __construct($test, $class = null, $constructorArgs = null)
public function __construct($test, $class = null, $constructorArgs = null, $disableRouterReload = false)
{
$this->_test = $test;
$this->_class = $class ?: Configure::read('App.namespace') . '\Application';
$this->_constructorArgs = $constructorArgs ?: [CONFIG];
$this->_disableRouterReload = $disableRouterReload;

try {
$reflect = new ReflectionClass($this->_class);
Expand Down Expand Up @@ -126,7 +135,9 @@ protected function resolveRoute(array $url)
}

$out = Router::url($url);
Router::reload();
if (!$this->_disableRouterReload) {
Router::reload();
}

return $out;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Routing/Filter/RoutingFilterTest.php
Expand Up @@ -18,6 +18,7 @@
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Routing\Filter\RoutingFilter;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -82,7 +83,7 @@ public function testBeforeDispatchSetsParameters()
*/
public function testBeforeDispatchRedirectRoute()
{
Router::scope('/', function ($routes) {
Router::scope('/', function (RouteBuilder $routes) {
$routes->redirect('/home', ['controller' => 'articles']);
$routes->connect('/:controller/:action/*');
});
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Routing/RouteBuilderTest.php
Expand Up @@ -582,7 +582,7 @@ public function testResources()
public function testResourcesPathOption()
{
$routes = new RouteBuilder($this->collection, '/api');
$routes->resources('Articles', ['path' => 'posts'], function ($routes) {
$routes->resources('Articles', ['path' => 'posts'], function (RouteBuilder $routes) {
$routes->resources('Comments');
});
$all = $this->collection->routes();
Expand Down
16 changes: 15 additions & 1 deletion tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Event\EventManager;
use Cake\Http\Response;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\InflectedRoute;
use Cake\TestSuite\IntegrationTestCase;
Expand All @@ -43,7 +44,8 @@ public function setUp()
static::setAppNamespace();

Router::reload();
Router::scope('/', function ($routes) {
Router::extensions(['json']);
Router::scope('/', function (RouteBuilder $routes) {
$routes->setRouteClass(InflectedRoute::class);
$routes->get('/get/:controller/:action', []);
$routes->head('/head/:controller/:action', []);
Expand Down Expand Up @@ -570,6 +572,18 @@ public function testRequestSetsPropertiesHttpServer()
$this->assertEquals('value', $this->viewVariable('test'));
}

/**
* Tests URLs containing extensions.
*
* @return void
*/
public function testRequestWithExt()
{
$this->get(['controller' => 'Posts', 'action' => 'ajax', '_ext' => 'json']);

$this->assertResponseCode(200);
}

/**
* Assert that the stored template doesn't change when cells are rendered.
*
Expand Down
18 changes: 16 additions & 2 deletions tests/test_app/TestApp/Application.php
Expand Up @@ -16,27 +16,41 @@

use Cake\Http\BaseApplication;
use Cake\Routing\Middleware\RoutingMiddleware;
use Cake\Routing\RouteBuilder;
use TestApp\Command\AbortCommand;

class Application extends BaseApplication
{

/**
* @return void
*/
public function bootstrap()
{
parent::bootstrap();
}

/**
* @param \Cake\Console\CommandCollection $commands
*
* @return \Cake\Console\CommandCollection
*/
public function console($commands)
{
return $commands
->add('abort_command', new AbortCommand())
->addMany($commands->autoDiscover());
}

/**
* @param \Cake\Http\MiddlewareQueue $middleware
*
* @return \Cake\Http\MiddlewareQueue
*/
public function middleware($middleware)
{
$middleware->add(new RoutingMiddleware());
$middleware->add(function ($req, $res, $next) {
/** @var \Cake\Http\ServerRequest $res */
$res = $next($req, $res);

return $res->withHeader('X-Middleware', 'true');
Expand All @@ -53,7 +67,7 @@ public function middleware($middleware)
*/
public function routes($routes)
{
$routes->scope('/app', function ($routes) {
$routes->scope('/app', function (RouteBuilder $routes) {
$routes->connect('/articles', ['controller' => 'Articles']);
});
}
Expand Down
5 changes: 5 additions & 0 deletions tests/test_app/TestApp/ApplicationWithDefaultRoutes.php
Expand Up @@ -39,6 +39,11 @@ public function bootstrap()
// Do nothing.
}

/**
* @param \Cake\Http\MiddlewareQueue $middlewareQueue
*
* @return \Cake\Http\MiddlewareQueue
*/
public function middleware($middlewareQueue)
{
$middlewareQueue->add(new RoutingMiddleware($this));
Expand Down
11 changes: 10 additions & 1 deletion tests/test_app/TestApp/ApplicationWithPluginRoutes.php
Expand Up @@ -16,15 +16,24 @@

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

class ApplicationWithPluginRoutes extends BaseApplication
{
/**
* @return void
*/
public function bootstrap()
{
parent::bootstrap();
$this->addPlugin('TestPlugin');
}

/**
* @param \Cake\Http\MiddlewareQueue $middleware
*
* @return \Cake\Http\MiddlewareQueue
*/
public function middleware($middleware)
{
$middleware->add(new RoutingMiddleware($this));
Expand All @@ -40,7 +49,7 @@ public function middleware($middleware)
*/
public function routes($routes)
{
$routes->scope('/app', function ($routes) {
$routes->scope('/app', function (RouteBuilder $routes) {
$routes->connect('/articles', ['controller' => 'Articles']);
});
$routes->loadPlugin('TestPlugin');
Expand Down
13 changes: 13 additions & 0 deletions tests/test_app/TestApp/Controller/PostsController.php
Expand Up @@ -82,6 +82,19 @@ public function get()
// Do nothing.
}

/**
* Stub AJAX method
*
* @return void
*/
public function ajax()
{
$data = [];

$this->set(compact('data'));
$this->set('_serialize', ['data']);
}

/**
* Post endpoint for integration testing with security component.
*
Expand Down
4 changes: 3 additions & 1 deletion tests/test_app/config/routes.php
Expand Up @@ -12,10 +12,12 @@
* @since 2.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;

Router::extensions('json');
Router::scope('/', function ($routes) {
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'pages', 'action' => 'display', 'home']);
$routes->connect(
'/some_alias',
Expand Down

0 comments on commit aff3831

Please sign in to comment.