12
12
13
13
use Codeception \Exception \ModuleConfigException ;
14
14
use Codeception \Module ;
15
- use Codeception \TestInterface ;
16
15
use Doctrine \ORM \EntityManagerInterface ;
16
+ use Nette \DI \Container ;
17
17
use PDOException ;
18
18
19
19
class DoctrineModule extends Module
@@ -26,40 +26,49 @@ class DoctrineModule extends Module
26
26
public function _beforeSuite ($ settings = [])
27
27
{
28
28
$ 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
+ }
29
35
}
30
36
31
- public function _before ( TestInterface $ test )
37
+ private function initializeDatabase ( Container $ container )
32
38
{
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 ();
36
41
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 ));
39
44
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 ]);
48
51
}
49
- } catch (PDOException $ e ) {
50
- throw new ModuleConfigException (__CLASS__ , $ e ->getMessage (), $ e );
52
+ $ stmt ->closeCursor ();
51
53
}
54
+ } catch (PDOException $ e ) {
55
+ throw new ModuleConfigException (__CLASS__ , $ e ->getMessage (), $ e );
52
56
}
53
57
}
54
58
}
55
59
60
+ /**
61
+ * @param string $sql
62
+ *
63
+ * @return \Generator
64
+ */
56
65
public function load ($ sql )
57
66
{
58
- $ sql = explode ("\n" , preg_replace ('%/\*(?!!\d+)(?:(?!\*/).)*\*/%s ' , '' , $ sql ));
67
+ $ lines = explode ("\n" , preg_replace ('%/\*(?!!\d+)(?:(?!\*/).)*\*/%s ' , '' , $ sql ));
59
68
$ query = '' ;
60
69
$ delimiter = '; ' ;
61
70
$ delimiterLength = 1 ;
62
- foreach ($ sql as $ sqlLine ) {
71
+ foreach ($ lines as $ sqlLine ) {
63
72
if (preg_match ('/DELIMITER ([\;\$\| \\\\]+)/i ' , $ sqlLine , $ match )) {
64
73
$ delimiter = $ match [1 ];
65
74
$ delimiterLength = strlen ($ delimiter );
0 commit comments