Last active
August 29, 2015 14:08
Codeception workaroung to load the dump.sql using the mysql cli binary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Codeception\Module; | |
use Codeception\Lib\Driver\Db as Driver; | |
use Codeception\Exception\Module as ModuleException; | |
use Codeception\Exception\ModuleConfig as ModuleConfigException; | |
use Codeception\Configuration as Configuration; | |
class MysqlHelper extends \Codeception\Module\Db | |
{ | |
protected $mysqlBin = 'mysql'; | |
public function _initialize() | |
{ | |
if ($this->config['dump'] && ($this->config['cleanup'] or ($this->config['populate']))) { | |
if (!file_exists(Configuration::projectDir() . $this->config['dump'])) { | |
throw new ModuleConfigException( | |
__CLASS__, | |
"\nFile with dump doesn't exist. | |
Please, check path for sql file: " . $this->config['dump'] | |
); | |
} | |
$this->sql = Configuration::projectDir() . $this->config['dump']; | |
} | |
try { | |
$this->driver = Driver::create($this->config['dsn'], $this->config['user'], $this->config['password']); | |
} catch (\PDOException $e) { | |
throw new ModuleException(__CLASS__, $e->getMessage() . ' while creating PDO connection'); | |
} | |
$this->dbh = $this->driver->getDbh(); | |
// starting with loading dump | |
if ($this->config['populate']) { | |
$this->cleanup(); | |
$this->loadDump(); | |
$this->populated = true; | |
} | |
$this->mysqlBin = @$this->config['mysqlBin'] ?: $this->mysqlBin; | |
} | |
protected function loadDump() | |
{ | |
if (! $this->sql) { | |
return; | |
} | |
try { | |
$output = []; | |
$return = 0; | |
$dsn = []; | |
foreach (explode(';', str_replace('mysql:', '', $this->config['dsn'])) as $item) { | |
$i = explode('=', $item); | |
$dsn[$i[0]] = $i[1]; | |
} | |
$user = '-u '.$this->config['user']; | |
$password = $this->config['password'] ? '-p '.$this->config['password'] : ''; | |
$host = '-h '.(@$dsn['host'] ?: '127.0.0.1'); | |
$port = '-P '.(@$dsn['port'] ?: 3306); | |
$database = '-D '.$dsn['dbname']; | |
$command = implode( | |
' ', | |
[this->mysqlBin, $user, $password, $host, $port, $database, '<', $this->sql] | |
) | |
exec($command, $output, $return); | |
if (0 !== $return) { | |
throw new \Exception("Error importing mysql dump: ".implode("\n", $output)); | |
} | |
} catch (\Exception $e) { | |
throw new ModuleException( | |
__CLASS__, | |
$e->getMessage() | |
); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name: FunctionalTester | |
modules: | |
enabled: [Asserts, MysqlHelper] | |
config: | |
MysqlHelper: | |
dsn: 'mysql:host=127.0.0.1;dbname=test' | |
user: 'user' | |
password: '' | |
dump: tests/_data/dump.sql | |
populate: true | |
cleanup: false | |
mysqlBin: 'mysql' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment