Skip to content

Commit 08e7ea1

Browse files
committed
doctrine config converter fixes and its integration tests
1 parent e335492 commit 08e7ea1

3 files changed

Lines changed: 202 additions & 8 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
return [
4+
5+
'doctrine' => [
6+
'connection' => [
7+
'orm_default' => [
8+
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
9+
'params' => [
10+
'user' => 'root',
11+
'password' => '',
12+
'dbname' => 'errorheromodule',
13+
'host' => '127.0.0.1',
14+
'port' => '3306',
15+
'driverOptions' => [
16+
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
17+
],
18+
],
19+
],
20+
],
21+
],
22+
23+
'log' => [
24+
'ErrorHeroModuleLogger' => [
25+
'writers' => [
26+
27+
[
28+
'name' => 'db',
29+
'options' => [
30+
'db' => 'Zend\Db\Adapter\Adapter',
31+
'table' => 'log',
32+
'column' => [
33+
'timestamp' => 'date',
34+
'priority' => 'type',
35+
'message' => 'event',
36+
'extra' => [
37+
'url' => 'url',
38+
'file' => 'file',
39+
'line' => 'line',
40+
'error_type' => 'error_type',
41+
'trace' => 'trace',
42+
'request_data' => 'request_data',
43+
],
44+
],
45+
],
46+
],
47+
48+
],
49+
],
50+
],
51+
52+
'error-hero-module' => [
53+
'enable' => true,
54+
'display-settings' => [
55+
56+
// excluded php errors
57+
'exclude-php-errors' => [
58+
E_USER_DEPRECATED
59+
],
60+
61+
// show or not error
62+
'display_errors' => 0,
63+
64+
// if enable and display_errors = 0, the page will bring layout and view
65+
'template' => [
66+
'layout' => 'layout/layout',
67+
'view' => 'error-hero-module/error-default'
68+
],
69+
70+
// if enable and display_errors = 0, the console will bring message
71+
'console' => [
72+
'message' => 'We have encountered a problem and we can not fulfill your request. An error report has been generated and send to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience.',
73+
],
74+
75+
],
76+
'logging-settings' => [
77+
'same-error-log-time-range' => 86400,
78+
],
79+
'email-notification-settings' => [
80+
// set to true to activate email notification on log error
81+
'enable' => false,
82+
83+
// Zend\Mail\Message instance registered at service manager
84+
'mail-message' => 'YourMailMessageService',
85+
86+
// Zend\Mail\Transport\TransportInterface instance registered at service manager
87+
'mail-transport' => 'YourMailTransportService',
88+
89+
// email sender
90+
'email-from' => 'Sender Name <sender@host.com>',
91+
92+
'email-to-send' => [
93+
'developer1@foo.com',
94+
'developer2@foo.com',
95+
],
96+
],
97+
],
98+
];
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 with doctrine', function () {
16+
17+
given('application', function () {
18+
19+
Console::overrideIsConsole(true);
20+
21+
$application = Application::init([
22+
'modules' => [
23+
'Zend\Router',
24+
'DoctrineModule',
25+
'DoctrineORMModule',
26+
'ErrorHeroModule',
27+
],
28+
'module_listener_options' => [
29+
'config_glob_paths' => [
30+
realpath(__DIR__).'/../Fixture/config/autoload-with-doctrine/error-hero-module.local.php',
31+
realpath(__DIR__).'/../Fixture/config/module.local.php',
32+
],
33+
],
34+
]);
35+
36+
$events = $application->getEventManager();
37+
$serviceManager = $application->getServiceManager();
38+
$serviceManager->get('SendResponseListener')
39+
->detach($events);
40+
41+
$db = $serviceManager->get('Zend\Db\Adapter\Adapter');
42+
$tableGateway = new TableGateway('log', $db, null, new ResultSet());
43+
$tableGateway->delete([]);
44+
45+
return $application;
46+
47+
});
48+
49+
describe('error-preview', function() {
50+
51+
it('show error page', function() {
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+
Quit::disable();
80+
81+
$_SERVER['argv'] = [
82+
__FILE__,
83+
'error-preview',
84+
'controller' => ErrorPreviewConsoleController::class,
85+
'action' => 'error',
86+
];
87+
88+
ob_start();
89+
$closure = function () {
90+
$this->application->run();
91+
};
92+
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
93+
$content = ob_get_clean();
94+
95+
expect($content)->toContain('|We have encountered a problem and we can not fulfill your request');
96+
97+
});
98+
});
99+
100+
});

src/Module.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ErrorHeroModule;
44

55
use Doctrine\ORM\EntityManager;
6+
use Zend\Db\Adapter\Adapter;
67
use Zend\ModuleManager\ModuleEvent;
78
use Zend\ModuleManager\ModuleManager;
89

@@ -15,7 +16,7 @@ class Module
1516
public function init(ModuleManager $moduleManager)
1617
{
1718
$eventManager = $moduleManager->getEventManager();
18-
$eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'convertDoctrineToZendDbConfig'], 100);
19+
$eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'convertDoctrineToZendDbConfig']);
1920
$eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'errorPreviewPageHandler'], 101);
2021
}
2122

@@ -43,7 +44,7 @@ public function convertDoctrineToZendDbConfig(ModuleEvent $event)
4344
$params = $doctrineDBALConnection->getParams();
4445
$driverOptions = (isset($params['driverOptions'])) ? $params['driverOptions'] : [];
4546

46-
$configuration['db'] = [
47+
$config = [
4748
'username' => $doctrineDBALConnection->getUsername(),
4849
'password' => $doctrineDBALConnection->getPassword(),
4950
'driver' => $doctrineDBALConnection->getDriver()->getName(),
@@ -53,14 +54,9 @@ public function convertDoctrineToZendDbConfig(ModuleEvent $event)
5354
'driver_options' => $driverOptions,
5455
];
5556

56-
$configuration['service_manager']['factories']['Zend\Db\Adapter\Adapter'] = 'Zend\Db\Adapter\AdapterServiceFactory';
57-
58-
$configListener->setMergedConfig($configuration);
59-
$event->setConfigListener($configListener);
60-
6157
$allowOverride = $services->getAllowOverride();
6258
$services->setAllowOverride(true);
63-
$services->setService('config', $configuration);
59+
$services->setService('Zend\Db\Adapter\Adapter', new Adapter($config));
6460
$services->setAllowOverride($allowOverride);
6561
}
6662

0 commit comments

Comments
 (0)