Skip to content

Commit 8a825cc

Browse files
committed
console preview class alias fix and its spec
1 parent ef225be commit 8a825cc

9 files changed

Lines changed: 160 additions & 183 deletions

config/module.config.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,19 @@
44

55
use Zend\ServiceManager\Factory\InvokableFactory;
66
use Zend\Log;
7-
use Zend\Mvc\Controller\AbstractConsoleController as ZF2AbstractConsoleController;
87

98
return [
109

1110
'controllers' => [
1211
'invokables' => [
1312
// sm v2 compat
1413
Controller\ErrorPreviewController::class => Controller\ErrorPreviewController::class,
15-
Controller\ErrorPreviewConsoleZF2Controller::class => Controller\ErrorPreviewConsoleZF2Controller::class,
16-
Controller\ErrorPreviewConsoleZF3Controller::class => Controller\ErrorPreviewConsoleZF3Controller::class,
14+
Controller\ErrorPreviewConsoleController::class => Controller\ErrorPreviewConsoleController::class,
1715
],
1816
'factories' => [
1917
// sm v3
2018
Controller\ErrorPreviewController::class => InvokableFactory::class,
21-
Controller\ErrorPreviewConsoleZF2Controller::class => InvokableFactory::class,
22-
Controller\ErrorPreviewConsoleZF3Controller::class => InvokableFactory::class,
19+
Controller\ErrorPreviewConsoleController::class => InvokableFactory::class,
2320
],
2421
],
2522

@@ -47,7 +44,7 @@
4744
'options' => [
4845
'route' => 'error-preview [<action>]',
4946
'defaults' => [
50-
'controller' => class_exists(ZF2AbstractConsoleController::class) ? Controller\ErrorPreviewConsoleZF2Controller::class : Controller\ErrorPreviewConsoleZF3Controller::class,
47+
'controller' => Controller\ErrorPreviewConsoleController::class,
5148
'action' => 'exception'
5249
],
5350
],

kahlan-config.php

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace ErrorHeroModule\Spec;
4+
5+
use ErrorHeroModule\Controller\ErrorPreviewConsoleController;
6+
7+
describe('ErrorPreviewConsoleController', function () {
8+
9+
given('controller', function () {
10+
11+
return new ErrorPreviewConsoleController();
12+
13+
});
14+
15+
describe('->exceptionAction()', function() {
16+
17+
it('throw Exception', function() {
18+
19+
$controller = $this->controller;
20+
$closure = function() use ($controller) {
21+
$controller->exceptionAction();
22+
};
23+
expect($closure)->toThrow(new \Exception('a sample error preview'));
24+
25+
});
26+
27+
});
28+
29+
describe('->errorAction()', function() {
30+
31+
it('Error', function() {
32+
33+
skipIf(PHP_MAJOR_VERSION < 7);
34+
35+
try {
36+
$controller = $this->controller;
37+
$controller->errorAction();
38+
} catch (\Throwable $error) {
39+
expect($error)->toBeAnInstanceOf(\Throwable::class);
40+
}
41+
42+
});
43+
44+
});
45+
46+
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace ErrorHeroModule\Spec;
4+
5+
use ErrorHeroModule;
6+
use ErrorHeroModule\Controller\ErrorPreviewConsoleController;
7+
use Kahlan\Plugin\Quit;
8+
use Kahlan\QuitException;
9+
use Zend\Console\Console;
10+
use Zend\Db\ResultSet\ResultSet;
11+
use Zend\Db\TableGateway\TableGateway;
12+
use Zend\Log;
13+
use Zend\Mvc\Application;
14+
15+
describe('Integration via ErrorPreviewConsoleController', function () {
16+
17+
given('application', function () {
18+
19+
Console::overrideIsConsole(true);
20+
21+
$application = Application::init([
22+
'modules' => [
23+
'Zend\Router',
24+
'Zend\Db',
25+
'ErrorHeroModule',
26+
],
27+
'module_listener_options' => [
28+
'config_glob_paths' => [
29+
realpath(__DIR__).'/Fixture/autoload/{{,*.}global,{,*.}local}.php',
30+
],
31+
],
32+
]);
33+
34+
$events = $application->getEventManager();
35+
$serviceManager = $application->getServiceManager();
36+
$serviceManager->get('SendResponseListener')
37+
->detach($events);
38+
39+
$db = $serviceManager->get('Zend\Db\Adapter\Adapter');
40+
$tableGateway = new TableGateway('log', $db, null, new ResultSet());
41+
$tableGateway->delete([]);
42+
43+
return $application;
44+
45+
});
46+
47+
describe('/error-preview', function() {
48+
49+
it('show error page', function() {
50+
51+
skipIf(PHP_MAJOR_VERSION < 7);
52+
53+
Quit::disable();
54+
55+
$_SERVER['argv'] = [
56+
__FILE__,
57+
'error-preview',
58+
'controller' => ErrorPreviewConsoleController::class,
59+
'action' => 'exception',
60+
];
61+
62+
ob_start();
63+
$closure = function () {
64+
$this->application->run();
65+
};
66+
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
67+
$content = ob_get_clean();
68+
69+
expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');
70+
71+
});
72+
73+
});
74+
75+
describe('/error-preview/error', function() {
76+
77+
it('show error page', function() {
78+
79+
skipIf(PHP_MAJOR_VERSION < 7);
80+
81+
Quit::disable();
82+
83+
$_SERVER['argv'] = [
84+
__FILE__,
85+
'error-preview',
86+
'controller' => ErrorPreviewConsoleController::class,
87+
'action' => 'error',
88+
];
89+
90+
ob_start();
91+
$closure = function () {
92+
$this->application->run();
93+
};
94+
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
95+
$content = ob_get_clean();
96+
97+
expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');
98+
99+
});
100+
});
101+
102+
});

spec/IntegrationViaErrorPreviewControllerForIdempotentSpec.php

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -81,49 +81,6 @@
8181

8282
});
8383

84-
it('show error console message in console env', function() {
85-
86-
Console::overrideIsConsole(true);
87-
88-
skipIf(PHP_MAJOR_VERSION < 7);
89-
90-
Quit::disable();
91-
92-
allow('php_uname')->toBeCalled()->andReturn('Apples-MacBook-Pro.local');
93-
94-
$application = Application::init([
95-
'modules' => [
96-
'Zend\Router',
97-
'Zend\Db',
98-
'ErrorHeroModule',
99-
],
100-
'module_listener_options' => [
101-
'config_glob_paths' => [
102-
realpath(__DIR__).'/Fixture/autoload/{{,*.}global,{,*.}local}.php',
103-
],
104-
],
105-
]);
106-
107-
$events = $application->getEventManager();
108-
$serviceManager = $application->getServiceManager();
109-
$serviceManager->get('SendResponseListener')
110-
->detach($events);
111-
112-
$request = $application->getRequest();
113-
$request->setMethod('GET');
114-
$request->setUri('/error-preview');
115-
116-
ob_start();
117-
$closure = function () use ($application) {
118-
$application->run();
119-
};
120-
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
121-
$content = ob_get_clean();
122-
123-
expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');
124-
125-
});
126-
12784
});
12885

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

