Browse Source

Updated the TestSuite to also pass along the $requests

Ilie Pandia 8 years ago
parent
commit
50fdde588e
2 changed files with 44 additions and 36 deletions
  1. 43 4
      src/TestSuite/IntegrationTestCase.php
  2. 1 32
      src/TestSuite/MiddlewareDispatcher.php

+ 43 - 4
src/TestSuite/IntegrationTestCase.php

@@ -33,6 +33,8 @@ use Cake\Utility\Text;
 use Cake\View\Helper\SecureFieldTokenTrait;
 use Exception;
 use LogicException;
+use Cake\Http\ServerRequestFactory;
+use Zend\Diactoros\Stream;
 use PHPUnit\Exception as PhpunitException;
 
 /**
@@ -483,7 +485,13 @@ abstract class IntegrationTestCase extends TestCase
         $dispatcher = $this->_makeDispatcher();
         try {
             $request = $this->_buildRequest($url, $method, $data);
-            $response = $dispatcher->execute($request);
+            if($dispatcher instanceof LegacyRequestDispatcher ){
+                //The legacy dispatcher expects an array...
+                $response = $dispatcher->execute($request);
+            }elseif($dispatcher instanceof MiddlewareDispatcher ){
+                $psrRequest = $this->_createRequest($request);
+                $response = $dispatcher->execute($psrRequest);
+            }
             $this->_requestSession = $request['session'];
             if ($this->_retainFlashMessages && $this->_flashMessages) {
                 $this->_requestSession->write('Flash', $this->_flashMessages);
@@ -497,11 +505,41 @@ abstract class IntegrationTestCase extends TestCase
             throw $e;
         } catch (Exception $e) {
             $this->_exception = $e;
-            $this->_handleError($e);
+            $this->_handleError($e, $psrRequest);
         }
     }
 
     /**
+     * Create a PSR7 request from the request spec.
+     *
+     * @param array $spec The request spec.
+     * @return \Psr\Http\Message\RequestInterface
+     */
+    protected function _createRequest($spec)
+    {
+        if (isset($spec['input'])) {
+            $spec['post'] = [];
+        }
+        $request = ServerRequestFactory::fromGlobals(
+            array_merge($_SERVER, $spec['environment'], ['REQUEST_URI' => $spec['url']]),
+            $spec['query'],
+            $spec['post'],
+            $spec['cookies']
+        );
+        $request = $request->withAttribute('session', $spec['session']);
+
+        if (isset($spec['input'])) {
+            $stream = new Stream('php://memory', 'rw');
+            $stream->write($spec['input']);
+            $stream->rewind();
+            $request = $request->withBody($stream);
+        }
+
+        return $request;
+    }
+
+
+    /**
      * Get the correct dispatcher instance.
      *
      * @return \Cake\TestSuite\MiddlewareDispatcher|\Cake\TestSuite\LegacyRequestDispatcher A dispatcher instance
@@ -550,17 +588,18 @@ abstract class IntegrationTestCase extends TestCase
      * If that class does not exist, the built-in renderer will be used.
      *
      * @param \Exception $exception Exception to handle.
+     * @param \Psr\Http\Message\ServerRequestInterface $request The request.
      * @return void
      * @throws \Exception
      */
-    protected function _handleError($exception)
+    protected function _handleError($exception, $request)
     {
         $class = Configure::read('Error.exceptionRenderer');
         if (empty($class) || !class_exists($class)) {
             $class = 'Cake\Error\ExceptionRenderer';
         }
         /** @var \Cake\Error\ExceptionRenderer $instance */
-        $instance = new $class($exception);
+        $instance = new $class($exception, $request);
         $this->_response = $instance->render();
     }
 

+ 1 - 32
src/TestSuite/MiddlewareDispatcher.php

@@ -20,7 +20,6 @@ use Cake\Http\ServerRequestFactory;
 use LogicException;
 use ReflectionClass;
 use ReflectionException;
-use Zend\Diactoros\Stream;
 
 /**
  * Dispatches a request capturing the response for integration
@@ -92,37 +91,7 @@ class MiddlewareDispatcher
         );
 
         $server = new Server($app);
-        $psrRequest = $this->_createRequest($request);
-
-        return $server->run($psrRequest);
+        return $server->run($request);
     }
 
-    /**
-     * Create a PSR7 request from the request spec.
-     *
-     * @param array $spec The request spec.
-     * @return \Psr\Http\Message\RequestInterface
-     */
-    protected function _createRequest($spec)
-    {
-        if (isset($spec['input'])) {
-            $spec['post'] = [];
-        }
-        $request = ServerRequestFactory::fromGlobals(
-            array_merge($_SERVER, $spec['environment'], ['REQUEST_URI' => $spec['url']]),
-            $spec['query'],
-            $spec['post'],
-            $spec['cookies']
-        );
-        $request = $request->withAttribute('session', $spec['session']);
-
-        if (isset($spec['input'])) {
-            $stream = new Stream('php://memory', 'rw');
-            $stream->write($spec['input']);
-            $stream->rewind();
-            $request = $request->withBody($stream);
-        }
-
-        return $request;
-    }
 }