@@ -84,14 +84,13 @@ class Codecept
8484 public function __construct (array $ options = [])
8585 {
8686 $ 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 );
8989
90- $ baseOptions = $ this ->mergeOptions ($ options );
91- $ this ->extensionLoader ->bootGlobalExtensions ($ baseOptions ); // extensions may override config
90+ $ this ->extensionLoader ->bootGlobalExtensions ($ this ->mergeOptions ($ options ));
9291
9392 $ this ->config = Configuration::config ();
94- $ this ->options = $ this ->mergeOptions ($ options ); // options updated from config
93+ $ this ->options = $ this ->mergeOptions ($ options );
9594
9695 $ this ->output = new Output ($ this ->options );
9796
@@ -105,23 +104,31 @@ public function __construct(array $options = [])
105104 */
106105 protected function mergeOptions (array $ options ): array
107106 {
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+ }
111118 }
112119
113120 public function registerSubscribers (): void
114121 {
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+
125132 if (!$ this ->options ['no-rebuild ' ]) {
126133 $ this ->dispatcher ->addSubscriber (new AutoRebuild ());
127134 }
@@ -131,10 +138,12 @@ public function registerSubscribers(): void
131138 }
132139
133140 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+ ]);
138147 }
139148
140149 if ($ this ->options ['report ' ]) {
@@ -157,10 +166,7 @@ private function isConsolePrinterSubscribed(): bool
157166 {
158167 foreach ($ this ->dispatcher ->getListeners () as $ listeners ) {
159168 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)) {
164170 return true ;
165171 }
166172 }
@@ -176,33 +182,24 @@ private function registerReporters(): void
176182 ''
177183 );
178184 }
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+ }
193195 }
194196 }
195197
196198 public function run (string $ suite , ?string $ test = null , ?array $ config = null ): void
197199 {
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 ' );
205201
202+ $ config = Configuration::suiteSettings ($ suite , $ config ?: Configuration::config ());
206203 $ selectedEnvironments = $ this ->options ['env ' ];
207204
208205 if (!$ selectedEnvironments || empty ($ config ['env ' ])) {
@@ -214,9 +211,7 @@ public function run(string $suite, ?string $test = null, ?array $config = null):
214211 foreach (array_unique ($ selectedEnvironments ) as $ envList ) {
215212 $ envSet = explode (', ' , (string ) $ envList );
216213 $ suiteEnvConfig = $ config ;
217-
218- // contains a list of the environments used in this suite configuration env set.
219- $ envConfigs = [];
214+ $ envConfigs = [];
220215 foreach ($ envSet as $ currentEnv ) {
221216 // The $settings['env'] actually contains all parsed configuration files as a
222217 // 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):
225220 return ;
226221 }
227222
228- // Merge configuration consecutively with already build configuration
229223 if (is_array ($ config ['env ' ][$ currentEnv ])) {
230224 $ suiteEnvConfig = Configuration::mergeConfigs ($ suiteEnvConfig , $ config ['env ' ][$ currentEnv ]);
231225 }
232- $ envConfigs [] = $ currentEnv ;
226+ $ envConfigs [] = $ currentEnv ;
233227 }
234228
235229 $ suiteEnvConfig ['current_environment ' ] = implode (', ' , $ envConfigs );
236230
237- $ suiteToRun = $ suite ;
238- if (!empty ($ envList )) {
239- $ suiteToRun .= ' ( ' . implode (', ' , $ envSet ) . ') ' ;
240- }
231+ $ suiteToRun = $ suite . (empty ($ envList ) ? '' : ' ( ' . implode (', ' , $ envSet ) . ') ' );
241232 $ this ->runSuite ($ suiteEnvConfig , $ suiteToRun , $ test );
242233 }
243234 }
244235
245236 public function runSuite (array $ settings , string $ suite , ?string $ test = null ): void
246237 {
247238 $ 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 );
249240 $ suiteManager ->initialize ();
250241 mt_srand ($ this ->options ['seed ' ]);
251242 $ suiteManager ->loadTests ($ test );
0 commit comments