Skip to content

Commit 2d2217c

Browse files
committed
Fix code coverage measuring of nette/di extensions with DoctrineModule
1 parent 4bda2d0 commit 2d2217c

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

src/Module/DoctrineModule.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
use Codeception\Exception\ModuleConfigException;
1414
use Codeception\Module;
15-
use Codeception\TestInterface;
1615
use Doctrine\ORM\EntityManagerInterface;
16+
use Nette\DI\Container;
1717
use PDOException;
1818

1919
class DoctrineModule extends Module
@@ -26,40 +26,49 @@ class DoctrineModule extends Module
2626
public function _beforeSuite($settings = [])
2727
{
2828
$this->path = $settings['path'];
29+
30+
if ($this->config['loadFiles']) {
31+
$this->getModule(NetteDIModule::class)->onCreateContainer[] = function (Container $container) {
32+
$this->initializeDatabase($container);
33+
};
34+
}
2935
}
3036

31-
public function _before(TestInterface $test)
37+
private function initializeDatabase(Container $container)
3238
{
33-
if ($this->config['loadFiles']) {
34-
$em = $this->getModule(NetteDIModule::class)->grabService(EntityManagerInterface::class);
35-
$connection = $em->getConnection();
39+
$em = $container->getByType(EntityManagerInterface::class);
40+
$connection = $em->getConnection();
3641

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

40-
try {
41-
foreach ($generator as $command) {
42-
$stmt = $connection->prepare($command);
43-
if (!$stmt->execute()) {
44-
$error = $stmt->errorInfo();
45-
throw new ModuleConfigException(__CLASS__, $error[2]);
46-
}
47-
$stmt->closeCursor();
45+
try {
46+
foreach ($generator as $command) {
47+
$stmt = $connection->prepare($command);
48+
if (!$stmt->execute()) {
49+
$error = $stmt->errorInfo();
50+
throw new ModuleConfigException(__CLASS__, $error[2]);
4851
}
49-
} catch (PDOException $e) {
50-
throw new ModuleConfigException(__CLASS__, $e->getMessage(), $e);
52+
$stmt->closeCursor();
5153
}
54+
} catch (PDOException $e) {
55+
throw new ModuleConfigException(__CLASS__, $e->getMessage(), $e);
5256
}
5357
}
5458
}
5559

60+
/**
61+
* @param string $sql
62+
*
63+
* @return \Generator
64+
*/
5665
public function load($sql)
5766
{
58-
$sql = explode("\n", preg_replace('%/\*(?!!\d+)(?:(?!\*/).)*\*/%s', '', $sql));
67+
$lines = explode("\n", preg_replace('%/\*(?!!\d+)(?:(?!\*/).)*\*/%s', '', $sql));
5968
$query = '';
6069
$delimiter = ';';
6170
$delimiterLength = 1;
62-
foreach ($sql as $sqlLine) {
71+
foreach ($lines as $sqlLine) {
6372
if (preg_match('/DELIMITER ([\;\$\|\\\\]+)/i', $sqlLine, $match)) {
6473
$delimiter = $match[1];
6574
$delimiterLength = strlen($delimiter);

src/Module/NetteDIModule.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323

2424
class NetteDIModule extends Module
2525
{
26+
/**
27+
* @var callable[]
28+
*/
29+
public $onCreateContainer = [];
30+
2631
protected $config = [
2732
'configFiles' => [],
2833
'appDir' => null,
@@ -156,5 +161,9 @@ private function createContainer()
156161
}
157162

158163
$this->container = $configurator->createContainer();
164+
165+
foreach ($this->onCreateContainer as $callback) {
166+
$callback($this->container);
167+
}
159168
}
160169
}

0 commit comments

Comments
 (0)