1111namespace Arachne \Codeception \Module ;
1212
1313use Codeception \Module ;
14- use Codeception \TestCase ;
14+ use Codeception \TestInterface ;
15+ use Nette \Caching \Storages \IJournal ;
16+ use Nette \Caching \Storages \SQLiteJournal ;
1517use Nette \Configurator ;
18+ use Nette \DI \MissingServiceException ;
19+ use Nette \Http \Session ;
1620use Nette \Utils \FileSystem ;
21+ use ReflectionProperty ;
1722
1823class Container extends Module
1924{
@@ -33,25 +38,52 @@ class Container extends Module
3338 */
3439 private $ path ;
3540
41+ /**
42+ * @var Container
43+ */
44+ private $ container ;
45+
3646 public function _beforeSuite ($ settings = [])
3747 {
3848 $ this ->path = $ settings ['path ' ];
3949 }
4050
41- public function _before (TestCase $ test )
51+ public function _before (TestInterface $ test )
4252 {
4353 $ tempDir = $ this ->path .'/ ' .$ this ->config ['tempDir ' ];
4454 FileSystem::delete ($ tempDir );
4555 FileSystem::createDir ($ tempDir );
56+ $ this ->container = null ;
4657 }
4758
48- public function _afterSuite ( )
59+ public function _after ( TestInterface $ test )
4960 {
50- FileSystem::delete ($ this ->path .'/ ' .$ this ->config ['tempDir ' ]);
61+ if ($ this ->container ) {
62+ try {
63+ $ this ->container ->getByType (Session::class)->close ();
64+ } catch (MissingServiceException $ e ) {
65+ }
66+
67+ try {
68+ $ journal = $ this ->container ->getByType (IJournal::class);
69+ if ($ journal instanceof SQLiteJournal) {
70+ $ property = new ReflectionProperty (SQLiteJournal::class, 'pdo ' );
71+ $ property ->setAccessible (true );
72+ $ property ->setValue ($ journal , null );
73+ }
74+ } catch (MissingServiceException $ e ) {
75+ }
76+
77+ FileSystem::delete ($ this ->container ->getParameters ()['tempDir ' ]);
78+ }
5179 }
5280
5381 public function createContainer (array $ configFiles = null )
5482 {
83+ if ($ this ->container ) {
84+ $ this ->fail ('Can \'t create more than one container. ' );
85+ }
86+
5587 $ configurator = new $ this ->config ['configurator ' ]();
5688
5789 if ($ this ->config ['logDir ' ]) {
@@ -72,6 +104,8 @@ public function createContainer(array $configFiles = null)
72104 $ configurator ->addConfig ($ this ->path .'/ ' .$ file , false );
73105 }
74106
75- return $ configurator ->createContainer ();
107+ $ this ->container = $ configurator ->createContainer ();
108+
109+ return $ this ->container ;
76110 }
77111}
0 commit comments