@@ -84,14 +84,13 @@ class Codecept
84
84
public function __construct (array $ options = [])
85
85
{
86
86
$ this ->resultAggregator = new ResultAggregator ();
87
- $ this ->dispatcher = new EventDispatcher ();
88
- $ this ->extensionLoader = new ExtensionLoader ($ this ->dispatcher );
87
+ $ this ->dispatcher = new EventDispatcher ();
88
+ $ this ->extensionLoader = new ExtensionLoader ($ this ->dispatcher );
89
89
90
- $ baseOptions = $ this ->mergeOptions ($ options );
91
- $ this ->extensionLoader ->bootGlobalExtensions ($ baseOptions ); // extensions may override config
90
+ $ this ->extensionLoader ->bootGlobalExtensions ($ this ->mergeOptions ($ options ));
92
91
93
92
$ this ->config = Configuration::config ();
94
- $ this ->options = $ this ->mergeOptions ($ options ); // options updated from config
93
+ $ this ->options = $ this ->mergeOptions ($ options );
95
94
96
95
$ this ->output = new Output ($ this ->options );
97
96
@@ -105,23 +104,31 @@ public function __construct(array $options = [])
105
104
*/
106
105
protected function mergeOptions (array $ options ): array
107
106
{
108
- $ config = Configuration::config ();
109
- $ baseOptions = array_merge ($ this ->options , $ config ['settings ' ]);
110
- return array_merge ($ baseOptions , $ options );
107
+ return array_merge ($ this ->options , Configuration::config ()['settings ' ], $ options );
108
+ }
109
+
110
+ /**
111
+ * \Symfony\Component\EventDispatcher\EventSubscriberInterface[] $subscribers
112
+ */
113
+ private function addSubscribers (array $ subscribers ): void
114
+ {
115
+ foreach ($ subscribers as $ subscriber ) {
116
+ $ this ->dispatcher ->addSubscriber ($ subscriber );
117
+ }
111
118
}
112
119
113
120
public function registerSubscribers (): void
114
121
{
115
- // required
116
- $ this -> dispatcher -> addSubscriber ( new GracefulTermination ($ this ->resultAggregator ));
117
- $ this -> dispatcher -> addSubscriber ( new ErrorHandler ());
118
- $ this -> dispatcher -> addSubscriber ( new Dependencies ());
119
- $ this -> dispatcher -> addSubscriber ( new Bootstrap ());
120
- $ this -> dispatcher -> addSubscriber ( new PrepareTest ());
121
- $ this -> dispatcher -> addSubscriber ( new Module ());
122
- $ this -> dispatcher -> addSubscriber ( new BeforeAfterTest ());
123
-
124
- // optional
122
+ $ this -> addSubscribers ([
123
+ new GracefulTermination ($ this ->resultAggregator ),
124
+ new ErrorHandler (),
125
+ new Dependencies (),
126
+ new Bootstrap (),
127
+ new PrepareTest (),
128
+ new Module (),
129
+ new BeforeAfterTest (),
130
+ ]);
131
+
125
132
if (!$ this ->options ['no-rebuild ' ]) {
126
133
$ this ->dispatcher ->addSubscriber (new AutoRebuild ());
127
134
}
@@ -131,10 +138,12 @@ public function registerSubscribers(): void
131
138
}
132
139
133
140
if ($ this ->options ['coverage ' ]) {
134
- $ this ->dispatcher ->addSubscriber (new Local ($ this ->options ));
135
- $ this ->dispatcher ->addSubscriber (new LocalServer ($ this ->options ));
136
- $ this ->dispatcher ->addSubscriber (new RemoteServer ($ this ->options ));
137
- $ this ->dispatcher ->addSubscriber (new CoveragePrinter ($ this ->options , $ this ->output ));
141
+ $ this ->addSubscribers ([
142
+ new Local ($ this ->options ),
143
+ new LocalServer ($ this ->options ),
144
+ new RemoteServer ($ this ->options ),
145
+ new CoveragePrinter ($ this ->options , $ this ->output ),
146
+ ]);
138
147
}
139
148
140
149
if ($ this ->options ['report ' ]) {
@@ -157,10 +166,7 @@ private function isConsolePrinterSubscribed(): bool
157
166
{
158
167
foreach ($ this ->dispatcher ->getListeners () as $ listeners ) {
159
168
foreach ($ listeners as $ listener ) {
160
- if ($ listener instanceof ConsolePrinter) {
161
- return true ;
162
- }
163
- if (is_array ($ listener ) && $ listener [0 ] instanceof ConsolePrinter) {
169
+ if ($ listener instanceof ConsolePrinter || (is_array ($ listener ) && $ listener [0 ] instanceof ConsolePrinter)) {
164
170
return true ;
165
171
}
166
172
}
@@ -176,33 +182,24 @@ private function registerReporters(): void
176
182
''
177
183
);
178
184
}
179
- if ($ this ->options ['html ' ]) {
180
- $ this ->dispatcher ->addSubscriber (
181
- new HtmlReporter ($ this ->options , $ this ->output )
182
- );
183
- }
184
- if ($ this ->options ['xml ' ]) {
185
- $ this ->dispatcher ->addSubscriber (
186
- new JUnitReporter ($ this ->options , $ this ->output )
187
- );
188
- }
189
- if ($ this ->options ['phpunit-xml ' ]) {
190
- $ this ->dispatcher ->addSubscriber (
191
- new PhpUnitReporter ($ this ->options , $ this ->output )
192
- );
185
+
186
+ $ map = [
187
+ 'html ' => fn () => new HtmlReporter ($ this ->options , $ this ->output ),
188
+ 'xml ' => fn () => new JUnitReporter ($ this ->options , $ this ->output ),
189
+ 'phpunit-xml ' => fn () => new PhpUnitReporter ($ this ->options , $ this ->output ),
190
+ ];
191
+ foreach ($ map as $ flag => $ create ) {
192
+ if ($ this ->options [$ flag ]) {
193
+ $ this ->dispatcher ->addSubscriber ($ create ());
194
+ }
193
195
}
194
196
}
195
197
196
198
public function run (string $ suite , ?string $ test = null , ?array $ config = null ): void
197
199
{
198
- ini_set (
199
- 'memory_limit ' ,
200
- $ this ->config ['settings ' ]['memory_limit ' ] ?? '1024M '
201
- );
202
-
203
- $ config = $ config ?: Configuration::config ();
204
- $ config = Configuration::suiteSettings ($ suite , $ config );
200
+ ini_set ('memory_limit ' , $ this ->config ['settings ' ]['memory_limit ' ] ?? '1024M ' );
205
201
202
+ $ config = Configuration::suiteSettings ($ suite , $ config ?: Configuration::config ());
206
203
$ selectedEnvironments = $ this ->options ['env ' ];
207
204
208
205
if (!$ selectedEnvironments || empty ($ config ['env ' ])) {
@@ -214,9 +211,7 @@ public function run(string $suite, ?string $test = null, ?array $config = null):
214
211
foreach (array_unique ($ selectedEnvironments ) as $ envList ) {
215
212
$ envSet = explode (', ' , (string ) $ envList );
216
213
$ suiteEnvConfig = $ config ;
217
-
218
- // contains a list of the environments used in this suite configuration env set.
219
- $ envConfigs = [];
214
+ $ envConfigs = [];
220
215
foreach ($ envSet as $ currentEnv ) {
221
216
// The $settings['env'] actually contains all parsed configuration files as a
222
217
// filename => filecontents key-value array. If there is no configuration file for the
@@ -225,27 +220,23 @@ public function run(string $suite, ?string $test = null, ?array $config = null):
225
220
return ;
226
221
}
227
222
228
- // Merge configuration consecutively with already build configuration
229
223
if (is_array ($ config ['env ' ][$ currentEnv ])) {
230
224
$ suiteEnvConfig = Configuration::mergeConfigs ($ suiteEnvConfig , $ config ['env ' ][$ currentEnv ]);
231
225
}
232
- $ envConfigs [] = $ currentEnv ;
226
+ $ envConfigs [] = $ currentEnv ;
233
227
}
234
228
235
229
$ suiteEnvConfig ['current_environment ' ] = implode (', ' , $ envConfigs );
236
230
237
- $ suiteToRun = $ suite ;
238
- if (!empty ($ envList )) {
239
- $ suiteToRun .= ' ( ' . implode (', ' , $ envSet ) . ') ' ;
240
- }
231
+ $ suiteToRun = $ suite . (empty ($ envList ) ? '' : ' ( ' . implode (', ' , $ envSet ) . ') ' );
241
232
$ this ->runSuite ($ suiteEnvConfig , $ suiteToRun , $ test );
242
233
}
243
234
}
244
235
245
236
public function runSuite (array $ settings , string $ suite , ?string $ test = null ): void
246
237
{
247
238
$ settings ['shard ' ] = $ this ->options ['shard ' ];
248
- $ suiteManager = new SuiteManager ($ this ->dispatcher , $ suite , $ settings , $ this ->options );
239
+ $ suiteManager = new SuiteManager ($ this ->dispatcher , $ suite , $ settings , $ this ->options );
249
240
$ suiteManager ->initialize ();
250
241
mt_srand ($ this ->options ['seed ' ]);
251
242
$ suiteManager ->loadTests ($ test );
0 commit comments