Skip to content

Commit

Permalink
Added Doctrine module
Browse files Browse the repository at this point in the history
  • Loading branch information
enumag committed Apr 12, 2015
1 parent 680aab6 commit 696af84
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": ">=5.4.0",
"php": ">=5.5.0",
"arachne/bootstrap": "~0.1",
"codeception/codeception": "~2.0",
"nette/bootstrap": "~2.2",
Expand Down
60 changes: 60 additions & 0 deletions src/Module/Doctrine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Arachne\Codeception\Module;

use Arachne\Codeception\Module\Nette;
use Codeception\Exception\ModuleConfig;
use Codeception\Module;
use Codeception\TestCase;
use Doctrine\ORM\EntityManagerInterface;

class Doctrine extends Module
{

public function _before(TestCase $test)
{
if ($this->config['dump']) {
$em = $this->getModule(Nette::class)->grabService(EntityManagerInterface::class);
$connection = $em->getConnection();
$generator = $this->load(file_get_contents($this->config['dump']));

try {
foreach ($generator as $command) {
$stmt = $connection->prepare($command);
if (!$stmt->execute()) {
$error = $stmt->errorInfo();
throw new ModuleConfig(__CLASS__, $error[2]);
}
$stmt->closeCursor();
}

} catch (\PDOException $e) {
throw new ModuleConfig(__CLASS__, $e->getMessage(), $e);
}
}
}

public function load($sql)
{
$sql = explode("\n", preg_replace('%/\*(?!!\d+)(?:(?!\*/).)*\*/%s', '', $sql));
$query = '';
$delimiter = ';';
$delimiterLength = 1;
foreach ($sql as $sqlLine) {
if (preg_match('/DELIMITER ([\;\$\|\\\\]+)/i', $sqlLine, $match)) {
$delimiter = $match[1];
$delimiterLength = strlen($delimiter);
continue;
}
if (trim($sqlLine) == '' || trim($sqlLine) == ';' || preg_match('~^((--.*?)|(#))~s', $sqlLine)) {
continue;
}
$query .= "\n" . rtrim($sqlLine);
if (substr($query, -1 * $delimiterLength, $delimiterLength) == $delimiter) {
yield substr($query, 0, -1 * $delimiterLength);
$query = '';
}
}
}

}

0 comments on commit 696af84

Please sign in to comment.