Navigation Menu

Skip to content

Commit

Permalink
Updated the TestSuite to also pass along the $requests
Browse files Browse the repository at this point in the history
  • Loading branch information
iliepandia committed Apr 22, 2018
1 parent 6f05de9 commit 50fdde5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
47 changes: 43 additions & 4 deletions src/TestSuite/IntegrationTestCase.php
Expand Up @@ -33,6 +33,8 @@ class_alias('PHPUnit_Exception', 'PHPUnit\Exception');
use Cake\View\Helper\SecureFieldTokenTrait;
use Exception;
use LogicException;
use Cake\Http\ServerRequestFactory;
use Zend\Diactoros\Stream;
use PHPUnit\Exception as PhpunitException;

/**
Expand Down Expand Up @@ -483,7 +485,13 @@ protected function _sendRequest($url, $method, $data = [])
$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);
Expand All @@ -497,10 +505,40 @@ protected function _sendRequest($url, $method, $data = [])
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.
*
Expand Down Expand Up @@ -550,17 +588,18 @@ 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\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();
}

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

/**
* Dispatches a request capturing the response for integration
Expand Down Expand Up @@ -92,37 +91,7 @@ public function execute($request)
);

$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;
}
}

0 comments on commit 50fdde5

Please sign in to comment.