Skip to content

Commit c4802e3

Browse files
committed
Simplify src root classes
1 parent c840074 commit c4802e3

17 files changed

+527
-743
lines changed

src/Codeception/Actor.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ abstract class Actor
1515
use Comment;
1616
use Pause;
1717

18-
public function __construct(protected Scenario $scenario)
18+
public function __construct(protected readonly Scenario $scenario)
1919
{
2020
}
2121

@@ -40,10 +40,9 @@ public function wantToTest(string $text): void
4040
{
4141
}
4242

43-
public function __call(string $method, array $arguments)
43+
public function __call(string $method, array $arguments): never
4444
{
45-
$class = static::class;
46-
throw new RuntimeException("Call to undefined method {$class}::{$method}");
45+
throw new RuntimeException(sprintf('Call to undefined method %s::%s', static::class, $method));
4746
}
4847

4948
/**

src/Codeception/Application.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ class Application extends BaseApplication
2727
*/
2828
public function registerCustomCommands(): void
2929
{
30+
$output = new ConsoleOutput();
3031
try {
3132
$this->readCustomCommandsFromConfig();
3233
} catch (ConfigurationException $e) {
3334
if ($e->getCode() === 404) {
3435
return;
3536
}
36-
$this->renderExceptionWrapper($e, new ConsoleOutput());
37+
$this->renderExceptionWrapper($e, $output);
3738
exit(1);
3839
} catch (Exception $e) {
39-
$this->renderExceptionWrapper($e, new ConsoleOutput());
40+
$this->renderExceptionWrapper($e, $output);
4041
exit(1);
4142
}
4243
}
@@ -67,8 +68,7 @@ protected function readCustomCommandsFromConfig(): void
6768
}
6869

6970
foreach ($config['extensions']['commands'] as $commandClass) {
70-
$commandName = $this->getCustomCommandName($commandClass);
71-
$this->add(new $commandClass($commandName));
71+
$this->add(new $commandClass($this->getCustomCommandName($commandClass)));
7272
}
7373
}
7474

@@ -84,11 +84,10 @@ protected function getCustomCommandName(string $commandClass): string
8484
throw new ConfigurationException("Extension: Command class {$commandClass} not found");
8585
}
8686

87-
$interfaces = class_implements($commandClass);
88-
89-
if (!in_array(CustomCommandInterface::class, $interfaces)) {
90-
throw new ConfigurationException("Extension: Command {$commandClass} must implement " .
91-
"the interface `Codeception\\CustomCommandInterface`");
87+
if (!is_subclass_of($commandClass, CustomCommandInterface::class)) {
88+
throw new ConfigurationException(
89+
"Extension: Command {$commandClass} must implement the interface `Codeception\\CustomCommandInterface`"
90+
);
9291
}
9392

9493
return $commandClass::getCommandName();
@@ -140,30 +139,31 @@ protected function getCoreArguments(): SymfonyArgvInput
140139
}
141140

