From c5a078455d036274286107c8e698ad4880d75081 Mon Sep 17 00:00:00 2001 From: dakur Date: Fri, 6 Oct 2023 15:54:02 +0200 Subject: [PATCH] Application: previous presenter is passed to error presenter (#317) --- src/Application/Application.php | 2 +- tests/Application/Application.run.phpt | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Application/Application.php b/src/Application/Application.php index dc7c4154d..1d535c25b 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -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); diff --git a/tests/Application/Application.run.phpt b/tests/Application/Application.run.phpt index 3b7c84382..db09a24ec 100644 --- a/tests/Application/Application.run.phpt +++ b/tests/Application/Application.run.phpt @@ -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')); }); @@ -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')); }); @@ -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')); }); @@ -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); @@ -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')); });