Skip to content

Commit

Permalink
Merge pull request #40 from samsonasik/refactor-listener
Browse files Browse the repository at this point in the history
refactor mvc listener to use stopPropagation(true) when possible
  • Loading branch information
samsonasik committed May 14, 2018
2 parents ad8f376 + 17102f6 commit 95f5cc0
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 98 deletions.
Expand Up @@ -4,8 +4,6 @@

use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Mvc\Application;

Expand Down Expand Up @@ -42,17 +40,12 @@

it('show error page', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand All @@ -66,17 +59,12 @@

it('show error page', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview/error');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand Down
Expand Up @@ -4,8 +4,6 @@

use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Mvc\Application;

Expand Down Expand Up @@ -42,17 +40,12 @@

it('show error page', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand All @@ -62,17 +55,12 @@

it('show error page, idempotent for error exist check in DB', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand All @@ -86,17 +74,12 @@

it('show error page', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview/error');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand All @@ -106,17 +89,12 @@

it('show error page, idempotent for error exist check in DB', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview/error');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand Down
Expand Up @@ -4,8 +4,6 @@

use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\PhpEnvironment\Response;
Expand Down Expand Up @@ -44,8 +42,6 @@

it('show error page', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview');
Expand All @@ -54,10 +50,7 @@
allow(Response::class)->toReceive('getHeaders', 'addHeaderLine');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toBe(<<<json
Expand Down
Expand Up @@ -4,8 +4,6 @@

use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\PhpEnvironment\Response;
Expand Down Expand Up @@ -44,8 +42,6 @@

it('show error page', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview');
Expand All @@ -54,10 +50,7 @@
allow(Response::class)->toReceive('getHeaders', 'addHeaderLine');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toBe('We have encountered a problem and we can not fulfill your request. An error report has been generated and sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience.');
Expand Down
16 changes: 2 additions & 14 deletions spec/Integration/IntegrationViaErrorPreviewControllerSpec.php
Expand Up @@ -4,8 +4,6 @@

use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
Expand Down Expand Up @@ -49,17 +47,12 @@

it('show error page', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand All @@ -73,17 +66,12 @@

it('show error page', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview/error');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand Down
Expand Up @@ -5,7 +5,6 @@
use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
Expand Down Expand Up @@ -56,10 +55,7 @@
$request->setUri('/error-preview');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand All @@ -80,10 +76,7 @@
$request->setUri('/error-preview/error');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand Down
Expand Up @@ -5,7 +5,6 @@
use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
Expand Down Expand Up @@ -49,17 +48,12 @@

it('show error page for exception', function() {

Quit::disable();

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('/error-preview');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand All @@ -80,10 +74,7 @@
$request->setUri('/error-preview/error');

ob_start();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$this->application->run();
$content = ob_get_clean();

expect($content)->toContain('<title>Error');
Expand Down
18 changes: 10 additions & 8 deletions src/Listener/Mvc.php
Expand Up @@ -77,13 +77,13 @@ public function exceptionError(Event $e) : void
return;
}

$this->showDefaultViewWhenDisplayErrorSetttingIsDisabled();
$this->showDefaultViewWhenDisplayErrorSetttingIsDisabled($e);
}

/**
* It show default view if display_errors setting = 0.
*/
private function showDefaultViewWhenDisplayErrorSetttingIsDisabled() : void
private function showDefaultViewWhenDisplayErrorSetttingIsDisabled(Event $e) : void
{
if (! Console::isConsole()) {
$response = new HttpResponse();
Expand All @@ -98,10 +98,11 @@ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled() : void
$contentType = $this->detectAjaxMessageContentType($message);

$response->getHeaders()->addHeaderLine('Content-type', $contentType);
$response->setContent($message);
$response->sendHeaders();
echo $message;

$response->send();
exit(-1);
$e->stopPropagation(true);
return;
}

Assertion::isInstanceOf($this->renderer, PhpRenderer::class);
Expand All @@ -114,10 +115,11 @@ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled() : void
$layout->setVariable('content', $this->renderer->render($view));

$response->getHeaders()->addHeaderLine('Content-type', 'text/html');
$response->setContent($this->renderer->render($layout));
$response->sendHeaders();
echo $this->renderer->render($layout);

$response->send();
exit(-1);
$e->stopPropagation(true);
return;
}

$response = new ConsoleResponse();
Expand Down

0 comments on commit 95f5cc0

Please sign in to comment.