Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ abstract class Presenter extends Control implements Application\IPresenter
private $response;

/** @var array */
private $globalParams;
private $globalParams = [];

/** @var array */
private $globalState;
Expand Down
88 changes: 74 additions & 14 deletions tests/Application/Application.run.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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(''));
}
}

Expand Down Expand Up @@ -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);
$httpRequest->shouldReceive('getMethod')->andReturn('GET');
$httpRequest->shouldReceive('getPost')->andReturn([]);
$httpRequest->shouldReceive('getFiles')->andReturn([]);
Expand Down Expand Up @@ -97,6 +90,8 @@ test(function () use ($httpRequest, $httpResponse) {
$router = Mockery::mock(Router::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';
Expand All @@ -123,6 +118,8 @@ test(function () use ($httpRequest, $httpResponse) {
$router = Mockery::mock(Router::class);
$router->shouldReceive('match')->andReturn(['presenter' => 'Error']);

$errorPresenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse);

$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
$app->catchExceptions = true;
$app->errorPresenter = 'Error';
Expand Down Expand Up @@ -165,6 +162,8 @@ test(function () use ($httpRequest, $httpResponse) {
$router = Mockery::mock(Router::class);
$router->shouldReceive('match')->andReturn(['presenter' => 'Missing']);

$errorPresenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse);

$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
$app->catchExceptions = true;
$app->errorPresenter = 'Error';
Expand Down Expand Up @@ -207,6 +206,8 @@ test(function () use ($httpRequest, $httpResponse) {
$router = Mockery::mock(Router::class);
$router->shouldReceive('match')->andReturn(['presenter' => 'Bad']);

$errorPresenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse);

$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
$app->catchExceptions = true;
$app->errorPresenter = 'Error';
Expand Down Expand Up @@ -235,6 +236,8 @@ Assert::noError(function () use ($httpRequest, $httpResponse) {
$router = Mockery::mock(Router::class);
$router->shouldReceive('match')->andReturn(['presenter' => 'Good']);

$presenter->injectPrimary(null, $presenterFactory, $router, $httpRequest, $httpResponse);

$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
$app->run();

Expand All @@ -259,6 +262,9 @@ Assert::noError(function () use ($httpRequest, $httpResponse) {
$router = Mockery::mock(Router::class);
$router->shouldReceive('match')->andReturn(['presenter' => 'Good']);

$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';
Expand All @@ -285,6 +291,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);
Expand All @@ -308,6 +316,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;
Expand Down