Skip to content

Commit 06e19a5

Browse files
Add support to "dump", "populate" and "cleanup" parameters
1 parent 120ef9c commit 06e19a5

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

src/Codeception/Extension/MultiDb.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Codeception\Exception\ModuleConfig as ModuleConfigException;
1414
use Codeception\Module;
1515
use Codeception\TestCase;
16+
use Codeception\Configuration as Configuration;
1617

1718
/**
1819
* MultiDb - Module that allows tests to perform setup queries and assertions across multiple databases.
@@ -85,6 +86,55 @@ public function _initialize()
8586
$this->timezone = $this->config['timezone'];
8687

8788
parent::_initialize();
89+
90+
foreach ($this->config['connectors'] as $connector => $connectorConfig) {
91+
if ($connectorConfig['populate']) {
92+
if ($connectorConfig['cleanup']) {
93+
$this->cleanup($connector);
94+
}
95+
$this->loadDump($connector);
96+
}
97+
}
98+
}
99+
100+
protected function loadDump($connector)
101+
{
102+
$config = $this->config['connectors'][$connector];
103+
104+
if ($config['dump'] && ($config['cleanup'] or ($config['populate']))) {
105+
if (!file_exists(Configuration::projectDir() . $config['dump'])) {
106+
throw new ModuleConfigException(
107+
__CLASS__,
108+
"\nFile with dump doesn't exist.
109+
Please, check path for sql file: " . $config['dump']
110+
);
111+
}
112+
$sql = file_get_contents(Configuration::projectDir() . $config['dump']);
113+
$sql = preg_replace('%/\*(?!!\d+)(?:(?!\*/).)*\*/%s', "", $sql);
114+
if (!empty($sql)) {
115+
$sql = explode("\n", $sql);
116+
}
117+
}
118+
if (!$sql) {
119+
return;
120+
}
121+
try {
122+
$this->getDriver($connector)->load($sql);
123+
} catch (\PDOException $e) {
124+
throw new ModuleException(
125+
__CLASS__,
126+
$e->getMessage() . "\nSQL query being executed: " . $sql
127+
);
128+
}
129+
}
130+
131+
protected function cleanup($connector)
132+
{
133+
try {
134+
$this->getDriver($connector)->cleanup();
135+
} catch (\Exception $e) {
136+
throw new ModuleException(__CLASS__, $e->getMessage());
137+
}
88138
}
89139

90140
// HOOK: before scenario

0 commit comments

Comments
 (0)