Skip to content

Commit

Permalink
doctrine config converter fixes and its integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Feb 20, 2017
1 parent e335492 commit 08e7ea1
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 8 deletions.
@@ -0,0 +1,98 @@
<?php

return [

'doctrine' => [
'connection' => [
'orm_default' => [
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => [
'user' => 'root',
'password' => '',
'dbname' => 'errorheromodule',
'host' => '127.0.0.1',
'port' => '3306',
'driverOptions' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
],
],
],
],
],

'log' => [
'ErrorHeroModuleLogger' => [
'writers' => [

[
'name' => 'db',
'options' => [
'db' => 'Zend\Db\Adapter\Adapter',
'table' => 'log',
'column' => [
'timestamp' => 'date',
'priority' => 'type',
'message' => 'event',
'extra' => [
'url' => 'url',
'file' => 'file',
'line' => 'line',
'error_type' => 'error_type',
'trace' => 'trace',
'request_data' => 'request_data',
],
],
],
],

],
],
],

'error-hero-module' => [
'enable' => true,
'display-settings' => [

// excluded php errors
'exclude-php-errors' => [
E_USER_DEPRECATED
],

// show or not error
'display_errors' => 0,

// if enable and display_errors = 0, the page will bring layout and view
'template' => [
'layout' => 'layout/layout',
'view' => 'error-hero-module/error-default'
],

// if enable and display_errors = 0, the console will bring message
'console' => [
'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.',
],

],
'logging-settings' => [
'same-error-log-time-range' => 86400,
],
'email-notification-settings' => [
// set to true to activate email notification on log error
'enable' => false,

// Zend\Mail\Message instance registered at service manager
'mail-message' => 'YourMailMessageService',

// Zend\Mail\Transport\TransportInterface instance registered at service manager
'mail-transport' => 'YourMailTransportService',

// email sender
'email-from' => 'Sender Name <sender@host.com>',

'email-to-send' => [
'developer1@foo.com',
'developer2@foo.com',
],
],
],
];
@@ -0,0 +1,100 @@
<?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 with doctrine', function () {

given('application', function () {

Console::overrideIsConsole(true);

$application = Application::init([
'modules' => [
'Zend\Router',
'DoctrineModule',
'DoctrineORMModule',
'ErrorHeroModule',
],
'module_listener_options' => [
'config_glob_paths' => [
realpath(__DIR__).'/../Fixture/config/autoload-with-doctrine/error-hero-module.local.php',
realpath(__DIR__).'/../Fixture/config/module.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() {

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() {

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');

});
});

});
12 changes: 4 additions & 8 deletions src/Module.php
Expand Up @@ -3,6 +3,7 @@
namespace ErrorHeroModule;

use Doctrine\ORM\EntityManager;
use Zend\Db\Adapter\Adapter;
use Zend\ModuleManager\ModuleEvent;
use Zend\ModuleManager\ModuleManager;

Expand All @@ -15,7 +16,7 @@ class Module
public function init(ModuleManager $moduleManager)
{
$eventManager = $moduleManager->getEventManager();
$eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'convertDoctrineToZendDbConfig'], 100);
$eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'convertDoctrineToZendDbConfig']);
$eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'errorPreviewPageHandler'], 101);
}

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

$configuration['db'] = [
$config = [
'username' => $doctrineDBALConnection->getUsername(),
'password' => $doctrineDBALConnection->getPassword(),
'driver' => $doctrineDBALConnection->getDriver()->getName(),
Expand All @@ -53,14 +54,9 @@ public function convertDoctrineToZendDbConfig(ModuleEvent $event)
'driver_options' => $driverOptions,
];

$configuration['service_manager']['factories']['Zend\Db\Adapter\Adapter'] = 'Zend\Db\Adapter\AdapterServiceFactory';

$configListener->setMergedConfig($configuration);
$event->setConfigListener($configListener);

$allowOverride = $services->getAllowOverride();
$services->setAllowOverride(true);
$services->setService('config', $configuration);
$services->setService('Zend\Db\Adapter\Adapter', new Adapter($config));
$services->setAllowOverride($allowOverride);
}

Expand Down

0 comments on commit 08e7ea1

Please sign in to comment.