1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NunoMaduro\Collision\Adapters\Laravel\Commands; |
4
|
|
|
|
5
|
|
|
use Dotenv\Dotenv; |
6
|
|
|
use Dotenv\Repository\RepositoryBuilder; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Symfony\Component\Process\Exception\ProcessSignaledException; |
11
|
|
|
use Symfony\Component\Process\Process; |
12
|
|
|
|
13
|
|
|
class TestCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The name and signature of the console command. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'test {--without-tty : Disable output to TTY}'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Run the application tests'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The arguments to be used while calling phpunit. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $arguments = [ |
35
|
|
|
'--printer', |
36
|
|
|
'NunoMaduro\Collision\Adapters\Phpunit\Printer', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new command instance. |
41
|
|
|
* |
42
|
|
|
* @return void |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
parent::__construct(); |
47
|
|
|
|
48
|
|
|
$this->ignoreValidationErrors(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Execute the console command. |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function handle() |
57
|
|
|
{ |
58
|
|
|
$options = array_slice($_SERVER['argv'], $this->option('without-tty') ? 3 : 2); |
59
|
|
|
|
60
|
|
|
$this->clearEnv(); |
61
|
|
|
|
62
|
|
|
$process = (new Process(array_merge( |
63
|
|
|
$this->binary(), |
64
|
|
|
array_merge( |
65
|
|
|
$this->arguments, |
66
|
|
|
$this->phpunitArguments($options) |
67
|
|
|
) |
68
|
|
|
)))->setTimeout(null); |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$process->setTty(!$this->option('without-tty')); |
72
|
|
|
} catch (RuntimeException $e) { |
73
|
|
|
$this->output->writeln('Warning: ' . $e->getMessage()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
return $process->run(function ($type, $line) { |
78
|
|
|
$this->output->write($line); |
79
|
|
|
}); |
80
|
|
|
} catch (ProcessSignaledException $e) { |
81
|
|
|
if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) { |
82
|
|
|
throw $e; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the PHP binary to execute. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
protected function binary() |
93
|
|
|
{ |
94
|
|
|
if ('phpdbg' === PHP_SAPI) { |
95
|
|
|
return [PHP_BINARY, '-qrr', 'vendor/phpunit/phpunit/phpunit']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return [PHP_BINARY, 'vendor/phpunit/phpunit/phpunit']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the array of arguments for running PHPUnit. |
103
|
|
|
* |
104
|
|
|
* @param array $options |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
protected function phpunitArguments($options) |
109
|
|
|
{ |
110
|
|
|
$options = array_values(array_filter($options, function ($option) { |
111
|
|
|
return !Str::startsWith($option, '--env='); |
112
|
|
|
})); |
113
|
|
|
|
114
|
|
|
if (!file_exists($file = base_path('phpunit.xml'))) { |
115
|
|
|
$file = base_path('phpunit.xml.dist'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return array_merge(['-c', $file], $options); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Clears any set Environment variables set by Laravel if the --env option is empty. |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
protected function clearEnv() |
127
|
|
|
{ |
128
|
|
|
if (!$this->option('env')) { |
129
|
|
|
$repositories = RepositoryBuilder::create() |
130
|
|
|
->make(); |
131
|
|
|
|
132
|
|
|
$envs = Dotenv::create( |
133
|
|
|
$repositories, |
134
|
|
|
$this->laravel->environmentPath(), |
|
|
|
|
135
|
|
|
$this->laravel->environmentFile() |
|
|
|
|
136
|
|
|
)->safeLoad(); |
137
|
|
|
|
138
|
|
|
foreach ($envs as $name => $value) { |
139
|
|
|
$repositories->clear($name); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.