spec/IntegrationViaErrorPreviewControllerSpec.php

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,47 +67,6 @@
6767

6868
});
6969

70-
it('show error console message in console env', function() {
71-
72-
Console::overrideIsConsole(true);
73-
74-
skipIf(PHP_MAJOR_VERSION < 7);
75-
76-
Quit::disable();
77-
78-
$application = Application::init([
79-
'modules' => [
80-
'Zend\Router',
81-
'Zend\Db',
82-
'ErrorHeroModule',
83-
],
84-
'module_listener_options' => [
85-
'config_glob_paths' => [
86-
realpath(__DIR__).'/Fixture/autoload/{{,*.}global,{,*.}local}.php',
87-
],
88-
],
89-
]);
90-
91-
$events = $application->getEventManager();
92-
$serviceManager = $application->getServiceManager();
93-
$serviceManager->get('SendResponseListener')
94-
->detach($events);
95-
96-
$request = $application->getRequest();
97-
$request->setMethod('GET');
98-
$request->setUri('/error-preview');
99-
100-
ob_start();
101-
$closure = function () use ($application) {
102-
$application->run();
103-
};
104-
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
105-
$content = ob_get_clean();
106-
107-
expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');
108-
109-
});
110-
11170
});
11271

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

spec/IntegrationViaErrorPreviewControllerWithEnableSendMailSpec.php

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,47 +67,6 @@
6767

6868
});
6969

70-
it('show error console message in console env', function() {
71-
72-
Console::overrideIsConsole(true);
73-
74-
skipIf(PHP_MAJOR_VERSION < 7);
75-
76-
Quit::disable();
77-
78-
$application = Application::init([
79-
'modules' => [
80-
'Zend\Router',
81-
'Zend\Db',
82-
'ErrorHeroModule',
83-
],
84-
'module_listener_options' => [
85-
'config_glob_paths' => [
86-
realpath(__DIR__).'/Fixture/autoload/{{,*.}global,{,*.}local}.php',
87-
],
88-
],
89-
]);
90-
91-
$events = $application->getEventManager();
92-
$serviceManager = $application->getServiceManager();
93-
$serviceManager->get('SendResponseListener')
94-
->detach($events);
95-
96-
$request = $application->getRequest();
97-
$request->setMethod('GET');
98-
$request->setUri('/error-preview');
99-
100-
ob_start();
101-
$closure = function () use ($application) {
102-
$application->run();
103-
};
104-
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
105-
$content = ob_get_clean();
106-
107-
expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');
108-
109-
});
110-
11170
});
11271

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

src/Controller/ErrorPreviewConsoleZF3Controller.php renamed to src/Controller/ErrorPreviewConsoleController.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
namespace ErrorHeroModule\Controller;
44

5-
use Zend\Mvc\Console\Controller\AbstractConsoleController;
65
use Zend\View\Model\ConsoleModel;
76

8-
class ErrorPreviewConsoleZF3Controller extends AbstractConsoleController
7+
if (! class_exists('Zend\Mvc\Controller\AbstractConsoleController')) {
8+
class_alias(
9+
'Zend\Mvc\Console\Controller\AbstractConsoleController',
10+
'Zend\Mvc\Controller\AbstractConsoleController'
11+
);
12+
}
13+
use Zend\Mvc\Console\Controller\AbstractConsoleController;
14+
15+
class ErrorPreviewConsoleController extends AbstractConsoleController
916
{
1017
public function exceptionAction()
1118
{
@@ -16,7 +23,5 @@ public function errorAction()
1623
{
1724
$array = [];
1825
$array[1]; // E_NOTICE
19-
20-
return new ConsoleModel();
2126
}
2227
}

0 commit comments

Comments
 (0)