Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb4c2bcd

Browse files
TavoNiievezW0rma
andauthored
Simplify src root classes (#6849)
* Simplify src root classes* fix: add type annotation for $subscribers parameter---------Co-authored-by: Dieter Beck <beck.worma@gmail.com>
1 parenta1e931b commitb4c2bcd

17 files changed

+530
-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-
publicfunction__construct(protectedScenario$scenario)
18+
publicfunction__construct(protectedreadonlyScenario$scenario)
1919
{
2020
}
2121

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

43-
publicfunction__call(string$method,array$arguments)
43+
publicfunction__call(string$method,array$arguments): never
4444
{
45-
$class =static::class;
46-
thrownewRuntimeException("Call to undefined method{$class}::{$method}");
45+
thrownewRuntimeException(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
publicfunctionregisterCustomCommands():void
2929
{
30+
$output =newConsoleOutput();
3031
try {
3132
$this->readCustomCommandsFromConfig();
3233
}catch (ConfigurationException$e) {
3334
if ($e->getCode() ===404) {
3435
return;
3536
}
36-
$this->renderExceptionWrapper($e,newConsoleOutput());
37+
$this->renderExceptionWrapper($e,$output);
3738
exit(1);
3839
}catch (Exception$e) {
39-
$this->renderExceptionWrapper($e,newConsoleOutput());
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
thrownewConfigurationException("Extension: Command class{$commandClass} not found");
8585
}
8686

87-
$interfaces =class_implements($commandClass);
88-
89-
if (!in_array(CustomCommandInterface::class,$interfaces)) {
90-
thrownewConfigurationException("Extension: Command{$commandClass} must implement" .
91-
"the interface `Codeception\\CustomCommandInterface`");
87+
if (!is_subclass_of($commandClass, CustomCommandInterface::class)) {
88+
thrownewConfigurationException(
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 =newSymfonyArgvInput($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
thrownewConfigurationException("Your configuration file `{$configFile}` could not be found.",405);
178178
}
179179
throw$e;

‎src/Codeception/Codecept.php‎

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@ class Codecept
8484
publicfunction__construct(array$options = [])
8585
{
8686
$this->resultAggregator =newResultAggregator();
87-
$this->dispatcher =newEventDispatcher();
88-
$this->extensionLoader =newExtensionLoader($this->dispatcher);
87+
$this->dispatcher=newEventDispatcher();
88+
$this->extensionLoader=newExtensionLoader($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 =newOutput($this->options);
9796

@@ -105,23 +104,31 @@ public function __construct(array $options = [])
105104
*/
106105
protectedfunctionmergeOptions(array$options):array
107106
{
108-
$config = Configuration::config();
109-
$baseOptions =array_merge($this->options,$config['settings']);
110-
returnarray_merge($baseOptions,$options);
107+
returnarray_merge($this->options, Configuration::config()['settings'],$options);
108+
}
109+
110+
/**
111+
* \Symfony\Component\EventDispatcher\EventSubscriberInterface[] $subscribers
112+
*/
113+
privatefunctionaddSubscribers(array$subscribers):void
114+
{
115+
foreach ($subscribersas$subscriber) {
116+
$this->dispatcher->addSubscriber($subscriber);
117+
}
111118
}
112119

113120
publicfunctionregisterSubscribers():void
114121
{
115-
// required
116-
$this->dispatcher->addSubscriber(newGracefulTermination($this->resultAggregator));
117-
$this->dispatcher->addSubscriber(newErrorHandler());
118-
$this->dispatcher->addSubscriber(newDependencies());
119-
$this->dispatcher->addSubscriber(newBootstrap());
120-
$this->dispatcher->addSubscriber(newPrepareTest());
121-
$this->dispatcher->addSubscriber(newModule());
122-
$this->dispatcher->addSubscriber(newBeforeAfterTest());
123-
124-
// optional
122+
$this->addSubscribers([
123+
newGracefulTermination($this->resultAggregator),
124+
newErrorHandler(),
125+
newDependencies(),
126+
newBootstrap(),
127+
newPrepareTest(),
128+
newModule(),
129+
newBeforeAfterTest(),
130+
]);
131+
125132
if (!$this->options['no-rebuild']) {
126133
$this->dispatcher->addSubscriber(newAutoRebuild());
127134
}
@@ -131,10 +138,12 @@ public function registerSubscribers(): void
131138
}
132139

133140
if ($this->options['coverage']) {
134-
$this->dispatcher->addSubscriber(newLocal($this->options));
135-
$this->dispatcher->addSubscriber(newLocalServer($this->options));
136-
$this->dispatcher->addSubscriber(newRemoteServer($this->options));
137-
$this->dispatcher->addSubscriber(newCoveragePrinter($this->options,$this->output));
141+
$this->addSubscribers([
142+
newLocal($this->options),
143+
newLocalServer($this->options),
144+
newRemoteServer($this->options),
145+
newCoveragePrinter($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 ($listenersas$listener) {
160-
if ($listenerinstanceof ConsolePrinter) {
161-
returntrue;
162-
}
163-
if (is_array($listener) &&$listener[0]instanceof ConsolePrinter) {
169+
if ($listenerinstanceof ConsolePrinter || (is_array($listener) &&$listener[0]instanceof ConsolePrinter)) {
164170
returntrue;
165171
}
166172
}
@@ -176,33 +182,24 @@ private function registerReporters(): void
176182
''
177183
);
178184
}
179-
if ($this->options['html']) {
180-
$this->dispatcher->addSubscriber(
181-
newHtmlReporter($this->options,$this->output)
182-
);
183-
}
184-
if ($this->options['xml']) {
185-
$this->dispatcher->addSubscriber(
186-
newJUnitReporter($this->options,$this->output)
187-
);
188-
}
189-
if ($this->options['phpunit-xml']) {
190-
$this->dispatcher->addSubscriber(
191-
newPhpUnitReporter($this->options,$this->output)
192-
);
185+
186+
$map = [
187+
'html' =>fn () =>newHtmlReporter($this->options,$this->output),
188+
'xml' =>fn () =>newJUnitReporter($this->options,$this->output),
189+
'phpunit-xml' =>fn () =>newPhpUnitReporter($this->options,$this->output),
190+
];
191+
foreach ($mapas$flag =>$create) {
192+
if ($this->options[$flag]) {
193+
$this->dispatcher->addSubscriber($create());
194+
}
193195
}
194196
}
195197

196198
publicfunctionrun(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 ($envSetas$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
publicfunctionrunSuite(array$settings,string$suite, ?string$test =null):void
246237
{
247238
$settings['shard'] =$this->options['shard'];
248-
$suiteManager =newSuiteManager($this->dispatcher,$suite,$settings,$this->options);
239+
$suiteManager=newSuiteManager($this->dispatcher,$suite,$settings,$this->options);
249240
$suiteManager->initialize();
250241
mt_srand($this->options['seed']);
251242
$suiteManager->loadTests($test);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp