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