From fcd2c9890c50535712a4541867b0c81341c20da2 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 26 Apr 2018 13:50:57 -0400 Subject: [PATCH] Remove unused code and restore changes. Revert the changes that add more complexity to IntegrationTestCase as they are no longer necessary. Refs #11978 --- src/TestSuite/IntegrationTestCase.php | 48 ++----------------- src/TestSuite/MiddlewareDispatcher.php | 36 ++++++++++++-- .../ApplicationWithExceptionsInMiddleware.php | 4 +- .../Controller/JsonResponseController.php | 47 ------------------ .../Middleware/ThrowsExceptionMiddleware.php | 2 +- 5 files changed, 41 insertions(+), 96 deletions(-) delete mode 100644 tests/test_app/TestApp/Controller/JsonResponseController.php diff --git a/src/TestSuite/IntegrationTestCase.php b/src/TestSuite/IntegrationTestCase.php index 421f6e9aace..f640c188e3e 100644 --- a/src/TestSuite/IntegrationTestCase.php +++ b/src/TestSuite/IntegrationTestCase.php @@ -35,7 +35,6 @@ class_alias('PHPUnit_Exception', 'PHPUnit\Exception'); 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 @@ protected function _sendRequest($url, $method, $data = []) $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 @@ protected function _sendRequest($url, $method, $data = []) 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 @@ public function controllerSpy($event, $controller = null) * 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(); } diff --git a/src/TestSuite/MiddlewareDispatcher.php b/src/TestSuite/MiddlewareDispatcher.php index 86570f699b3..df7854375f4 100644 --- a/src/TestSuite/MiddlewareDispatcher.php +++ b/src/TestSuite/MiddlewareDispatcher.php @@ -20,6 +20,7 @@ use LogicException; use ReflectionClass; use ReflectionException; +use Zend\Diactoros\Stream; /** * Dispatches a request capturing the response for integration @@ -65,13 +66,42 @@ public function __construct($test, $class = null, $constructorArgs = null) $this->_constructorArgs = $constructorArgs ?: [CONFIG]; } + /** + * 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 @@ public function execute($request) $server = new Server($app); - return $server->run($request); + return $server->run($this->_createRequest($requestSpec)); } } diff --git a/tests/test_app/TestApp/ApplicationWithExceptionsInMiddleware.php b/tests/test_app/TestApp/ApplicationWithExceptionsInMiddleware.php index 9178a6bba10..ca555eca2d9 100644 --- a/tests/test_app/TestApp/ApplicationWithExceptionsInMiddleware.php +++ b/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 @@ public function middleware($middlewareQueue) // 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. diff --git a/tests/test_app/TestApp/Controller/JsonResponseController.php b/tests/test_app/TestApp/Controller/JsonResponseController.php deleted file mode 100644 index 1694d974dc3..00000000000 --- a/tests/test_app/TestApp/Controller/JsonResponseController.php +++ /dev/null @@ -1,47 +0,0 @@ - [ - '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'); - } -} diff --git a/tests/test_app/TestApp/Middleware/ThrowsExceptionMiddleware.php b/tests/test_app/TestApp/Middleware/ThrowsExceptionMiddleware.php index b7654d1e662..6a46aeb9e81 100644 --- a/tests/test_app/TestApp/Middleware/ThrowsExceptionMiddleware.php +++ b/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;