Skip to content

Commit

Permalink
Revert "refactor Mvc Listener: no more exit(-1) direct usage"
Browse files Browse the repository at this point in the history
This reverts commit 47cdc05.
  • Loading branch information
samsonasik committed Apr 4, 2017
1 parent d39020d commit 5d4c648
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 @@ -41,12 +42,17 @@

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

Quit::disable();

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

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

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');
Expand All @@ -66,7 +72,10 @@
$request->setUri('/error-preview/error');

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

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 @@ -41,12 +42,17 @@

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

Quit::disable();

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

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

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');
Expand All @@ -55,12 +61,17 @@

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();
$this->application->run();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$content = ob_get_clean();

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');
Expand All @@ -73,12 +84,17 @@

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

Quit::disable();

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

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

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');
Expand All @@ -94,7 +110,10 @@
$request->setUri('/error-preview/error');

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

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Http\PhpEnvironment\Request;
use Zend\Mvc\Application;
Expand Down Expand Up @@ -41,14 +43,19 @@

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

Quit::disable();

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

allow(Request::class)->toReceive('isXmlHttpRequest')->andReturn(true);

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

expect($content)->toBe(<<<json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Http\PhpEnvironment\Request;
use Zend\Mvc\Application;
Expand Down Expand Up @@ -41,14 +43,19 @@

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

Quit::disable();

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

allow(Request::class)->toReceive('isXmlHttpRequest')->andReturn(true);

ob_start();
$this->application->run();
$closure = function () {
$this->application->run();
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$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: 14 additions & 2 deletions spec/Integration/IntegrationViaErrorPreviewControllerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
Expand Down Expand Up @@ -46,12 +48,17 @@

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

Quit::disable();

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

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

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');
Expand All @@ -64,12 +71,17 @@

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

Quit::disable();

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

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

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
Expand Down Expand Up @@ -46,12 +48,17 @@

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

Quit::disable();

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

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

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');
Expand All @@ -64,12 +71,17 @@

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

Quit::disable();

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

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

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');
Expand Down
12 changes: 10 additions & 2 deletions spec/Listener/MvcSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ErrorHeroModule\Listener\Mvc;
use Kahlan\Plugin\Double;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Db\Adapter\Adapter;
use Zend\EventManager\EventManagerInterface;
Expand Down Expand Up @@ -236,7 +237,10 @@
);

ob_start();
$listener->exceptionError($mvcEvent);
$closure = function () use ($listener, $mvcEvent) {
$listener->exceptionError($mvcEvent);
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$content = ob_get_clean();

expect($content)->toContain('We have encountered a problem');
Expand All @@ -245,6 +249,7 @@
it('call logging->handleErrorException() with default view error if $e->getParam("exception") and display_errors = 0 and not a console', function () {

Console::overrideIsConsole(false);
Quit::disable();
$exception = new \Exception('message');

$mvcEvent = Double::instance(['extends' => MvcEvent::class, 'methods' => '__construct']);
Expand All @@ -253,7 +258,10 @@

ob_start();
allow($this->renderer)->toReceive('render')->andReturn(include __DIR__ . '/../../view/error-hero-module/error-default.phtml');
$this->listener->exceptionError($mvcEvent);
$closure = function () use ($mvcEvent) {
$this->listener->exceptionError($mvcEvent);
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$content = ob_get_clean();

expect($content)->toContain('We have encountered a problem');
Expand Down
2 changes: 2 additions & 0 deletions spec/Middleware/ExpressiveSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use ErrorHeroModule\Handler\Logging;
use ErrorHeroModule\Middleware\Expressive;
use Kahlan\Plugin\Double;
use Zend\Console\Console;
use Zend\Db\Adapter\Adapter;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Expressive\Template\TemplateRendererInterface;
use Zend\Expressive\ZendView\ZendViewRenderer;
use Zend\Http\PhpEnvironment\Request;
use Zend\Log\Logger;
Expand Down
1 change: 1 addition & 0 deletions src/HeroTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ErrorException;
use ErrorHeroModule\Handler\Logging;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\ServerRequest;
use Zend\Expressive\Template\TemplateRendererInterface;
use Zend\View\Renderer\PhpRenderer;
Expand Down
15 changes: 2 additions & 13 deletions src/Listener/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ class Mvc extends AbstractListenerAggregate
{
use HeroTrait;

/**
* @var Event
*/
private $event;

/**
* @param array $errorHeroModuleConfig
* @param Logging $logging
Expand Down Expand Up @@ -68,8 +63,6 @@ public function attach(EventManagerInterface $events, $priority = 1)
*/
public function phpError(Event $e)
{
$this->event = $e;

register_shutdown_function([$this, 'execOnShutdown']);
set_error_handler([$this, 'phpErrorHandler']);
}
Expand Down Expand Up @@ -103,7 +96,6 @@ public function exceptionError(Event $e)
return;
}

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

Expand Down Expand Up @@ -132,9 +124,7 @@ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled()
$response->setContent($content);

$response->send();

$this->event->stopPropagation();
return;
exit(-1);
}

$view = new ViewModel();
Expand All @@ -148,9 +138,8 @@ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled()
$response->setContent($this->renderer->render($layout));

$response->send();
exit(-1);

$this->event->stopPropagation();
return;
}

$response = new ConsoleResponse();
Expand Down

0 comments on commit 5d4c648

Please sign in to comment.