1212
1313use Codeception \Exception \ModuleConfigException ;
1414use Codeception \Module ;
15- use Codeception \TestInterface ;
1615use Doctrine \ORM \EntityManagerInterface ;
16+ use Nette \DI \Container ;
1717use PDOException ;
1818
1919class 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 );
0 commit comments