forked from laravel/valet
-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathFunctionalTestCase.php
86 lines (74 loc) · 2.14 KB
/
FunctionalTestCase.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
<?php
namespace Valet\Tests\Functional;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Process\Process;
class FunctionalTestCase extends TestCase
{
/**
* Execute valet command.
* Fail if exit code is different from 0.
*
* @param string $command
* @param null|string $workingDir
* @return string
*/
protected function valetCommand($command, $workingDir = null)
{
return $this->exec($this->valet() . ' ' . $command, $workingDir);
}
/**
* Get valet prefix for commands.
*
* @return string
*/
protected function valet()
{
if (isset($_SERVER['REPOSITORY'])) {
return $_SERVER['REPOSITORY'] . '/valet';
}
return 'valet';
}
/**
* Pass the command to the command line and display the output.
* Fail if exit code is different from 0.
*
* @param string $command
* @param null|string $workingDir
* @return string
*/
protected function exec($command, $workingDir = null)
{
$process = new Process($command);
$process->setWorkingDirectory(is_null($workingDir) ? realpath(__DIR__ . '/../..') : $workingDir);
$processOutput = '';
$process->setTimeout(null)->run(function ($type, $line) use (&$processOutput) {
$processOutput .= $line;
});
if ($process->getExitCode() > 0) {
throw new RuntimeException(
'Command "' . $command . '" exited with exit code ' . $process->getExitCode() . PHP_EOL .
$processOutput
);
}
return $processOutput;
}
/**
* Run a command in the background.
*
* @param string $command
* @param null|string $workingDir
* @return Process
*/
protected function background($command, $workingDir = null)
{
$process = new Process($command);
$process
->setWorkingDirectory(
is_null($workingDir) ? realpath(__DIR__ . '/../..') : $workingDir
)
->setTimeout(null)
->start();
return $process;
}
}