Skip to content

Commit c40f577

Browse files
committed
Add newContainerForEachTest option (closes #3)
1 parent 2af3943 commit c40f577

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

src/Module/NetteDIModule.php

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class NetteDIModule extends Module
3535
'wwwDir' => null,
3636
'debugMode' => null,
3737
'removeDefaultExtensions' => false,
38+
'newContainerForEachTest' => false,
3839
];
3940

4041
protected $requiredFields = [
@@ -59,40 +60,34 @@ class NetteDIModule extends Module
5960
public function _beforeSuite($settings = [])
6061
{
6162
$this->path = $settings['path'];
63+
$this->clearTempDir();
6264
}
6365

6466
public function _before(TestInterface $test)
6567
{
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+
}
7072
}
7173

72-
public function _after(TestInterface $test)
74+
public function _afterSuite()
7375
{
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+
}
8978

90-
FileSystem::delete(realpath($this->container->getParameters()['tempDir']));
79+
public function _after(TestInterface $test)
80+
{
81+
if ($this->config['newContainerForEachTest']) {
82+
$this->stopContainer();
9183
}
9284
}
9385

9486
public function useConfigFiles(array $configFiles)
9587
{
88+
if (!$this->config['newContainerForEachTest']) {
89+
$this->fail('The useConfigFiles can only be used if the newContainerForEachTest option is set to true.');
90+
}
9691
if ($this->container) {
9792
$this->fail('Can\'t set configFiles after the container is created.');
9893
}
@@ -165,4 +160,35 @@ private function createContainer()
165160
$callback($this->container);
166161
}
167162
}
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+
}
168194
}

0 commit comments

Comments
 (0)