Skip to content

Instantly share code, notes, and snippets.

@brutuscat
Last active August 29, 2015 14:08
Show Gist options
  • Save brutuscat/cd5a9a290cbd5b01444f to your computer and use it in GitHub Desktop.
Save brutuscat/cd5a9a290cbd5b01444f to your computer and use it in GitHub Desktop.
Codeception workaroung to load the dump.sql using the mysql cli binary
<?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()
);
}
}
}
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