-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathDuskCommand.php
359 lines (306 loc) · 8.33 KB
/
DuskCommand.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
namespace Laravel\Dusk\Console;
use Dotenv\Dotenv;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Exception\ProcessSignaledException;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;
class DuskCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dusk
{--browse : Open a browser instead of using headless mode}
{--without-tty : Disable output to TTY}
{--pest : Run the tests using Pest}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run the Dusk tests for the application';
/**
* Indicates if the project has its own PHPUnit configuration.
*
* @var bool
*/
protected $hasPhpUnitConfiguration = false;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->ignoreValidationErrors();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->purgeScreenshots();
$this->purgeConsoleLogs();
$this->purgeSourceLogs();
$options = collect($_SERVER['argv'])
->slice(2)
->diff(['--browse', '--without-tty'])
->values()
->all();
return $this->withDuskEnvironment(function () use ($options) {
$process = (new Process(array_merge(
$this->binary(), $this->phpunitArguments($options)
), null, $this->env()))->setTimeout(null);
try {
$process->setTty(! $this->option('without-tty'));
} catch (RuntimeException $e) {
$this->output->writeln('Warning: '.$e->getMessage());
}
try {
return $process->run(function ($type, $line) {
$this->output->write($line);
});
} catch (ProcessSignaledException $e) {
if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) {
throw $e;
}
}
});
}
/**
* Get the PHP binary to execute.
*
* @return array
*/
protected function binary()
{
$binaryPath = 'vendor/phpunit/phpunit/phpunit';
if ($this->option('pest')) {
$binaryPath = 'vendor/pestphp/pest/bin/pest';
}
if ('phpdbg' === PHP_SAPI) {
return [PHP_BINARY, '-qrr', $binaryPath];
}
return [PHP_BINARY, $binaryPath];
}
/**
* Get the array of arguments for running PHPUnit.
*
* @param array $options
* @return array
*/
protected function phpunitArguments($options)
{
$options = array_values(array_filter($options, function ($option) {
return ! Str::startsWith($option, ['--env=', '--pest']);
}));
if (! file_exists($file = base_path('phpunit.dusk.xml'))) {
$file = base_path('phpunit.dusk.xml.dist');
}
return array_merge(['-c', $file], $options);
}
/**
* Get the PHP binary environment variables.
*
* @return array|null
*/
protected function env()
{
if ($this->option('browse') && ! isset($_ENV['CI']) && ! isset($_SERVER['CI'])) {
return ['DUSK_HEADLESS_DISABLED' => true];
}
}
/**
* Purge the failure screenshots.
*
* @return void
*/
protected function purgeScreenshots()
{
$this->purgeDebuggingFiles(
base_path('tests/Browser/screenshots'), 'failure-*'
);
}
/**
* Purge the console logs.
*
* @return void
*/
protected function purgeConsoleLogs()
{
$this->purgeDebuggingFiles(
base_path('tests/Browser/console'), '*.log'
);
}
/**
* Purge the source logs.
*
* @return void
*/
protected function purgeSourceLogs()
{
$this->purgeDebuggingFiles(
base_path('tests/Browser/source'), '*.txt'
);
}
/**
* Purge debugging files based on path and patterns.
*
* @param string $path
* @param string $patterns
* @return void
*/
protected function purgeDebuggingFiles($path, $patterns)
{
if (! is_dir($path)) {
return;
}
$files = Finder::create()->files()
->in($path)
->name($patterns);
foreach ($files as $file) {
@unlink($file->getRealPath());
}
}
/**
* Run the given callback with the Dusk configuration files.
*
* @param \Closure $callback
* @return mixed
*/
protected function withDuskEnvironment($callback)
{
$this->setupDuskEnvironment();
try {
return $callback();
} finally {
$this->teardownDuskEnviroment();
}
}
/**
* Setup the Dusk environment.
*
* @return void
*/
protected function setupDuskEnvironment()
{
if (file_exists(base_path($this->duskFile()))) {
if (file_get_contents(base_path('.env')) !== file_get_contents(base_path($this->duskFile()))) {
$this->backupEnvironment();
}
$this->refreshEnvironment();
}
$this->writeConfiguration();
$this->setupSignalHandler();
}
/**
* Backup the current environment file.
*
* @return void
*/
protected function backupEnvironment()
{
copy(base_path('.env'), base_path('.env.backup'));
copy(base_path($this->duskFile()), base_path('.env'));
}
/**
* Refresh the current environment variables.
*
* @return void
*/
protected function refreshEnvironment()
{
// BC fix to support Dotenv ^2.2...
if (! method_exists(Dotenv::class, 'create')) {
(new Dotenv(base_path()))->overload();
return;
}
// BC fix to support Dotenv ^3.0...
if (! method_exists(Dotenv::class, 'createMutable')) {
Dotenv::create(base_path())->overload();
return;
}
Dotenv::createMutable(base_path())->load();
}
/**
* Write the Dusk PHPUnit configuration.
*
* @return void
*/
protected function writeConfiguration()
{
if (! file_exists($file = base_path('phpunit.dusk.xml')) &&
! file_exists(base_path('phpunit.dusk.xml.dist'))) {
copy(realpath(__DIR__.'/../../stubs/phpunit.xml'), $file);
return;
}
$this->hasPhpUnitConfiguration = true;
}
/**
* Setup the SIGINT signal handler for CTRL+C exits.
*
* @return void
*/
protected function setupSignalHandler()
{
if (extension_loaded('pcntl')) {
pcntl_async_signals(true);
pcntl_signal(SIGINT, function () {
$this->teardownDuskEnviroment();
});
}
}
/**
* Restore the original environment.
*
* @return void
*/
protected function teardownDuskEnviroment()
{
$this->removeConfiguration();
if (file_exists(base_path($this->duskFile())) && file_exists(base_path('.env.backup'))) {
$this->restoreEnvironment();
}
}
/**
* Remove the Dusk PHPUnit configuration.
*
* @return void
*/
protected function removeConfiguration()
{
if (! $this->hasPhpUnitConfiguration && file_exists($file = base_path('phpunit.dusk.xml'))) {
unlink($file);
}
}
/**
* Restore the backed-up environment file.
*
* @return void
*/
protected function restoreEnvironment()
{
copy(base_path('.env.backup'), base_path('.env'));
unlink(base_path('.env.backup'));
}
/**
* Get the name of the Dusk file for the environment.
*
* @return string
*/
protected function duskFile()
{
if (file_exists(base_path($file = '.env.dusk.'.$this->laravel->environment()))) {
return $file;
}
return '.env.dusk';
}
}