142141
$argvWithoutConfig = [];
143-
if (isset($_SERVER['argv'])) {
144-
$argv = $_SERVER['argv'];
145-
146-
for ($i = 0; $i < count($argv); ++$i) {
147-
if (preg_match('#^(?:-([^c-]*)?c|--config(?:=|$))(.*)$#', $argv[$i], $match)) {
148-
if (!empty($match[2])) { //same index
149-
$this->preloadConfiguration($match[2]);
150-
} elseif (isset($argv[$i + 1])) { //next index
151-
$this->preloadConfiguration($argv[++$i]);
152-
}
153-
if (!empty($match[1])) {
154-
$argvWithoutConfig[] = "-" . $match[1]; //rest commands
142+
$argv = $_SERVER['argv'] ?? [];
143+
144+
for ($i = 0, $count = count($argv); $i < $count; ++$i) {
145+
if (preg_match('#^(?:-([^c-]*)?c|--config(?:=|$))(.*)$#', $argv[$i], $match)) {
146+
$value = $match[2] !== '' ? $match[2] : ($argv[$i + 1] ?? '');
147+
if ($value !== '') {
148+
$this->preloadConfiguration($value);
149+
if ($match[2] === '') {
150+
++$i;
155151
}
156-
continue;
157152
}
158-
$argvWithoutConfig[] = $argv[$i];
153+
if (!empty($match[1])) {
154+
$argvWithoutConfig[] = '-' . $match[1];
155+
}
156+
continue;
159157
}
158+
159+
$argvWithoutConfig[] = $argv[$i];
160160
}
161161

162162
return $this->coreArguments = new SymfonyArgvInput($argvWithoutConfig);
163163
}
164164

165165
/**
166-
* Pre load Configuration, the config option is use.
166+
* Preload Configuration, the config option is use.
167167
*
168168
* @param string $configFile Path to Configuration
169169
* @throws ConfigurationException
@@ -173,7 +173,7 @@ protected function preloadConfiguration(string $configFile): void
173173
try {
174174
Configuration::config($configFile);
175175
} catch (ConfigurationException $e) {
176-
if ($e->getCode() == 404) {
176+
if ($e->getCode() === 404) {
177177
throw new ConfigurationException("Your configuration file `{$configFile}` could not be found.", 405);
178178
}
179179
throw $e;

src/Codeception/Codecept.php

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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,28 @@ 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+
private function addSubscribers(array $subscribers): void
111+
{
112+
foreach ($subscribers as $subscriber) {
113+
$this->dispatcher->addSubscriber($subscriber);
114+
}
111115
}
112116

113117
public function registerSubscribers(): void
114118
{
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+
125129
if (!$this->options['no-rebuild']) {
126130
$this->dispatcher->addSubscriber(new AutoRebuild());
127131
}
@@ -131,10 +135,12 @@ public function registerSubscribers(): void
131135
}
132136

133137
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+
]);
138144
}
139145

140146
if ($this->options['report']) {
@@ -157,10 +163,7 @@ private function isConsolePrinterSubscribed(): bool
157163
{
158164
foreach ($this->dispatcher->getListeners() as $listeners) {
159165
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)) {
164167
return true;
165168
}
166169
}
@@ -176,33 +179,24 @@ private function registerReporters(): void
176179
''
177180
);
178181
}
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+
}
193192
}
194193
}
195194

196195
public function run(string $suite, ?string $test = null, ?array $config = null): void
197196
{
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');
205198

199+
$config = Configuration::suiteSettings($suite, $config ?: Configuration::config());
206200
$selectedEnvironments = $this->options['env'];
207201

208202
if (!$selectedEnvironments || empty($config['env'])) {
@@ -214,9 +208,7 @@ public function run(string $suite, ?string $test = null, ?array $config = null):
214208
foreach (array_unique($selectedEnvironments) as $envList) {
215209
$envSet = explode(',', (string) $envList);
216210
$suiteEnvConfig = $config;
217-
218-
// contains a list of the environments used in this suite configuration env set.
219-
$envConfigs = [];
211+
$envConfigs = [];
220212
foreach ($envSet as $currentEnv) {
221213
// The $settings['env'] actually contains all parsed configuration files as a
222214
// 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):
225217
return;
226218
}
227219

228-
// Merge configuration consecutively with already build configuration
229220
if (is_array($config['env'][$currentEnv])) {
230221
$suiteEnvConfig = Configuration::mergeConfigs($suiteEnvConfig, $config['env'][$currentEnv]);
231222
}
232-
$envConfigs[] = $currentEnv;
223+
$envConfigs[] = $currentEnv;
233224
}
234225

235226
$suiteEnvConfig['current_environment'] = implode(',', $envConfigs);
236227

237-
$suiteToRun = $suite;
238-
if (!empty($envList)) {
239-
$suiteToRun .= ' (' . implode(', ', $envSet) . ')';
240-
}
228+
$suiteToRun = $suite . (empty($envList) ? '' : ' (' . implode(', ', $envSet) . ')');
241229
$this->runSuite($suiteEnvConfig, $suiteToRun, $test);
242230
}
243231
}
244232

245233
public function runSuite(array $settings, string $suite, ?string $test = null): void
246234
{
247235
$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);
249237
$suiteManager->initialize();
250238
mt_srand($this->options['seed']);
251239
$suiteManager->loadTests($test);

0 commit comments

Comments
 (0)