Skip to content

Commit

Permalink
Revert "Merge branch 'issue-11978' into master."
Browse files Browse the repository at this point in the history
This reverts commit 88186b3, reversing
changes made to 13b705f.
  • Loading branch information
markstory committed Apr 26, 2018
1 parent e325872 commit a48697a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 157 deletions.
17 changes: 2 additions & 15 deletions src/Error/ExceptionRenderer.php
Expand Up @@ -29,7 +29,6 @@
use Cake\View\Exception\MissingTemplateException;
use Exception;
use PDOException;
use Psr\Http\Message\ServerRequestInterface;

/**
* Exception Renderer.
Expand Down Expand Up @@ -79,26 +78,16 @@ class ExceptionRenderer implements ExceptionRendererInterface
*/
public $method = '';

/**
* If set, this will be request used to create the controller that will render
* the error.
*
* @var \Psr\Http\Message\ServerRequestInterface|null
*/
protected $request = null;

/**
* Creates the controller to perform rendering on the error response.
* If the error is a Cake\Core\Exception\Exception it will be converted to either a 400 or a 500
* code error depending on the code used to construct the error.
*
* @param \Exception $exception Exception.
* @param \Psr\Http\Message\ServerRequestInterface $request The request - if this is set it will be used instead of creating a new one
*/
public function __construct(Exception $exception, ServerRequestInterface $request = null)
public function __construct(Exception $exception)
{
$this->error = $exception;
$this->request = $request;
$this->controller = $this->_getController();
}

