Skip to content

Commit 0133b0e

Browse files
xificurkdg
authored andcommitted
Presenter: change default value of globalParams to fix warning while forwarding to Error presenter [Closes #202]
1 parent ac454ca commit 0133b0e

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/Application/UI/Presenter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ abstract class Presenter extends Control implements Application\IPresenter
6767
private $response;
6868

6969
/** @var array */
70-
private $globalParams;
70+
private $globalParams = [];
7171

7272
/** @var array */
7373
private $globalState;

tests/Application/Application.run.phpt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,30 @@ class ErrorPresenter implements Nette\Application\IPresenter
6666
}
6767

6868

69+
class UIPresenter extends Nette\Application\UI\Presenter
70+
{
71+
public function actionDefault()
72+
{
73+
$this->sendResponse(new TextResponse(''));
74+
}
75+
}
76+
77+
78+
class UIErrorPresenter extends Nette\Application\UI\Presenter
79+
{
80+
public function actionDefault()
81+
{
82+
$this->sendResponse(new TextResponse(''));
83+
}
84+
}
85+
86+
6987
$httpRequest = Mockery::mock(Nette\Http\IRequest::class);
7088
$httpRequest->shouldReceive('getMethod')->andReturn('GET');
7189
$httpRequest->shouldReceive('getPost')->andReturn([]);
7290
$httpRequest->shouldReceive('getFiles')->andReturn([]);
7391
$httpRequest->shouldReceive('isSecured')->andReturn(false);
92+
$httpRequest->shouldReceive('isAjax')->andReturn(false);
7493

7594
$httpResponse = Mockery::mock(Nette\Http\IResponse::class);
7695
$httpResponse->shouldIgnoreMissing();
@@ -308,6 +327,58 @@ Assert::noError(function () use ($httpRequest, $httpResponse) {
308327
});
309328

310329

330+
// error during onPresenter with catchException + errorPresenter
331+
Assert::noError(function () use ($httpRequest, $httpResponse) {
332+
$presenter = new UIPresenter;
333+
$errorPresenter = new UIErrorPresenter;
334+
335+
$presenterFactory = Mockery::mock(IPresenterFactory::class);
336+
$presenterFactory->shouldReceive('createPresenter')->with('UI')->andReturn($presenter);
337+
$presenterFactory->shouldReceive('createPresenter')->with('UIError')->andReturn($errorPresenter);
338+
$presenterFactory->shouldReceive('getPresenterClass')->with('UIError')->andReturn(UIErrorPresenter::class);
339+
340+
$router = Mockery::mock(Router::class);
341+
$router->shouldReceive('match')->andReturn(['presenter' => 'UI']);
342+
343+
$errors = [];
344+
345+
$presenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse);
346+
$errorPresenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse);
347+
348+
$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
349+
$app->catchExceptions = true;
350+
$app->errorPresenter = 'UIError';
351+
352+
$app->onPresenter[] = function (Application $application, Nette\Application\IPresenter $presenter) use ($errorPresenter) {
353+
if (!$presenter instanceof UIErrorPresenter) {
354+
throw new RuntimeException('Error on presenter');
355+
}
356+
};
357+
358+
$app->onError[] = function ($app, $e) use (&$errors) {
359+
$errors[] = $e;
360+
};
361+
362+
Assert::noError(function () use ($app) {
363+
$app->run();
364+
});
365+
366+
Assert::count(1, $errors);
367+
Assert::same('Error on presenter', $errors[0]->getMessage());
368+
369+
$requests = $app->getRequests();
370+
Assert::count(2, $requests);
371+
372+
Assert::same('GET', $requests[0]->getMethod());
373+
Assert::same('UI', $requests[0]->getPresenterName());
374+
Assert::null($presenter->getRequest()); // Good presenter did not run
375+
376+
Assert::same('FORWARD', $requests[1]->getMethod());
377+
Assert::same('UIError', $requests[1]->getPresenterName());
378+
Assert::equal($requests[1], $errorPresenter->getRequest());
379+
});
380+
381+
311382
// check maxLoop
312383
Assert::noError(function () use ($httpRequest, $httpResponse) {
313384
$presenter = new InfinityForwardingPresenter;

0 commit comments

Comments
 (0)