11
11
namespace Arachne \Codeception \Module ;
12
12
13
13
use Arachne \Codeception \Connector \Nette as NetteConnector ;
14
- use Codeception \Exception \ModuleConfigException ;
15
14
use Codeception \Lib \Framework ;
16
15
use Codeception \TestCase ;
16
+ use Nette \Configurator ;
17
+ use Nette \DI \Container ;
17
18
use Nette \DI \MissingServiceException ;
19
+ use Nette \Utils \FileSystem ;
18
20
19
21
/**
20
22
* @author Jáchym Toušek <enumag@gmail.com>
21
23
*/
22
24
class Nette extends Framework
23
25
{
24
- public static $ containerClass ;
25
-
26
26
protected $ config = [
27
27
'followRedirects ' => true ,
28
+ 'configFiles ' => [],
29
+ 'logDir ' => null ,
30
+ 'debugMode ' => null ,
31
+ 'configurator ' => Configurator::class,
32
+ ];
33
+
34
+ protected $ requiredFields = [
35
+ 'tempDir ' ,
28
36
];
29
37
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
+
30
63
public function _before (TestCase $ test )
31
64
{
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
+
37
95
$ this ->client = new NetteConnector ();
38
- $ this ->client ->setContainer ($ this ->container );
96
+ $ this ->client ->setContainerAccessor ($ this ->containerAccessor );
39
97
$ this ->client ->followRedirects ($ this ->config ['followRedirects ' ]);
98
+
40
99
parent ::_before ($ test );
41
100
}
42
101
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
+
43
110
public function _after (TestCase $ test )
44
111
{
45
112
parent ::_after ($ test );
46
113
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 ' ]);
50
121
}
51
122
52
123
$ _SESSION = [];
@@ -64,7 +135,7 @@ public function _after(TestCase $test)
64
135
public function grabService ($ service )
65
136
{
66
137
try {
67
- return $ this ->container ->getByType ($ service );
138
+ return call_user_func ( $ this ->containerAccessor ) ->getByType ($ service );
68
139
} catch (MissingServiceException $ e ) {
69
140
$ this ->fail ($ e ->getMessage ());
70
141
}
@@ -75,8 +146,9 @@ public function seeRedirectTo($url)
75
146
if ($ this ->config ['followRedirects ' ]) {
76
147
$ this ->fail ('Method seeRedirectTo only works when followRedirects option is disabled ' );
77
148
}
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 ' );
80
152
if ($ response ->getHeader ('Location ' ) !== $ request ->getUrl ()->getHostUrl ().$ url && $ response ->getHeader ('Location ' ) !== $ url ) {
81
153
$ this ->fail ('Couldn \'t confirm redirect target to be " ' .$ url .'", Location header contains " ' .$ response ->getHeader ('Location ' ).'". ' );
82
154
}
0 commit comments