Skip to content

Commit a4ca33e

Browse files
committed
doctrine driverOptions parameter definition support
1 parent a9b8976 commit a4ca33e

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

spec/ModuleSpec.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989

9090
});
9191

92-
it('has EntityManager service but already does not has db config', function () {
92+
it('has EntityManager service but already does not has db config not isset driverOptions', function () {
9393

9494
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
9595
$serviceManager = Double::instance(['implements' => ServiceLocatorInterface::class]);
@@ -106,6 +106,44 @@
106106
$driver = Double::instance(['extends' => Driver::class, 'methods' => '__construct']);
107107
allow($driver)->toReceive('getName')->andReturn('pdo_mysql');
108108

109+
allow($connection)->toReceive('getParams')->andReturn([]);
110+
allow($connection)->toReceive('getUsername')->andReturn('root');
111+
allow($connection)->toReceive('getPassword')->andReturn('');
112+
allow($connection)->toReceive('getDriver')->andReturn($driver);
113+
allow($connection)->toReceive('getDatabase')->andReturn('mydb');
114+
allow($connection)->toReceive('getHost')->andReturn('localhost');
115+
allow($connection)->toReceive('getPort')->andReturn('3306');
116+
117+
allow($entityManager)->toReceive('getConnection')->andReturn($connection);
118+
allow($serviceManager)->toReceive('get')->with(EntityManager::class)->andReturn($entityManager);
119+
120+
$this->module->convertDoctrineToZendDbConfig($moduleEvent);
121+
expect($serviceManager)->toReceive('get')->with(EntityManager::class);
122+
123+
});
124+
125+
it('has EntityManager service but already does not has db config with isset driverOptions', function () {
126+
127+
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
128+
$serviceManager = Double::instance(['implements' => ServiceLocatorInterface::class]);
129+
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
130+
allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(true);
131+
132+
$configListener = Double::instance(['extends' => ConfigListener::class, 'methods' => '__construct']);
133+
allow($moduleEvent)->toReceive('getConfigListener')->andReturn($configListener);
134+
allow($configListener)->toReceive('getMergedConfig')->andReturn([]);
135+
136+
$entityManager = Double::instance(['extends' => EntityManager::class, 'methods' => '__construct']);
137+
$connection = Double::instance(['extends' => Connection::class, 'methods' => '__construct']);
138+
139+
$driver = Double::instance(['extends' => Driver::class, 'methods' => '__construct']);
140+
allow($driver)->toReceive('getName')->andReturn('pdo_mysql');
141+
142+
allow($connection)->toReceive('getParams')->andReturn([
143+
'driverOptions' => [
144+
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
145+
],
146+
]);
109147
allow($connection)->toReceive('getUsername')->andReturn('root');
110148
allow($connection)->toReceive('getPassword')->andReturn('');
111149
allow($connection)->toReceive('getDriver')->andReturn($driver);

src/Module.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ public function convertDoctrineToZendDbConfig(ModuleEvent $event)
4040
$entityManager = $services->get(EntityManager::class);
4141
$doctrineDBALConnection = $entityManager->getConnection();
4242

43+
$params = $doctrineDBALConnection->getParams();
44+
$driverOptions = (isset($params['driverOptions'])) ? $params['driverOptions'] : [];
45+
4346
$configuration['db'] = [
44-
'username' => $doctrineDBALConnection->getUsername(),
45-
'password' => $doctrineDBALConnection->getPassword(),
46-
'driver' => $doctrineDBALConnection->getDriver()->getName(),
47-
'database' => $doctrineDBALConnection->getDatabase(),
48-
'host' => $doctrineDBALConnection->getHost(),
49-
'port' => $doctrineDBALConnection->getPort()
47+
'username' => $doctrineDBALConnection->getUsername(),
48+
'password' => $doctrineDBALConnection->getPassword(),
49+
'driver' => $doctrineDBALConnection->getDriver()->getName(),
50+
'database' => $doctrineDBALConnection->getDatabase(),
51+
'host' => $doctrineDBALConnection->getHost(),
52+
'port' => $doctrineDBALConnection->getPort(),
53+
'driver_options' => $driverOptions,
5054
];
5155

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

0 commit comments

Comments
 (0)