|
|
@@ -14,9 +14,12 @@
|
|
|
namespace Cake\TestSuite;
|
|
|
|
|
|
use Cake\Core\Configure;
|
|
|
+use Cake\Core\HttpApplicationInterface;
|
|
|
+use Cake\Core\PluginApplicationInterface;
|
|
|
use Cake\Event\EventManager;
|
|
|
use Cake\Http\Server;
|
|
|
use Cake\Http\ServerRequestFactory;
|
|
|
+use Cake\Routing\Router;
|
|
|
use LogicException;
|
|
|
use ReflectionClass;
|
|
|
use ReflectionException;
|
|
|
@@ -52,6 +55,13 @@ class MiddlewareDispatcher
|
|
|
protected $_constructorArgs;
|
|
|
|
|
|
/**
|
|
|
+ * The application that is being dispatched.
|
|
|
+ *
|
|
|
+ * @var \Cake\Core\HttpApplicationInterface
|
|
|
+ */
|
|
|
+ protected $app;
|
|
|
+
|
|
|
+ /**
|
|
|
* Constructor
|
|
|
*
|
|
|
* @param \Cake\TestSuite\IntegrationTestCase $test The test case to run.
|
|
|
@@ -64,6 +74,58 @@ class MiddlewareDispatcher
|
|
|
$this->_test = $test;
|
|
|
$this->_class = $class ?: Configure::read('App.namespace') . '\Application';
|
|
|
$this->_constructorArgs = $constructorArgs ?: [CONFIG];
|
|
|
+
|
|
|
+ try {
|
|
|
+ $reflect = new ReflectionClass($this->_class);
|
|
|
+ $this->app = $reflect->newInstanceArgs($this->_constructorArgs);
|
|
|
+ } catch (ReflectionException $e) {
|
|
|
+ throw new LogicException(sprintf('Cannot load "%s" for use in integration testing.', $this->_class));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Resolve the provided URL into a string.
|
|
|
+ *
|
|
|
+ * @param array|string $url The URL array/string to resolve.
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public function resolveUrl($url)
|
|
|
+ {
|
|
|
+ // If we need to resolve a Route URL but there are no routes, load routes.
|
|
|
+ if (is_array($url) && count(Router::getRouteCollection()->routes()) === 0) {
|
|
|
+ return $this->resolveRoute($url);
|
|
|
+ }
|
|
|
+
|
|
|
+ return Router::url($url);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Convert a URL array into a string URL via routing.
|
|
|
+ *
|
|
|
+ * @param array $url The url to resolve
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ protected function resolveRoute(array $url)
|
|
|
+ {
|
|
|
+ // Simulate application bootstrap and route loading.
|
|
|
+ // We need both to ensure plugins are loaded.
|
|
|
+ $this->app->bootstrap();
|
|
|
+ if ($this->app instanceof PluginApplicationInterface) {
|
|
|
+ $this->app->pluginBootstrap();
|
|
|
+ }
|
|
|
+ $builder = Router::createRouteBuilder('/');
|
|
|
+
|
|
|
+ if ($this->app instanceof HttpApplicationInterface) {
|
|
|
+ $this->app->routes($builder);
|
|
|
+ }
|
|
|
+ if ($this->app instanceof PluginApplicationInterface) {
|
|
|
+ $this->app->pluginRoutes($builder);
|
|
|
+ }
|
|
|
+
|
|
|
+ $out = Router::url($url);
|
|
|
+ Router::reload();
|
|
|
+
|
|
|
+ return $out;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -74,16 +136,6 @@ class MiddlewareDispatcher
|
|
|
*/
|
|
|
public function execute($request)
|
|
|
{
|
|
|
- try {
|
|
|
- $reflect = new ReflectionClass($this->_class);
|
|
|
- $app = $reflect->newInstanceArgs($this->_constructorArgs);
|
|
|
- } catch (ReflectionException $e) {
|
|
|
- throw new LogicException(sprintf(
|
|
|
- 'Cannot load "%s" for use in integration testing.',
|
|
|
- $this->_class
|
|
|
- ));
|
|
|
- }
|
|
|
-
|
|
|
// Spy on the controller using the initialize hook instead
|
|
|
// of the dispatcher hooks as those will be going away one day.
|
|
|
EventManager::instance()->on(
|
|
|
@@ -91,7 +143,7 @@ class MiddlewareDispatcher
|
|
|
[$this->_test, 'controllerSpy']
|
|
|
);
|
|
|
|
|
|
- $server = new Server($app);
|
|
|
+ $server = new Server($this->app);
|
|
|
$psrRequest = $this->_createRequest($request);
|
|
|
|
|
|
return $server->run($psrRequest);
|