-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Codecept.php
287 lines (243 loc) · 9.76 KB
/
Codecept.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
declare(strict_types=1);
namespace Codeception;
use Codeception\Coverage\Subscriber\Local;
use Codeception\Coverage\Subscriber\LocalServer;
use Codeception\Coverage\Subscriber\Printer as CoveragePrinter;
use Codeception\Coverage\Subscriber\RemoteServer;
use Codeception\Event\PrintResultEvent;
use Codeception\Exception\ConfigurationException;
use Codeception\Lib\Console\Output;
use Codeception\Lib\Interfaces\ConsolePrinter;
use Codeception\Lib\Notification;
use Codeception\Reporter\HtmlReporter;
use Codeception\Reporter\JUnitReporter;
use Codeception\Reporter\PhpUnitReporter;
use Codeception\Reporter\ReportPrinter;
use Codeception\Subscriber\AutoRebuild;
use Codeception\Subscriber\BeforeAfterTest;
use Codeception\Subscriber\Bootstrap;
use Codeception\Subscriber\Console;
use Codeception\Subscriber\Dependencies;
use Codeception\Subscriber\Deprecation;
use Codeception\Subscriber\ErrorHandler;
use Codeception\Subscriber\ExtensionLoader;
use Codeception\Subscriber\FailFast;
use Codeception\Subscriber\GracefulTermination;
use Codeception\Subscriber\Module;
use Codeception\Subscriber\PrepareTest;
use Symfony\Component\EventDispatcher\EventDispatcher;
class Codecept
{
/**
* @var string
*/
public const VERSION = '5.0.0-RC4';
protected ResultAggregator $resultAggregator;
protected EventDispatcher $dispatcher;
protected ExtensionLoader $extensionLoader;
protected array $options = [
'silent' => false,
'debug' => false,
'steps' => false,
'html' => false,
'xml' => false,
'phpunit-xml' => false,
'no-redirect' => true,
'report' => false,
'colors' => false,
'coverage' => false,
'coverage-xml' => false,
'coverage-html' => false,
'coverage-text' => false,
'coverage-crap4j' => false,
'coverage-cobertura' => false,
'coverage-phpunit' => false,
'groups' => null,
'excludeGroups' => null,
'filter' => null,
'shard' => null,
'env' => null,
'fail-fast' => 0,
'ansi' => true,
'verbosity' => 1,
'interactive' => true,
'no-rebuild' => false,
'quiet' => false,
];
protected array $config = [];
protected array $extensions = [];
private Output $output;
public function __construct(array $options = [])
{
$this->resultAggregator = new ResultAggregator();
$this->dispatcher = new EventDispatcher();
$this->extensionLoader = new ExtensionLoader($this->dispatcher);
$baseOptions = $this->mergeOptions($options);
$this->extensionLoader->bootGlobalExtensions($baseOptions); // extensions may override config
$this->config = Configuration::config();
$this->options = $this->mergeOptions($options); // options updated from config
$this->output = new Output($this->options);
$this->registerSubscribers();
}
/**
* Merges given options with default values and current configuration
*
* @throws ConfigurationException
*/
protected function mergeOptions(array $options): array
{
$config = Configuration::config();
$baseOptions = array_merge($this->options, $config['settings']);
return array_merge($baseOptions, $options);
}
public function registerSubscribers(): void
{
// required
$this->dispatcher->addSubscriber(new GracefulTermination($this->resultAggregator));
$this->dispatcher->addSubscriber(new ErrorHandler());
$this->dispatcher->addSubscriber(new Dependencies());
$this->dispatcher->addSubscriber(new Bootstrap());
$this->dispatcher->addSubscriber(new PrepareTest());
$this->dispatcher->addSubscriber(new Module());
$this->dispatcher->addSubscriber(new BeforeAfterTest());
// optional
if (!$this->options['no-rebuild']) {
$this->dispatcher->addSubscriber(new AutoRebuild());
}
if ($this->options['fail-fast'] > 0) {
$this->dispatcher->addSubscriber(new FailFast($this->options['fail-fast'], $this->resultAggregator));
}
if ($this->options['coverage']) {
$this->dispatcher->addSubscriber(new Local($this->options));
$this->dispatcher->addSubscriber(new LocalServer($this->options));
$this->dispatcher->addSubscriber(new RemoteServer($this->options));
$this->dispatcher->addSubscriber(new CoveragePrinter($this->options, $this->output));
}
if ($this->options['report']) {
$this->dispatcher->addSubscriber(new ReportPrinter($this->options));
}
$this->dispatcher->addSubscriber($this->extensionLoader);
$this->extensionLoader->registerGlobalExtensions();
if (!$this->options['silent'] && !$this->isConsolePrinterSubscribed()) {
$this->dispatcher->addSubscriber(new Console($this->options));
}
$this->dispatcher->addSubscriber(new Deprecation($this->options));
$this->registerReporters();
}
private function isConsolePrinterSubscribed(): bool
{
foreach ($this->dispatcher->getListeners() as $event => $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof ConsolePrinter) {
return true;
}
if (is_array($listener) && $listener[0] instanceof ConsolePrinter) {
return true;
}
}
}
return false;
}
private function registerReporters(): void
{
if (isset($this->config['reporters'])) {
Notification::warning(
"'reporters' option is not supported! Custom reporters must be reimplemented as extensions.",
''
);
}
if ($this->options['html']) {
$this->dispatcher->addSubscriber(
new HtmlReporter($this->options, $this->output)
);
}
if ($this->options['xml']) {
$this->dispatcher->addSubscriber(
new JUnitReporter($this->options, $this->output)
);
}
if ($this->options['phpunit-xml']) {
$this->dispatcher->addSubscriber(
new PhpUnitReporter($this->options, $this->output)
);
}
}
private function absolutePath(string $path): string
{
if ((str_starts_with($path, '/')) or (strpos($path, ':') === 1)) { // absolute path
return $path;
}
return Configuration::outputDir() . $path;
}
public function run(string $suite, string $test = null, array $config = null): void
{
ini_set(
'memory_limit',
$this->config['settings']['memory_limit'] ?? '1024M'
);
$config = $config ?: Configuration::config();
$config = Configuration::suiteSettings($suite, $config);
$selectedEnvironments = $this->options['env'];
if (!$selectedEnvironments || empty($config['env'])) {
$this->runSuite($config, $suite, $test);
return;
}
// Iterate over all unique environment sets and runs the given suite with each of the merged configurations.
foreach (array_unique($selectedEnvironments) as $envList) {
$envSet = explode(',', $envList);
$suiteEnvConfig = $config;
// contains a list of the environments used in this suite configuration env set.
$envConfigs = [];
foreach ($envSet as $currentEnv) {
// The $settings['env'] actually contains all parsed configuration files as a
// filename => filecontents key-value array. If there is no configuration file for the
// $currentEnv the merge will be skipped.
if (!array_key_exists($currentEnv, $config['env'])) {
return;
}
// Merge configuration consecutively with already build configuration
if (is_array($config['env'][$currentEnv])) {
$suiteEnvConfig = Configuration::mergeConfigs($suiteEnvConfig, $config['env'][$currentEnv]);
}
$envConfigs[] = $currentEnv;
}
$suiteEnvConfig['current_environment'] = implode(',', $envConfigs);
$suiteToRun = $suite;
if (!empty($envList)) {
$suiteToRun .= ' (' . implode(', ', $envSet) . ')';
}
$this->runSuite($suiteEnvConfig, $suiteToRun, $test);
}
}
public function runSuite(array $settings, string $suite, string $test = null): void
{
$settings['shard'] = $this->options['shard'];
$suiteManager = new SuiteManager($this->dispatcher, $suite, $settings, $this->options);
$suiteManager->initialize();
srand($this->options['seed']);
$suiteManager->loadTests($test);
srand();
$suiteManager->run($this->resultAggregator);
}
public static function versionString(): string
{
return 'Codeception PHP Testing Framework v' . self::VERSION;
}
public function printResult(): void
{
$this->dispatcher->dispatch(new PrintResultEvent($this->resultAggregator), Events::RESULT_PRINT_AFTER);
}
public function getResultAggregator(): ResultAggregator
{
return $this->resultAggregator;
}
public function getOptions(): array
{
return $this->options;
}
public function getDispatcher(): EventDispatcher
{
return $this->dispatcher;
}
}