Skip to content

Commit

Permalink
Detection of RedirectResponses when doing controller testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Oct 9, 2017
1 parent 82c67ad commit 6eedefb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
11 changes: 11 additions & 0 deletions tests/_support/Helpers/ControllerResponse.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Tests\Support\Helpers;

use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

Expand Down Expand Up @@ -113,4 +114,14 @@ public function isOK(): bool
return true;
}

/**
* Returns whether or not the Response was a redirect response
*
* @return bool
*/
public function isRedirect(): bool
{
return $this->response instanceof RedirectResponse;
}

}
23 changes: 15 additions & 8 deletions tests/_support/Helpers/ControllerTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,42 @@ public function execute(string $method, ...$params)
// so ensure it's available.
helper('url');

$response = (new ControllerResponse())
$result = (new ControllerResponse())
->setRequest($this->request)
->setResponse($this->response);

try
{
ob_start();

$this->controller->{$method}(...$params);
$response = $this->controller->{$method}(...$params);
}
catch (\Throwable $e)
{
$response->response()
->setStatusCode($e->getCode());
$result->response()
->setStatusCode($e->getCode());
}
finally
{
$output = ob_get_clean();

$response->response()->setBody($output);
// If the controller returned a redirect response
// then we need to use that...
if (isset($response) && $response instanceof Response)
{
$result->setResponse($response);
}

$result->response()->setBody($output);
}

// If not response code has been sent, assume a success
if (empty($response->response()->getStatusCode()))
if (empty($result->response()->getStatusCode()))
{
$response->response()->setStatusCode(200);
$result->response()->setStatusCode(200);
}

return $response;
return $result;
}

/**
Expand Down

0 comments on commit 6eedefb

Please sign in to comment.