Skip to content
Permalink
Browse files

Fix route reset and preserve extensions

I wasn't entirely happy with the solution in #12695 and wanted to try an
alternate approach. This set of changes adds state to fewer places and
doesn't require plugins to opt-in to backwards compatible behavior.
Instead it introduces new internal methods to reset router state that
can cause conflicts/duplicate work. Global extensions and default route
classes are not reset.

Refs #12695
Refs #12577
  • Loading branch information...
markstory committed Nov 13, 2018
1 parent 2ab20a6 commit ae6eb59a56ecf391ccada691e0081ee8f988b71a
Showing with 26 additions and 19 deletions.
  1. +24 −0 src/Routing/Router.php
  2. +1 −8 src/TestSuite/IntegrationTestTrait.php
  3. +1 −11 src/TestSuite/MiddlewareDispatcher.php
@@ -514,6 +514,30 @@ public static function reload()
static::$_collection = new RouteCollection();
}
/**
* Reset routes and related state.
*
* Similar to reload() except that this doesn't reset all global state,
* as that leads to incorrect behavior in some plugin test case scenarios.
*
* This method will reset:
*
* - routes
* - URL Filters
* - the initialized property
*
* Extensions and default route classes will not be modified
*
* @internal
* @return void
*/
public static function resetRoutes()
{
static::$_collection = new RouteCollection();
static::$_urlFilters = [];
static::$initialized = false;
}
/**
* Add a URL filter to Router.
*
@@ -193,13 +193,6 @@ trait IntegrationTestTrait
*/
protected $_cookieEncryptionKey;
/**
* Allow router reloading to be disabled.
*
* @var bool
*/
protected $_disableRouterReload = false;
/**
* Auto-detect if the HTTP middleware stack should be used.
*
@@ -547,7 +540,7 @@ protected function _sendRequest($url, $method, $data = [])
protected function _makeDispatcher()
{
if ($this->_useHttpServer) {
return new MiddlewareDispatcher($this, $this->_appClass, $this->_appArgs, $this->_disableRouterReload);
return new MiddlewareDispatcher($this, $this->_appClass, $this->_appArgs);
}
return new LegacyRequestDispatcher($this);
@@ -54,13 +54,6 @@ class MiddlewareDispatcher
*/
protected $_constructorArgs;
/**
* Allow router reloading to be disabled.
*
* @var bool
*/
protected $_disableRouterReload = false;
/**
* The application that is being dispatched.
*
@@ -85,7 +78,6 @@ public function __construct($test, $class = null, $constructorArgs = null, $disa
$this->_test = $test;
$this->_class = $class ?: Configure::read('App.namespace') . '\Application';
$this->_constructorArgs = $constructorArgs ?: [CONFIG];
$this->_disableRouterReload = $disableRouterReload;
try {
$reflect = new ReflectionClass($this->_class);
@@ -137,9 +129,7 @@ protected function resolveRoute(array $url)
}
$out = Router::url($url);
if (!$this->_disableRouterReload) {
Router::reload();
}
Router::resetRoutes();
return $out;
}

0 comments on commit ae6eb59

Please sign in to comment.
You can’t perform that action at this time.