Browse Source

Remove unused code and restore changes.

Revert the changes that add more complexity to IntegrationTestCase as
they are no longer necessary.

Refs #11978
mark_story 8 years ago
parent
commit
fcd2c9890c

+ 5 - 43
src/TestSuite/IntegrationTestCase.php

@@ -35,7 +35,6 @@ use Cake\View\Helper\SecureFieldTokenTrait;
 use Exception;
 use LogicException;
 use PHPUnit\Exception as PhpunitException;
-use Zend\Diactoros\Stream;
 
 /**
  * A test case class intended to make integration tests of
@@ -486,13 +485,7 @@ abstract class IntegrationTestCase extends TestCase
         $psrRequest = null;
         try {
             $request = $this->_buildRequest($url, $method, $data);
-            $psrRequest = $this->_createRequest($request);
-            if ($dispatcher instanceof LegacyRequestDispatcher) {
-                //The legacy dispatcher expects an array...
-                $response = $dispatcher->execute($request);
-            } else {
-                $response = $dispatcher->execute($psrRequest);
-            }
+            $response = $dispatcher->execute($request);
             $this->_requestSession = $request['session'];
             if ($this->_retainFlashMessages && $this->_flashMessages) {
                 $this->_requestSession->write('Flash', $this->_flashMessages);
@@ -506,39 +499,9 @@ abstract class IntegrationTestCase extends TestCase
             throw $e;
         } catch (Exception $e) {
             $this->_exception = $e;
-            //not passing the request it more accurately reflects what would happen if the global default
-            //exception handler was invoked
-            $this->_handleError($e, null);
-        }
-    }
-
-    /**
-     * Create a PSR7 request from the request spec.
-     *
-     * @param array $spec The request spec.
-     * @return \Psr\Http\Message\ServerRequestInterface
-     */
-    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);
+            // Simulate the global exception handler being invoked.
+            $this->_handleError($e);
         }
-
-        return $request;
     }
 
     /**
@@ -590,18 +553,17 @@ 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\RequestInterface $request The request.
      * @return void
      * @throws \Exception
      */
-    protected function _handleError($exception, $request)
+    protected function _handleError($exception)
     {
         $class = Configure::read('Error.exceptionRenderer');
         if (empty($class) || !class_exists($class)) {
             $class = 'Cake\Error\ExceptionRenderer';
         }
         /** @var \Cake\Error\ExceptionRenderer $instance */
-        $instance = new $class($exception, $request);
+        $instance = new $class($exception);
         $this->_response = $instance->render();
     }
 

+ 33 - 3
src/TestSuite/MiddlewareDispatcher.php

@@ -20,6 +20,7 @@ use Cake\Http\ServerRequestFactory;
 use LogicException;
 use ReflectionClass;
 use ReflectionException;
+use Zend\Diactoros\Stream;
 
 /**
  * Dispatches a request capturing the response for integration
@@ -66,12 +67,41 @@ class MiddlewareDispatcher
     }
 
     /**
+     * Create a PSR7 request from the request spec.
+     *
+     * @param array $spec The request spec.
+     * @return \Psr\Http\Message\ServerRequestInterface
+     */
+    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;
+    }
+
+    /**
      * Run a request and get the response.
      *
-     * @param \Psr\Http\Message\ServerRequestInterface $request The request to execute.
+     * @param array $requestSpec The request spec to execute.
      * @return \Psr\Http\Message\ResponseInterface The generated response.
      */
-    public function execute($request)
+    public function execute($requestSpec)
     {
         try {
             $reflect = new ReflectionClass($this->_class);
@@ -92,6 +122,6 @@ class MiddlewareDispatcher
 
         $server = new Server($app);
 
-        return $server->run($request);
+        return $server->run($this->_createRequest($requestSpec));
     }
 }

+ 2 - 2
tests/test_app/TestApp/ApplicationWithExceptionsInMiddleware.php

@@ -9,7 +9,7 @@
  *
  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  * @link          https://cakephp.org CakePHP(tm) Project
- * @since         3.5.2
+ * @since         3.6.2
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  */
 namespace TestApp;
@@ -43,7 +43,7 @@ class ApplicationWithExceptionsInMiddleware extends BaseApplication
             // and make an error page/response
             ->add(ErrorHandlerMiddleware::class)
 
-            // Handle plugin/theme assets like CakePHP normally does.
+            // Throw an error
             ->add(ThrowsExceptionMiddleware::class)
 
             // Add routing middleware.

+ 0 - 47
tests/test_app/TestApp/Controller/JsonResponseController.php

@@ -1,47 +0,0 @@
-<?php
-/**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
- * @link          https://cakephp.org CakePHP(tm) Project
- * @since         3.0.0
- * @license       https://opensource.org/licenses/mit-license.php MIT License
- */
-namespace TestApp\Controller;
-
-use Cake\Http\Exception\InternalErrorException;
-
-/**
- * PostsController class
- */
-class JsonResponseController extends AppController
-{
-    /**
-     * Components array
-     *
-     * @var array
-     */
-    public $components = [
-        'Flash',
-        'RequestHandler' => [
-            'enableBeforeRedirect' => false
-        ],
-    ];
-
-    public function apiGetData()
-    {
-        if (!$this->getRequest()->accepts('application/json')) {
-            throw new InternalErrorException("Client MUST sent the Accept: application/json header");
-        }
-
-        $data = ['a', 'b', 'c', 'd'];
-        $this->set(compact('data'));
-        $this->set('_serialize', ['data']);
-        $this->RequestHandler->renderAs($this, 'json');
-    }
-}

+ 1 - 1
tests/test_app/TestApp/Middleware/ThrowsExceptionMiddleware.php

@@ -9,7 +9,7 @@
  *
  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  * @link          https://cakephp.org CakePHP(tm) Project
- * @since         3.3.0
+ * @since         3.6.2
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  */
 namespace TestApp\Middleware;