1 | <?php |
||
2 | /* |
||
3 | * This file is part of the Noiselabs ZfTestCase Behat Extension. |
||
4 | * |
||
5 | * (c) Vítor Brandão <[email protected]> |
||
6 | * |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | */ |
||
10 | |||
11 | use Symfony\Component\Filesystem\Filesystem; |
||
12 | use Symfony\Component\Process\PhpExecutableFinder; |
||
13 | use Symfony\Component\Process\Process; |
||
14 | |||
15 | /** |
||
16 | * Class BehatRunner. |
||
17 | * |
||
18 | * Adapted from https://github.com/jakzal/BehatRestExtension/blob/master/features/bootstrap/BehatRunner.php |
||
19 | */ |
||
20 | class BehatRunner |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $workingDir; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $phpBin; |
||
31 | |||
32 | /** |
||
33 | * @var Process |
||
34 | */ |
||
35 | private $process; |
||
36 | |||
37 | /** |
||
38 | * @var Filesystem |
||
39 | */ |
||
40 | private $filesystem; |
||
41 | |||
42 | /** |
||
43 | * @param string $workingDir |
||
44 | */ |
||
45 | public function __construct($workingDir) |
||
46 | { |
||
47 | $this->workingDir = $workingDir; |
||
48 | $this->phpBin = $this->findPhpBinary(); |
||
49 | $this->process = new Process(null); |
||
50 | $this->filesystem = new Filesystem(); |
||
51 | |||
52 | $this->filesystem->mkdir($this->workingDir, 0777); |
||
53 | } |
||
54 | |||
55 | public function removeWorkspace() |
||
56 | { |
||
57 | $this->filesystem->remove($this->workingDir); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $filePath |
||
62 | * @param string $content |
||
63 | */ |
||
64 | public function addFile($filePath, $content) |
||
65 | { |
||
66 | $this->filesystem->dumpFile($this->workingDir . '/' . $filePath, $content); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $path |
||
71 | */ |
||
72 | public function addDirectory($path) |
||
73 | { |
||
74 | $this->filesystem->mirror($path, $this->workingDir); |
||
75 | } |
||
76 | |||
77 | public function run() |
||
78 | { |
||
79 | $this->process->setWorkingDirectory($this->workingDir); |
||
80 | $this->process->setCommandLine( |
||
81 | sprintf( |
||
82 | '%s %s %s', |
||
83 | $this->phpBin, |
||
84 | escapeshellarg(BEHAT_BIN_PATH), |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
85 | strtr('--format-settings=\'{"timer": false}\'', ['\'' => '"', '"' => '\"']) |
||
86 | ) |
||
87 | ); |
||
88 | $this->process->start(); |
||
89 | $this->process->wait(); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return int|null |
||
94 | */ |
||
95 | public function getExitCode() |
||
96 | { |
||
97 | return $this->process->getExitCode(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getFullOutput() |
||
104 | { |
||
105 | return $this->process->getErrorOutput() . $this->process->getOutput(); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | * |
||
111 | * @throws \RuntimeException |
||
112 | */ |
||
113 | private function findPhpBinary() |
||
114 | { |
||
115 | $phpFinder = new PhpExecutableFinder(); |
||
116 | |||
117 | if (false === $php = $phpFinder->find()) { |
||
118 | throw new \RuntimeException('Unable to find the PHP executable.'); |
||
119 | } |
||
120 | |||
121 | return $php; |
||
122 | } |
||
123 | } |