@@ -35,6 +35,7 @@ class NetteDIModule extends Module
35
35
'wwwDir ' => null ,
36
36
'debugMode ' => null ,
37
37
'removeDefaultExtensions ' => false ,
38
+ 'newContainerForEachTest ' => false ,
38
39
];
39
40
40
41
protected $ requiredFields = [
@@ -59,40 +60,34 @@ class NetteDIModule extends Module
59
60
public function _beforeSuite ($ settings = [])
60
61
{
61
62
$ this ->path = $ settings ['path ' ];
63
+ $ this ->clearTempDir ();
62
64
}
63
65
64
66
public function _before (TestInterface $ test )
65
67
{
66
- $ tempDir = $ this ->path . ' / ' . $ this -> config ['tempDir ' ];
67
- FileSystem:: delete ( realpath ( $ tempDir ) );
68
- FileSystem:: createDir ( $ tempDir ) ;
69
- $ this -> container = null ;
68
+ if ( $ this ->config ['newContainerForEachTest ' ]) {
69
+ $ this -> clearTempDir ( );
70
+ $ this -> container = null ;
71
+ }
70
72
}
71
73
72
- public function _after ( TestInterface $ test )
74
+ public function _afterSuite ( )
73
75
{
74
- if ($ this ->container ) {
75
- try {
76
- $ this ->container ->getByType (Session::class)->close ();
77
- } catch (MissingServiceException $ e ) {
78
- }
79
-
80
- try {
81
- $ journal = $ this ->container ->getByType (IJournal::class);
82
- if ($ journal instanceof SQLiteJournal) {
83
- $ property = new ReflectionProperty (SQLiteJournal::class, 'pdo ' );
84
- $ property ->setAccessible (true );
85
- $ property ->setValue ($ journal , null );
86
- }
87
- } catch (MissingServiceException $ e ) {
88
- }
76
+ $ this ->stopContainer ();
77
+ }
89
78
90
- FileSystem::delete (realpath ($ this ->container ->getParameters ()['tempDir ' ]));
79
+ public function _after (TestInterface $ test )
80
+ {
81
+ if ($ this ->config ['newContainerForEachTest ' ]) {
82
+ $ this ->stopContainer ();
91
83
}
92
84
}
93
85
94
86
public function useConfigFiles (array $ configFiles )
95
87
{
88
+ if (!$ this ->config ['newContainerForEachTest ' ]) {
89
+ $ this ->fail ('The useConfigFiles can only be used if the newContainerForEachTest option is set to true. ' );
90
+ }
96
91
if ($ this ->container ) {
97
92
$ this ->fail ('Can \'t set configFiles after the container is created. ' );
98
93
}
@@ -165,4 +160,35 @@ private function createContainer()
165
160
$ callback ($ this ->container );
166
161
}
167
162
}
163
+
164
+ private function clearTempDir (): void
165
+ {
166
+ $ tempDir = $ this ->path .'/ ' .$ this ->config ['tempDir ' ];
167
+ FileSystem::delete (realpath ($ tempDir ));
168
+ FileSystem::createDir ($ tempDir );
169
+ }
170
+
171
+ private function stopContainer (): void
172
+ {
173
+ if (!$ this ->container ) {
174
+ return ;
175
+ }
176
+
177
+ try {
178
+ $ this ->container ->getByType (Session::class)->close ();
179
+ } catch (MissingServiceException $ e ) {
180
+ }
181
+
182
+ try {
183
+ $ journal = $ this ->container ->getByType (IJournal::class);
184
+ if ($ journal instanceof SQLiteJournal) {
185
+ $ property = new ReflectionProperty (SQLiteJournal::class, 'pdo ' );
186
+ $ property ->setAccessible (true );
187
+ $ property ->setValue ($ journal , null );
188
+ }
189
+ } catch (MissingServiceException $ e ) {
190
+ }
191
+
192
+ FileSystem::delete (realpath ($ this ->container ->getParameters ()['tempDir ' ]));
193
+ }
168
194
}
0 commit comments