From d3de2a76765d552ceaf26668f314ca2d4b23c161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mor=C3=A1vek?= Date: Thu, 4 Oct 2018 12:56:17 +0200 Subject: [PATCH] Presenter: change default value of globalParams to fix warning while forwarding to Error presenter --- src/Application/UI/Presenter.php | 2 +- tests/Application/Application.run.phpt | 88 ++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 15 deletions(-) diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 89edeab60..c93f35348 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -66,7 +66,7 @@ abstract class Presenter extends Control implements Application\IPresenter private $response; /** @var array */ - private $globalParams; + private $globalParams = []; /** @var array */ private $globalState; diff --git a/tests/Application/Application.run.phpt b/tests/Application/Application.run.phpt index f1e304fe6..cee765121 100644 --- a/tests/Application/Application.run.phpt +++ b/tests/Application/Application.run.phpt @@ -17,15 +17,11 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -class GoodPresenter implements Nette\Application\IPresenter +class GoodPresenter extends Nette\Application\UI\Presenter { - public $request; - - - public function run(Request $request): IResponse + public function actionDefault() { - $this->request = $request; - return new TextResponse(''); + $this->sendResponse(new TextResponse('')); } } @@ -53,20 +49,17 @@ class BadPresenter implements Nette\Application\IPresenter } -class ErrorPresenter implements Nette\Application\IPresenter +class ErrorPresenter extends Nette\Application\UI\Presenter { - public $request; - - - public function run(Request $request): IResponse + public function actionDefault() { - $this->request = $request; - return new TextResponse(''); + $this->sendResponse(new TextResponse('')); } } $httpRequest = Mockery::mock(Nette\Http\IRequest::class); +$httpRequest->shouldReceive('isAjax')->andReturn(false); $httpResponse = Mockery::mock(Nette\Http\IResponse::class); $httpResponse->shouldIgnoreMissing(); @@ -92,6 +85,8 @@ test(function () use ($httpRequest, $httpResponse) { $router = Mockery::mock(IRouter::class); $router->shouldReceive('match')->andReturn(null); + $errorPresenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse); + $app = new Application($presenterFactory, $router, $httpRequest, $httpResponse); $app->catchExceptions = true; $app->errorPresenter = 'Error'; @@ -118,6 +113,8 @@ test(function () use ($httpRequest, $httpResponse) { $router = Mockery::mock(IRouter::class); $router->shouldReceive('match')->andReturn(new Request('Error', 'GET')); + $errorPresenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse); + $app = new Application($presenterFactory, $router, $httpRequest, $httpResponse); $app->catchExceptions = true; $app->errorPresenter = 'Error'; @@ -160,6 +157,8 @@ test(function () use ($httpRequest, $httpResponse) { $router = Mockery::mock(IRouter::class); $router->shouldReceive('match')->andReturn(new Request('Missing', 'GET')); + $errorPresenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse); + $app = new Application($presenterFactory, $router, $httpRequest, $httpResponse); $app->catchExceptions = true; $app->errorPresenter = 'Error'; @@ -202,6 +201,8 @@ test(function () use ($httpRequest, $httpResponse) { $router = Mockery::mock(IRouter::class); $router->shouldReceive('match')->andReturn(new Request('Bad', 'GET')); + $errorPresenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse); + $app = new Application($presenterFactory, $router, $httpRequest, $httpResponse); $app->catchExceptions = true; $app->errorPresenter = 'Error'; @@ -230,6 +231,8 @@ Assert::noError(function () use ($httpRequest, $httpResponse) { $router = Mockery::mock(IRouter::class); $router->shouldReceive('match')->andReturn(new Request('Good', 'GET')); + $presenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse); + $app = new Application($presenterFactory, $router, $httpRequest, $httpResponse); $app->run(); @@ -254,6 +257,9 @@ Assert::noError(function () use ($httpRequest, $httpResponse) { $router = Mockery::mock(IRouter::class); $router->shouldReceive('match')->andReturn(new Request('Good', 'GET')); + $presenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse); + $errorPresenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse); + $app = new Application($presenterFactory, $router, $httpRequest, $httpResponse); $app->catchExceptions = true; $app->errorPresenter = 'Error'; @@ -280,6 +286,8 @@ Assert::noError(function () use ($httpRequest, $httpResponse) { $errors = []; + $presenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse); + $app = new Application($presenterFactory, $router, $httpRequest, $httpResponse); $app->onStartup[] = function () { throw new RuntimeException('Error at startup', 1); @@ -303,6 +311,58 @@ Assert::noError(function () use ($httpRequest, $httpResponse) { }); +// error during onPresenter with catchException + errorPresenter +Assert::noError(function () use ($httpRequest, $httpResponse) { + $presenter = new GoodPresenter; + $errorPresenter = new ErrorPresenter; + + $presenterFactory = Mockery::mock(IPresenterFactory::class); + $presenterFactory->shouldReceive('createPresenter')->with('Good')->andReturn($presenter); + $presenterFactory->shouldReceive('createPresenter')->with('Error')->andReturn($errorPresenter); + $presenterFactory->shouldReceive('getPresenterClass')->with('Error')->andReturn(ErrorPresenter::class); + + $router = Mockery::mock(IRouter::class); + $router->shouldReceive('match')->andReturn(new Request('Good', 'GET')); + + $errors = []; + + $presenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse); + $errorPresenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse); + + $app = new Application($presenterFactory, $router, $httpRequest, $httpResponse); + $app->catchExceptions = true; + $app->errorPresenter = 'Error'; + + $app->onPresenter[] = function (Application $application, Nette\Application\IPresenter $presenter) use ($errorPresenter) { + if (!$presenter instanceof ErrorPresenter) { + throw new RuntimeException('Error on presenter'); + } + }; + + $app->onError[] = function ($app, $e) use (&$errors) { + $errors[] = $e; + }; + + Assert::noError(function () use ($app) { + $app->run(); + }); + + Assert::count(1, $errors); + Assert::same('Error on presenter', $errors[0]->getMessage()); + + $requests = $app->getRequests(); + Assert::count(2, $requests); + + Assert::same('GET', $requests[0]->getMethod()); + Assert::same('Good', $requests[0]->getPresenterName()); + Assert::null($presenter->getRequest()); // Good presenter did not run + + Assert::same('FORWARD', $requests[1]->getMethod()); + Assert::same('Error', $requests[1]->getPresenterName()); + Assert::equal($requests[1], $errorPresenter->getRequest()); +}); + + // check maxLoop Assert::noError(function () use ($httpRequest, $httpResponse) { $presenter = new InfinityForwardingPresenter;