Skip to content

Commit

Permalink
Application: previous presenter is passed to error presenter (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
dakur authored and dg committed Oct 9, 2023
1 parent dae132b commit 32db4c2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function processException(\Throwable $e): void
$this->httpResponse->setCode($e instanceof BadRequestException ? ($e->getHttpCode() ?: 404) : 500);
}

$args = ['exception' => $e, 'request' => Arrays::last($this->requests) ?: null];
$args = ['exception' => $e, 'previousPresenter' => $this->presenter, 'request' => Arrays::last($this->requests) ?: null];
if ($this->presenter instanceof UI\Presenter) {
try {
$this->presenter->forward(":$this->errorPresenter:", $args);
Expand Down
7 changes: 6 additions & 1 deletion tests/Application/Application.run.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ test('no route with error presenter', function () use ($httpRequest, $httpRespon

Assert::equal($requests[0], $errorPresenter->request);
Assert::null($errorPresenter->request->getParameter('request'));
Assert::null($errorPresenter->request->getParameter('previousPresenter'));
Assert::type(BadRequestException::class, $errorPresenter->request->getParameter('exception'));
});

Expand Down Expand Up @@ -154,6 +155,7 @@ test('route to error presenter', function () use ($httpRequest, $httpResponse) {

Assert::equal($requests[1], $errorPresenter->request);
Assert::equal($requests[0], $errorPresenter->request->getParameter('request'));
Assert::null($errorPresenter->request->getParameter('previousPresenter'));
Assert::type(BadRequestException::class, $errorPresenter->request->getParameter('exception'));
});

Expand Down Expand Up @@ -195,6 +197,7 @@ test('missing presenter with error presenter', function () use ($httpRequest, $h

Assert::equal($requests[1], $errorPresenter->request);
Assert::equal($requests[0], $errorPresenter->request->getParameter('request'));
Assert::null($errorPresenter->request->getParameter('previousPresenter'));
Assert::type(BadRequestException::class, $errorPresenter->request->getParameter('exception'));
});

Expand All @@ -213,10 +216,11 @@ Assert::exception(function () use ($httpRequest, $httpResponse) {


test('presenter error with error presenter', function () use ($httpRequest, $httpResponse) {
$badPresenter = new BadPresenter;
$errorPresenter = new ErrorPresenter;

$presenterFactory = Mockery::mock(IPresenterFactory::class);
$presenterFactory->shouldReceive('createPresenter')->with('Bad')->andReturn(new BadPresenter);
$presenterFactory->shouldReceive('createPresenter')->with('Bad')->andReturn($badPresenter);
$presenterFactory->shouldReceive('createPresenter')->with('Error')->andReturn($errorPresenter);

$router = Mockery::mock(Router::class);
Expand All @@ -236,6 +240,7 @@ test('presenter error with error presenter', function () use ($httpRequest, $htt

Assert::equal($requests[1], $errorPresenter->request);
Assert::equal($requests[0], $errorPresenter->request->getParameter('request'));
Assert::equal($badPresenter, $errorPresenter->request->getParameter('previousPresenter'));
Assert::type(BadException::class, $errorPresenter->request->getParameter('exception'));
});

Expand Down

0 comments on commit 32db4c2

Please sign in to comment.