Expand All @@ -125,11 +114,9 @@ protected function _unwrap($exception)
*/
protected function _getController()
{
$request = $this->request ?: Router::getRequest(true);
if ($request === null) {
if (!$request = Router::getRequest(true)) {
$request = ServerRequestFactory::fromGlobals();
}

$response = new Response();
$controller = null;

Expand Down
9 changes: 4 additions & 5 deletions src/Error/Middleware/ErrorHandlerMiddleware.php
Expand Up @@ -113,7 +113,7 @@ public function __invoke($request, $response, $next)
*/
public function handleException($exception, $request, $response)
{
$renderer = $this->getRenderer($exception, $request);
$renderer = $this->getRenderer($exception);
try {
$res = $renderer->render();
$this->logException($request, $exception);
Expand Down Expand Up @@ -148,11 +148,10 @@ protected function handleInternalError($response)
* Get a renderer instance
*
* @param \Exception $exception The exception being rendered.
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @return \Cake\Error\ExceptionRendererInterface The exception renderer.
* @throws \Exception When the renderer class cannot be found.
*/
protected function getRenderer($exception, $request)
protected function getRenderer($exception)
{
if (!$this->exceptionRenderer) {
$this->exceptionRenderer = $this->getConfig('exceptionRenderer') ?: ExceptionRenderer::class;
Expand All @@ -172,11 +171,11 @@ protected function getRenderer($exception, $request)
));
}

return new $class($exception, $request);
return new $class($exception);
}
$factory = $this->exceptionRenderer;

return $factory($exception, $request);
return $factory($exception);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/TestSuite/IntegrationTestCase.php
Expand Up @@ -23,7 +23,6 @@ class_alias('PHPUnit_Exception', 'PHPUnit\Exception');
use Cake\Core\Configure;
use Cake\Database\Exception as DatabaseException;
use Cake\Http\ServerRequest;
use Cake\Http\ServerRequestFactory;
use Cake\Http\Session;
use Cake\Routing\Router;
use Cake\TestSuite\Stub\TestExceptionRenderer;
Expand Down Expand Up @@ -482,7 +481,6 @@ public function options($url)
protected function _sendRequest($url, $method, $data = [])
{
$dispatcher = $this->_makeDispatcher();
$psrRequest = null;
try {
$request = $this->_buildRequest($url, $method, $data);
$response = $dispatcher->execute($request);
Expand All @@ -499,7 +497,6 @@ protected function _sendRequest($url, $method, $data = [])
throw $e;
} catch (Exception $e) {
$this->_exception = $e;
// Simulate the global exception handler being invoked.
$this->_handleError($e);
}
}
Expand Down
63 changes: 32 additions & 31 deletions src/TestSuite/MiddlewareDispatcher.php
Expand Up @@ -66,11 +66,42 @@ public function __construct($test, $class = null, $constructorArgs = null)
$this->_constructorArgs = $constructorArgs ?: [CONFIG];
}

/**
* Run a request and get the response.
*
* @param \Cake\Http\ServerRequest $request The request to execute.
* @return \Psr\Http\Message\ResponseInterface The generated response.
*/
public function execute($request)
{
try {
$reflect = new ReflectionClass($this->_class);
$app = $reflect->newInstanceArgs($this->_constructorArgs);
} catch (ReflectionException $e) {
throw new LogicException(sprintf(
'Cannot load "%s" for use in integration testing.',
$this->_class
));
}

// Spy on the controller using the initialize hook instead
// of the dispatcher hooks as those will be going away one day.
EventManager::instance()->on(
'Controller.initialize',
[$this->_test, 'controllerSpy']
);

$server = new Server($app);
$psrRequest = $this->_createRequest($request);

return $server->run($psrRequest);
}

/**
* Create a PSR7 request from the request spec.
*
* @param array $spec The request spec.
* @return \Psr\Http\Message\ServerRequestInterface
* @return \Psr\Http\Message\RequestInterface
*/
protected function _createRequest($spec)
{
Expand All @@ -94,34 +125,4 @@ protected function _createRequest($spec)

return $request;
}

/**
* Run a request and get the response.
*
* @param array $requestSpec The request spec to execute.
* @return \Psr\Http\Message\ResponseInterface The generated response.
*/
public function execute($requestSpec)
{
try {
$reflect = new ReflectionClass($this->_class);
$app = $reflect->newInstanceArgs($this->_constructorArgs);
} catch (ReflectionException $e) {
throw new LogicException(sprintf(
'Cannot load "%s" for use in integration testing.',
$this->_class
));
}

// Spy on the controller using the initialize hook instead
// of the dispatcher hooks as those will be going away one day.
EventManager::instance()->on(
'Controller.initialize',
[$this->_test, 'controllerSpy']
);

$server = new Server($app);

return $server->run($this->_createRequest($requestSpec));
}
}
19 changes: 0 additions & 19 deletions tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -18,7 +18,6 @@
use Cake\Event\EventManager;
use Cake\Http\Response;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\InflectedRoute;
use Cake\TestSuite\IntegrationTestCase;
Expand Down Expand Up @@ -230,24 +229,6 @@ public function testGetUsingApplicationWithDefaultRoutes()
$this->assertEquals('5', $this->_getBodyAsString());
}

public function testExceptionsInMiddlewareJsonView()
{
Router::reload();
Router::connect('/json_response/api_get_data', [
'controller' => 'JsonResponse',
'action' => 'apiGetData'
]);

$this->configApplication(Configure::read('App.namespace') . '\ApplicationWithExceptionsInMiddleware', null);

$this->_request['headers'] = [ "Accept" => "application/json" ];
$this->get('/json_response/api_get_data');
$this->assertResponseCode(403);
$this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
$this->assertResponseContains('"message": "Sample Message"');
$this->assertResponseContains('"code": 403');
}

/**
* Test sending head requests.
*
Expand Down
54 changes: 0 additions & 54 deletions tests/test_app/TestApp/ApplicationWithExceptionsInMiddleware.php

This file was deleted.

3 changes: 1 addition & 2 deletions tests/test_app/TestApp/Error/TestAppsExceptionRenderer.php
Expand Up @@ -17,8 +17,7 @@ class TestAppsExceptionRenderer extends ExceptionRenderer
*/
protected function _getController()
{
$request = $this->request ?: Router::getRequest(true);
if ($request === null) {
if (!$request = Router::getRequest(true)) {
$request = new ServerRequest();
}
$response = new Response();
Expand Down
28 changes: 0 additions & 28 deletions tests/test_app/TestApp/Middleware/ThrowsExceptionMiddleware.php

This file was deleted.

0 comments on commit a48697a

Please sign in to comment.