/
TestFrameworkExecutableFinder.php
142 lines (117 loc) · 3.86 KB
/
TestFrameworkExecutableFinder.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
<?php
/**
* Copyright © 2017 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/
declare(strict_types=1);
namespace Infection\Finder;
use Infection\Finder\Exception\TestFrameworkExecutableFinderNotFound;
use Infection\Process\ExecutableFinder\PhpExecutableFinder;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
class TestFrameworkExecutableFinder extends AbstractExecutableFinder
{
/**
* @var string
*/
private $cachedExecutable;
/**
* @var string
*/
private $testFrameworkName;
/**
* @var string
*/
private $customPath;
/**
* @var bool
*/
private $cachedIncludedArgs;
public function __construct(string $testFrameworkName, string $customPath = null)
{
$this->testFrameworkName = $testFrameworkName;
$this->customPath = $customPath;
}
public function find(bool $includeArgs = true): string
{
if ($this->cachedExecutable === null || $this->cachedIncludedArgs !== $includeArgs) {
if (!$this->doesCustomPathExist()) {
$this->addVendorFolderToPath();
}
$this->cachedExecutable = $this->findExecutable($includeArgs);
}
$this->cachedIncludedArgs = $includeArgs;
return $this->cachedExecutable;
}
private function addVendorFolderToPath()
{
$vendorPath = null;
try {
$process = new Process(sprintf('%s %s', $this->findComposer(), 'config bin-dir'));
$process->run();
$vendorPath = trim($process->getOutput());
} catch (\RuntimeException $e) {
$candidate = getcwd() . '/vendor/bin';
if (file_exists($candidate)) {
$vendorPath = $candidate;
}
}
if (null !== $vendorPath) {
putenv('PATH=' . $vendorPath . PATH_SEPARATOR . getenv('PATH'));
}
}
private function findComposer(): string
{
return (new ComposerExecutableFinder())->find();
}
private function findExecutable(bool $includeArgs = true): string
{
if ($this->doesCustomPathExist()) {
return $this->makeExecutable($this->customPath);
}
$candidates = [$this->testFrameworkName, $this->testFrameworkName . '.phar'];
$finder = new ExecutableFinder();
foreach ($candidates as $name) {
if ($path = $finder->find($name, null, [getcwd()])) {
return $this->makeExecutable($path, $includeArgs);
}
}
$result = $this->searchNonExecutables($candidates, [getcwd()], $includeArgs);
if (null !== $result) {
return $result;
}
throw new TestFrameworkExecutableFinderNotFound(
sprintf(
'Unable to locate a %s executable on local system. Ensure that %s is installed and available.',
$this->testFrameworkName,
$this->testFrameworkName
)
);
}
/**
* Prefix commands with exec outside Windows to ensure process timeouts
* are enforced and end PHP processes properly
*
* @param string $path
* @param bool $includeArgs
*
* @return string
*/
protected function makeExecutable(string $path, bool $includeArgs = true): string
{
$path = realpath($path);
$phpFinder = new PhpExecutableFinder();
if (\defined('PHP_WINDOWS_VERSION_BUILD')) {
if (false !== strpos($path, '.bat')) {
return $path;
}
return sprintf('%s %s', $phpFinder->find($includeArgs), $path);
}
return sprintf('%s %s %s', 'exec', $phpFinder->find($includeArgs), $path);
}
private function doesCustomPathExist(): bool
{
return $this->customPath && file_exists($this->customPath);
}
}