Skip to content

Commit

Permalink
Fix code coverage measuring of nette/di extensions with DoctrineModule
Browse files Browse the repository at this point in the history
enumag committed Oct 1, 2016
1 parent 4bda2d0 commit 2d2217c
Showing 2 changed files with 37 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/Module/DoctrineModule.php
Original file line number Diff line number Diff line change
@@ -12,8 +12,8 @@

use Codeception\Exception\ModuleConfigException;
use Codeception\Module;
use Codeception\TestInterface;
use Doctrine\ORM\EntityManagerInterface;
use Nette\DI\Container;
use PDOException;

class DoctrineModule extends Module
@@ -26,40 +26,49 @@ class DoctrineModule extends Module
public function _beforeSuite($settings = [])
{
$this->path = $settings['path'];

if ($this->config['loadFiles']) {
$this->getModule(NetteDIModule::class)->onCreateContainer[] = function (Container $container) {
$this->initializeDatabase($container);
};
}
}

public function _before(TestInterface $test)
private function initializeDatabase(Container $container)
{
if ($this->config['loadFiles']) {
$em = $this->getModule(NetteDIModule::class)->grabService(EntityManagerInterface::class);
$connection = $em->getConnection();
$em = $container->getByType(EntityManagerInterface::class);
$connection = $em->getConnection();

foreach ((array) $this->config['loadFiles'] as $file) {
$generator = $this->load(file_get_contents($this->path.'/'.$file));
foreach ((array) $this->config['loadFiles'] as $file) {
$generator = $this->load(file_get_contents($this->path.'/'.$file));

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

/**
* @param string $sql
*
* @return \Generator
*/
public function load($sql)
{
$sql = explode("\n", preg_replace('%/\*(?!!\d+)(?:(?!\*/).)*\*/%s', '', $sql));
$lines = explode("\n", preg_replace('%/\*(?!!\d+)(?:(?!\*/).)*\*/%s', '', $sql));
$query = '';
$delimiter = ';';
$delimiterLength = 1;
foreach ($sql as $sqlLine) {
foreach ($lines as $sqlLine) {
if (preg_match('/DELIMITER ([\;\$\|\\\\]+)/i', $sqlLine, $match)) {
$delimiter = $match[1];
$delimiterLength = strlen($delimiter);
9 changes: 9 additions & 0 deletions src/Module/NetteDIModule.php
Original file line number Diff line number Diff line change
@@ -23,6 +23,11 @@

class NetteDIModule extends Module
{
/**
* @var callable[]
*/
public $onCreateContainer = [];

protected $config = [
'configFiles' => [],
'appDir' => null,
@@ -156,5 +161,9 @@ private function createContainer()
}

$this->container = $configurator->createContainer();

foreach ($this->onCreateContainer as $callback) {
$callback($this->container);
}
}
}

0 comments on commit 2d2217c

Please sign in to comment.