diff --git a/src/Codeception/Extension/MultiDb.php b/src/Codeception/Extension/MultiDb.php index 82fdcab..12ec713 100644 --- a/src/Codeception/Extension/MultiDb.php +++ b/src/Codeception/Extension/MultiDb.php @@ -13,6 +13,7 @@ use Codeception\Exception\ModuleConfig as ModuleConfigException; use Codeception\Module; use Codeception\TestCase; +use Codeception\Configuration as Configuration; /** * MultiDb - Module that allows tests to perform setup queries and assertions across multiple databases. @@ -85,6 +86,55 @@ public function _initialize() $this->timezone = $this->config['timezone']; parent::_initialize(); + + foreach ($this->config['connectors'] as $connector => $connectorConfig) { + if ($connectorConfig['populate']) { + if ($connectorConfig['cleanup']) { + $this->cleanup($connector); + } + $this->loadDump($connector); + } + } + } + + protected function loadDump($connector) + { + $config = $this->config['connectors'][$connector]; + + if ($config['dump'] && ($config['cleanup'] or ($config['populate']))) { + if (!file_exists(Configuration::projectDir() . $config['dump'])) { + throw new ModuleConfigException( + __CLASS__, + "\nFile with dump doesn't exist. + Please, check path for sql file: " . $config['dump'] + ); + } + $sql = file_get_contents(Configuration::projectDir() . $config['dump']); + $sql = preg_replace('%/\*(?!!\d+)(?:(?!\*/).)*\*/%s', "", $sql); + if (!empty($sql)) { + $sql = explode("\n", $sql); + } + } + if (!$sql) { + return; + } + try { + $this->getDriver($connector)->load($sql); + } catch (\PDOException $e) { + throw new ModuleException( + __CLASS__, + $e->getMessage() . "\nSQL query being executed: " . $sql + ); + } + } + + protected function cleanup($connector) + { + try { + $this->getDriver($connector)->cleanup(); + } catch (\Exception $e) { + throw new ModuleException(__CLASS__, $e->getMessage()); + } } // HOOK: before scenario