1111namespace Arachne \Codeception \Module ;
1212
1313use Arachne \Codeception \Connector \Nette as NetteConnector ;
14- use Codeception \Exception \ModuleConfigException ;
1514use Codeception \Lib \Framework ;
1615use Codeception \TestCase ;
16+ use Nette \Configurator ;
17+ use Nette \DI \Container ;
1718use Nette \DI \MissingServiceException ;
19+ use Nette \Utils \FileSystem ;
1820
1921/**
2022 * @author Jáchym Toušek <[email protected] > 2123 */
2224class Nette extends Framework
2325{
24- public static $ containerClass ;
25-
2626 protected $ config = [
2727 'followRedirects ' => true ,
28+ 'configFiles ' => [],
29+ 'logDir ' => null ,
30+ 'debugMode ' => null ,
31+ 'configurator ' => Configurator::class,
32+ ];
33+
34+ protected $ requiredFields = [
35+ 'tempDir ' ,
2836 ];
2937
38+ /**
39+ * @var array
40+ */
41+ private $ configFiles ;
42+
43+ /**
44+ * @var Container
45+ */
46+ private $ container ;
47+
48+ /**
49+ * @var callable
50+ */
51+ private $ containerAccessor ;
52+
53+ /**
54+ * @var string
55+ */
56+ private $ path ;
57+
58+ public function _beforeSuite ($ settings = [])
59+ {
60+ $ this ->path = $ settings ['path ' ];
61+ }
62+
3063 public function _before (TestCase $ test )
3164 {
32- if (!class_exists (self ::$ containerClass )) {
33- throw new ModuleConfigException (__CLASS__ , 'Specify container class in bootstrap. ' );
34- }
35- $ this ->container = new self::$ containerClass ();
36- $ this ->container ->initialize ();
65+ $ this ->configFiles = null ;
66+ $ this ->container = null ;
67+ $ this ->containerAccessor = function () {
68+ if (!$ this ->container ) {
69+ $ configurator = new $ this ->config ['configurator ' ]();
70+
71+ if ($ this ->config ['logDir ' ]) {
72+ $ configurator ->enableDebugger ($ this ->path .'/ ' .$ this ->config ['logDir ' ]);
73+ }
74+
75+ $ tempDir = $ this ->path .'/ ' .$ this ->config ['tempDir ' ];
76+ FileSystem::delete ($ tempDir );
77+ FileSystem::createDir ($ tempDir );
78+ $ configurator ->setTempDirectory ($ tempDir );
79+
80+ if ($ this ->config ['debugMode ' ] !== null ) {
81+ $ configurator ->setDebugMode ($ this ->config ['debugMode ' ]);
82+ }
83+
84+ $ configFiles = is_array ($ this ->configFiles ) ? $ this ->configFiles : $ this ->config ['configFiles ' ];
85+ foreach ($ configFiles as $ file ) {
86+ $ configurator ->addConfig ($ this ->path .'/ ' .$ file , false );
87+ }
88+
89+ $ this ->container = $ configurator ->createContainer ();
90+ }
91+
92+ return $ this ->container ;
93+ };
94+
3795 $ this ->client = new NetteConnector ();
38- $ this ->client ->setContainer ($ this ->container );
96+ $ this ->client ->setContainerAccessor ($ this ->containerAccessor );
3997 $ this ->client ->followRedirects ($ this ->config ['followRedirects ' ]);
98+
4099 parent ::_before ($ test );
41100 }
42101
102+ public function useConfigFiles (array $ configFiles )
103+ {
104+ if ($ this ->container ) {
105+ $ this ->fail ('Can \'t set configFiles after the container is created. ' );
106+ }
107+ $ this ->configFiles = $ configFiles ;
108+ }
109+
43110 public function _after (TestCase $ test )
44111 {
45112 parent ::_after ($ test );
46113
47- try {
48- $ this ->container ->getByType ('Nette\Http\Session ' )->close ();
49- } catch (MissingServiceException $ e ) {
114+ if ($ this ->container ) {
115+ try {
116+ $ this ->container ->getByType ('Nette\Http\Session ' )->close ();
117+ } catch (MissingServiceException $ e ) {
118+ }
119+
120+ FileSystem::delete ($ this ->container ->getParameters ()['tempDir ' ]);
50121 }
51122
52123 $ _SESSION = [];
@@ -64,7 +135,7 @@ public function _after(TestCase $test)
64135 public function grabService ($ service )
65136 {
66137 try {
67- return $ this ->container ->getByType ($ service );
138+ return call_user_func ( $ this ->containerAccessor ) ->getByType ($ service );
68139 } catch (MissingServiceException $ e ) {
69140 $ this ->fail ($ e ->getMessage ());
70141 }
@@ -75,8 +146,9 @@ public function seeRedirectTo($url)
75146 if ($ this ->config ['followRedirects ' ]) {
76147 $ this ->fail ('Method seeRedirectTo only works when followRedirects option is disabled ' );
77148 }
78- $ request = $ this ->container ->getByType ('Nette\Http\IRequest ' );
79- $ response = $ this ->container ->getByType ('Nette\Http\IResponse ' );
149+ $ container = call_user_func ($ this ->containerAccessor );
150+ $ request = $ container ->getByType ('Nette\Http\IRequest ' );
151+ $ response = $ container ->getByType ('Nette\Http\IResponse ' );
80152 if ($ response ->getHeader ('Location ' ) !== $ request ->getUrl ()->getHostUrl ().$ url && $ response ->getHeader ('Location ' ) !== $ url ) {
81153 $ this ->fail ('Couldn \'t confirm redirect target to be " ' .$ url .'", Location header contains " ' .$ response ->getHeader ('Location ' ).'". ' );
82154 }
0 commit comments