Browse Source

Start sketching out the PSR7 IntegrationTest work.

The biggest hurdle is how to invoke either the existing Dispatcher stack
or the Http\Server stack. I'm going to try sniffing out the class name,
and use its presence to go into PSR7 mode.

The PSR7 mode will do an additional translation from cake->psr as
I don't want to try and re-jig all the internals of IntegrationTestCase
just yet.
Mark Story 9 years ago
parent
commit
9f0820de9f
2 changed files with 100 additions and 9 deletions
  1. 42 9
      src/TestSuite/IntegrationTestCase.php
  2. 58 0
      src/TestSuite/RequestDispatcher.php

+ 42 - 9
src/TestSuite/IntegrationTestCase.php

@@ -20,6 +20,7 @@ use Cake\Network\Session;
 use Cake\Routing\DispatcherFactory;
 use Cake\Routing\Router;
 use Cake\TestSuite\Stub\Response;
+use Cake\TestSuite\RequestDispatcher;
 use Cake\Utility\CookieCryptTrait;
 use Cake\Utility\Hash;
 use Cake\Utility\Security;
@@ -45,6 +46,14 @@ abstract class IntegrationTestCase extends TestCase
     use SecureFieldTokenTrait;
 
     /**
+     * Track whether or not tests are run against
+     * the PSR7 HTTP stack.
+     *
+     * @var bool
+     */
+    protected $_useHttpServer = false;
+
+    /**
      * The data used to build the next request.
      *
      * @var array
@@ -130,6 +139,18 @@ abstract class IntegrationTestCase extends TestCase
     protected $_cookieEncriptionKey = null;
 
     /**
+     * Auto-detect if the Http middleware stack should be used.
+     *
+     * @return void
+     */
+    public function setUp()
+    {
+        parent::setUp();
+        $namespace = Configure::read('App.namespace');
+        $this->_useHttpServer = class_exists($namespace . '\Application');
+    }
+
+    /**
      * Clears the state used for requests.
      *
      * @return void
@@ -148,6 +169,18 @@ abstract class IntegrationTestCase extends TestCase
         $this->_requestSession = null;
         $this->_securityToken = false;
         $this->_csrfToken = false;
+        $this->_useHttpServer = false;
+    }
+
+    /**
+     * Toggle whether or not you want to use the HTTP Server stack.
+     *
+     * @param bool $enable Enable/disable the usage of the Http Stack.
+     * @return void
+     */
+    public function useHttpServer($enable)
+    {
+        $this->_useHttpServer = (bool)$enable;
     }
 
     /**
@@ -352,16 +385,16 @@ abstract class IntegrationTestCase extends TestCase
      */
     protected function _sendRequest($url, $method, $data = [])
     {
-        $request = $this->_buildRequest($url, $method, $data);
-        $response = new Response();
-        $dispatcher = DispatcherFactory::create();
-        $dispatcher->eventManager()->on(
-            'Dispatcher.invokeController',
-            ['priority' => 999],
-            [$this, 'controllerSpy']
-        );
+        if ($this->_useHttpServer) {
+            // The PSR7 mode will need to convert back into a cake request.
+            // and figure out how to handle the session data.
+            throw new \LogicException('Not implemented yet.');
+        } else {
+            $dispatcher = new RequestDispatcher($this);
+        }
         try {
-            $dispatcher->dispatch($request, $response);
+            $request = $this->_buildRequest($url, $method, $data);
+            $response = $dispatcher->execute($request);
             $this->_requestSession = $request->session();
             $this->_response = $response;
         } catch (PHPUnit_Exception $e) {

+ 58 - 0
src/TestSuite/RequestDispatcher.php

@@ -0,0 +1,58 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
+ * @since         3.3.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\TestSuite;
+
+use Cake\Core\Configure;
+use Cake\Network\Request;
+use Cake\Network\Session;
+use Cake\Routing\DispatcherFactory;
+use Cake\TestSuite\Stub\Response;
+
+/**
+ * Dispatches a request capturing the response for integration testing
+ * purposes into the Routing\Dispatcher stack.
+ *
+ * @internal
+ */
+class RequestDispatcher
+{
+    /**
+     * Constructor
+     *
+     * @param \Cake\TestSuite\IntegrationTestCase $test The test case to run.
+     */
+    public function __construct($test)
+    {
+        $this->_test = $test;
+    }
+
+    /**
+     * Run a request and get the response.
+     *
+     * @param \Cake\Network\Request $request The request to execute.
+     * @return \Cake\Network\Response The generated response.
+     */
+    public function execute($request)
+    {
+        $response = new Response();
+        $dispatcher = DispatcherFactory::create();
+        $dispatcher->eventManager()->on(
+            'Dispatcher.invokeController',
+            ['priority' => 999],
+            [$this->_test, 'controllerSpy']
+        );
+        $dispatcher->dispatch($request, $response);
+        return $response;
+    }
+}