|
13 | 13 | use Codeception\Exception\ModuleConfig as ModuleConfigException; |
14 | 14 | use Codeception\Module; |
15 | 15 | use Codeception\TestCase; |
| 16 | +use Codeception\Configuration as Configuration; |
16 | 17 |
|
17 | 18 | /** |
18 | 19 | * MultiDb - Module that allows tests to perform setup queries and assertions across multiple databases. |
@@ -85,6 +86,55 @@ public function _initialize() |
85 | 86 | $this->timezone = $this->config['timezone']; |
86 | 87 |
|
87 | 88 | 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 | + } |
88 | 138 | } |
89 | 139 |
|
90 | 140 | // HOOK: before scenario |
|
0 commit comments