Skip to content

Commit

Permalink
Merge branch 'console-argv'
Browse files Browse the repository at this point in the history
* console-argv:
  zend-mvc-console require command
  class_alias fix
  command "php"
  console preview class alias fix and its spec
  console controller zf3 preview name fix
  console preview
  exclude zf2 console controller from test
  syntax error fix
  adding error preview console controllers
  readme preview update
  better Console information with include scriptname and args
  • Loading branch information
samsonasik committed Jan 5, 2017
2 parents e406484 + 2bc8c0d commit d3f2385
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 143 deletions.
23 changes: 23 additions & 0 deletions README.md
Expand Up @@ -217,11 +217,34 @@ return [
Give it a try!
--------------

*Web Access*

| URl | Preview For |
|--------------------------------------|--------------|
| http://yourzfapp/error-preview | Exception |
| http://yourzfapp/error-preview/error | Error |

You will get the following page if display_errors config is 0:

![error preview in web](https://cloud.githubusercontent.com/assets/459648/21668589/d4fdadac-d335-11e6-95aa-5a8cfa3f8e4b.png)

*Console Access*

> If you use zend-mvc v3, you need to have `zendframework/zend-mvc-console` in your vendor, if you don't have, you can install it via command:

> ```
> composer require zendframework/zend-mvc-console --sort-packages
> ```
| Command | Preview For |
|------------------------------------------|--------------|
| php public/index.php error-preview | Exception |
| php public/index.php error-preview error | Error |
You will get the following page if display_errors config is 0:
![error preview in console](https://cloud.githubusercontent.com/assets/459648/21669141/8e7690f0-d33b-11e6-99c7-eed4f1ab7edb.png)
Contributing
------------
Contributions are very welcome. Please read [CONTRIBUTING.md](https://github.com/samsonasik/ErrorHeroModule/blob/master/CONTRIBUTING.md)
6 changes: 5 additions & 1 deletion composer.json
Expand Up @@ -27,7 +27,11 @@
"doctrine/doctrine-orm-module": "^1.1",
"kahlan/kahlan": "^3.0.0",
"satooshi/php-coveralls": "^1.0",
"zendframework/zend-mvc": "^2.5|^3.0"
"zendframework/zend-mvc": "^2.5|^3.0",
"zendframework/zend-mvc-console": "^1.1"
},
"suggest": {
"zendframework/zend-mvc-console": "^1.1 for zend-mvc ^3.0 usage to be able to use Console Controller"
},
"autoload": {
"psr-4": {
Expand Down
28 changes: 24 additions & 4 deletions config/module.config.php
Expand Up @@ -10,23 +10,43 @@
'controllers' => [
'invokables' => [
// sm v2 compat
Controller\ErrorPreviewController::class => Controller\ErrorPreviewController::class,
Controller\ErrorPreviewController::class => Controller\ErrorPreviewController::class,
Controller\ErrorPreviewConsoleController::class => Controller\ErrorPreviewConsoleController::class,
],
'factories' => [
// sm v3
Controller\ErrorPreviewController::class => InvokableFactory::class,
Controller\ErrorPreviewController::class => InvokableFactory::class,
Controller\ErrorPreviewConsoleController::class => InvokableFactory::class,
],
],

'router' => [
'routes' => [

'error-preview' => [
'type' => 'Segment',
'options' => [
'route' => '/error-preview[/][:action]',
'defaults' => [
'controller' => Controller\ErrorPreviewController::class,
'action' => 'exception',
'action' => 'exception',
],
],
],

],
],

'console' => [
'router' => [
'routes' => [
'error-preview-console' => [
'options' => [
'route' => 'error-preview [<action>]',
'defaults' => [
'controller' => Controller\ErrorPreviewConsoleController::class,
'action' => 'exception'
],
],
],
],
Expand All @@ -38,7 +58,7 @@
Log\LoggerAbstractServiceFactory::class,
],
'factories' => [
Listener\Mvc::class => Listener\MvcFactory::class,
Listener\Mvc::class => Listener\MvcFactory::class,
Handler\Logging::class => Handler\LoggingFactory::class,
],
],
Expand Down
46 changes: 46 additions & 0 deletions spec/Controller/ErrorPreviewConsoleControllerSpec.php
@@ -0,0 +1,46 @@
<?php

namespace ErrorHeroModule\Spec;

use ErrorHeroModule\Controller\ErrorPreviewConsoleController;

describe('ErrorPreviewConsoleController', function () {

given('controller', function () {

return new ErrorPreviewConsoleController();

});

describe('->exceptionAction()', function() {

it('throw Exception', function() {

$controller = $this->controller;
$closure = function() use ($controller) {
$controller->exceptionAction();
};
expect($closure)->toThrow(new \Exception('a sample error preview'));

});

});

describe('->errorAction()', function() {

it('Error', function() {

skipIf(PHP_MAJOR_VERSION < 7);

try {
$controller = $this->controller;
$controller->errorAction();
} catch (\Throwable $error) {
expect($error)->toBeAnInstanceOf(\Throwable::class);
}

});

});

});
102 changes: 102 additions & 0 deletions spec/IntegrationViaErrorPreviewConsoleControllerSpec.php
@@ -0,0 +1,102 @@
<?php

namespace ErrorHeroModule\Spec;

use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewConsoleController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Log;
use Zend\Mvc\Application;

describe('Integration via ErrorPreviewConsoleController', function () {

given('application', function () {

Console::overrideIsConsole(true);

$application = Application::init([
'modules' => [
'Zend\Router',
'Zend\Db',
'ErrorHeroModule',
],
'module_listener_options' => [
'config_glob_paths' => [
realpath(__DIR__).'/Fixture/autoload/{{,*.}global,{,*.}local}.php',
],
],
]);

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

$db = $serviceManager->get('Zend\Db\Adapter\Adapter');
$tableGateway = new TableGateway('log', $db, null, new ResultSet());
$tableGateway->delete([]);

return $application;

});

describe('/error-preview', function() {

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

skipIf(PHP_MAJOR_VERSION < 7);

Quit::disable();

$_SERVER['argv'] = [
__FILE__,
'error-preview',
'controller' => ErrorPreviewConsoleController::class,
'action' => 'exception',
];

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

expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');

});

});

describe('/error-preview/error', function() {

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

skipIf(PHP_MAJOR_VERSION < 7);

Quit::disable();

$_SERVER['argv'] = [
__FILE__,
'error-preview',
'controller' => ErrorPreviewConsoleController::class,
'action' => 'error',
];

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

expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');

});
});

});
43 changes: 0 additions & 43 deletions spec/IntegrationViaErrorPreviewControllerForIdempotentSpec.php
Expand Up @@ -81,49 +81,6 @@

});

