Skip to content

Commit cd860f4

Browse files
tadasauciunasNaktibalda
authored andcommitted
[Db] Support initial queries execution after creating connection (#5660)
* Support initial queries execution * Support initial queries execution * Support initial queries execution * initial queries documentation
1 parent cbe6e7c commit cd860f4

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Codeception/Module/Db.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* * ssl_verify_server_cert - disables certificate CN verification (MySQL specific, @see http://php.net/manual/de/ref.pdo-mysql.php)
5555
* * ssl_cipher - list of one or more permissible ciphers to use for SSL encryption (MySQL specific, @see http://php.net/manual/de/ref.pdo-mysql.php#pdo.constants.mysql-attr-cipher)
5656
* * databases - include more database configs and switch between them in tests.
57+
* * initial_queries - list of queries to be executed right after connection to the database has been initiated, i.e. creating the database if it does not exist or preparing the database collation
5758
*
5859
* ## Example
5960
*
@@ -73,6 +74,10 @@
7374
* ssl_ca: '/path/to/ca-cert.pem'
7475
* ssl_verify_server_cert: false
7576
* ssl_cipher: 'AES256-SHA'
77+
* initial_queries:
78+
* - 'CREATE DATABASE IF NOT EXISTS temp_db;'
79+
* - 'USE temp_db;'
80+
* - 'SET NAMES utf8;'
7681
*
7782
* ## Example with multi-dumps
7883
* modules:
@@ -559,6 +564,12 @@ private function connect($databaseKey, $databaseConfig)
559564
$this->_getDriver()->setWaitLock($databaseConfig['waitlock']);
560565
}
561566

567+
if (isset($databaseConfig['initial_queries'])) {
568+
foreach ($databaseConfig['initial_queries'] as $initialQuery) {
569+
$this->drivers[$databaseKey]->executeQuery($initialQuery, []);
570+
}
571+
}
572+
562573
$this->debugSection('Db', 'Connected to ' . $databaseKey . ' ' . $this->drivers[$databaseKey]->getDb());
563574
$this->dbhs[$databaseKey] = $this->drivers[$databaseKey]->getDbh();
564575
}

tests/unit/Codeception/Module/Db/MySqlDbTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ public function testConnectionIsResetOnEveryTestWhenReconnectIsTrue()
6464
$this->assertNotEquals($connection3, $connection2);
6565
}
6666

67+
public function testInitialQueriesAreExecuted()
68+
{
69+
$dbName = 'test_db';
70+
$config = $this->module->_getConfig();
71+
$config['initial_queries'] = [
72+
'CREATE DATABASE IF NOT EXISTS ' . $dbName . ';',
73+
'USE ' . $dbName . ';',
74+
];
75+
$this->module->_reconfigure($config);
76+
$this->module->_before(\Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface'));
77+
$usedDatabaseName = $this->module->dbh->query('SELECT DATABASE();')->fetch(PDO::FETCH_COLUMN);
78+
79+
$this->assertEquals($dbName, $usedDatabaseName);
80+
}
81+
6782
public function testGrabColumnFromDatabase()
6883
{
6984
$emails = $this->module->grabColumnFromDatabase('users', 'email');

0 commit comments

Comments
 (0)