1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Collision. |
5
|
|
|
* |
6
|
|
|
* (c) Nuno Maduro <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace NunoMaduro\Collision\Adapters\Phpunit; |
13
|
|
|
|
14
|
|
|
use RuntimeException; |
15
|
|
|
use Symfony\Component\Console\Style\OutputStyle; |
16
|
|
|
use Symfony\Component\Process\Exception\ProcessSignaledException; |
17
|
|
|
use Symfony\Component\Process\Process; |
18
|
|
|
|
19
|
|
|
class TestRunner |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Test runner working path. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $workingPath; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Disable output to TTY. |
30
|
|
|
* |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
protected $withoutTty; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The arguments to be used while calling phpunit. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $arguments = [ |
41
|
|
|
'--printer', |
42
|
|
|
'NunoMaduro\Collision\Adapters\Phpunit\Printer', |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Creates a new instance of the TestRunner. |
47
|
|
|
* |
48
|
|
|
* @param string $workingPath |
49
|
|
|
* @param bool $withoutTty |
50
|
|
|
*/ |
51
|
|
|
public function __construct(string $workingPath, bool $withoutTty = false) |
52
|
|
|
{ |
53
|
|
|
$this->workingPath = $workingPath; |
54
|
|
|
$this->withoutTty = $withoutTty; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Handle the test runner. |
59
|
|
|
* |
60
|
|
|
* @param \Symfony\Component\Console\Style\OutputStyle $output |
61
|
|
|
* @param array $options |
62
|
|
|
* |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
|
|
public function __invoke(OutputStyle $output, array $options): int |
66
|
|
|
{ |
67
|
|
|
$process = (new Process(array_merge( |
68
|
|
|
$this->binary(), array_merge( |
69
|
|
|
$this->arguments, |
70
|
|
|
$this->phpunitArguments($options) |
71
|
|
|
) |
72
|
|
|
)))->setTimeout(null); |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$process->setTty(! $this->withoutTty); |
76
|
|
|
} catch (RuntimeException $e) { |
77
|
|
|
$output->writeln('Warning: '.$e->getMessage()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
return $process->run(function ($type, $line) use ($output) { |
82
|
|
|
$output->write($line); |
83
|
|
|
}); |
84
|
|
|
} catch (ProcessSignaledException $e) { |
85
|
|
|
if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) { |
86
|
|
|
throw $e; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return 0; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the PHP binary to execute. |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function binary(): array |
99
|
|
|
{ |
100
|
|
|
if ('phpdbg' === PHP_SAPI) { |
101
|
|
|
return [PHP_BINARY, '-qrr', 'vendor/phpunit/phpunit/phpunit']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return [PHP_BINARY, 'vendor/phpunit/phpunit/phpunit']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the array of arguments for running PHPUnit. |
109
|
|
|
* |
110
|
|
|
* @param array $options |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
protected function phpunitArguments(array $options): array |
115
|
|
|
{ |
116
|
|
|
$options = array_values(array_filter($options, function ($option) { |
117
|
|
|
return strpos($option, '--env=') !== 0; |
118
|
|
|
})); |
119
|
|
|
|
120
|
|
|
if (! file_exists($file = "{$this->workingPath}/phpunit.xml")) { |
121
|
|
|
$file = "{$this->workingPath}/phpunit.xml.dist"; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return array_merge(['-c', $file], $options); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|