Skip to content

Commit

Permalink
Remove unused code and restore changes.
Browse files Browse the repository at this point in the history
Revert the changes that add more complexity to IntegrationTestCase as
they are no longer necessary.

Refs #11978
  • Loading branch information
markstory committed Apr 26, 2018
1 parent 0750b08 commit fcd2c98
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 96 deletions.
48 changes: 5 additions & 43 deletions src/TestSuite/IntegrationTestCase.php
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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();
}

Expand Down
36 changes: 33 additions & 3 deletions src/TestSuite/MiddlewareDispatcher.php
Expand Up @@ -20,6 +20,7 @@
use LogicException;
use ReflectionClass;
use ReflectionException;
use Zend\Diactoros\Stream;

/**
* Dispatches a request capturing the response for integration
Expand Down Expand Up @@ -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);
Expand All @@ -92,6 +122,6 @@ public function execute($request)

$server = new Server($app);

return $server->run($request);
return $server->run($this->_createRequest($requestSpec));
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
47 changes: 0 additions & 47 deletions tests/test_app/TestApp/Controller/JsonResponseController.php

This file was deleted.

Expand Up @@ -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;
Expand Down

0 comments on commit fcd2c98

Please sign in to comment.