it('show error console message in console env', function() {

Console::overrideIsConsole(true);

skipIf(PHP_MAJOR_VERSION < 7);

Quit::disable();

allow('php_uname')->toBeCalled()->andReturn('Apples-MacBook-Pro.local');

$application = Application::init([
'modules' => [
'Zend\Router',
'Zend\Db',
'ErrorHeroModule',
],
'module_listener_options' => [
'config_glob_paths' => [
realpath(__DIR__).'/Fixture/autoload/{{,*.}global,{,*.}local}.php',
],
],
]);

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

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

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

expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');

});

});

describe('/error-preview/error', function() {
Expand Down
41 changes: 0 additions & 41 deletions spec/IntegrationViaErrorPreviewControllerSpec.php
Expand Up @@ -67,47 +67,6 @@

});

it('show error console message in console env', function() {

Console::overrideIsConsole(true);

skipIf(PHP_MAJOR_VERSION < 7);

Quit::disable();

$application = Application::init([
'modules' => [
'Zend\Router',
'Zend\Db',
'ErrorHeroModule',
],
'module_listener_options' => [
'config_glob_paths' => [
realpath(__DIR__).'/Fixture/autoload/{{,*.}global,{,*.}local}.php',
],
],
]);

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

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

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

expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');

});

});

describe('/error-preview/error', function() {
Expand Down
Expand Up @@ -67,47 +67,6 @@

});

it('show error console message in console env', function() {

Console::overrideIsConsole(true);

skipIf(PHP_MAJOR_VERSION < 7);

Quit::disable();

$application = Application::init([
'modules' => [
'Zend\Router',
'Zend\Db',
'ErrorHeroModule',
],
'module_listener_options' => [
'config_glob_paths' => [
realpath(__DIR__).'/Fixture/autoload/{{,*.}global,{,*.}local}.php',
],
],
]);

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

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

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

expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');

});

});

describe('/error-preview/error', function() {
Expand Down

0 comments on commit d3f2385

Please sign in to comment.