-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
ExecuteTest.php
122 lines (96 loc) 路 3.63 KB
/
ExecuteTest.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
<?php
declare(strict_types=1);
namespace Psl\Tests\Unit\Shell;
use PHPUnit\Framework\TestCase;
use Psl\Env;
use Psl\OS;
use Psl\SecureRandom;
use Psl\Shell;
final class ExecuteTest extends TestCase
{
public function testExecute(): void
{
static::assertSame(
"Hello, World!",
Shell\execute(PHP_BINARY, ['-r', 'echo "Hello, World!";'])
);
}
public function testFailedExecution(): void
{
try {
Shell\execute('php', ['-r', 'write("Hello, World!");']);
} catch (Shell\Exception\FailedExecutionException $exception) {
static::assertSame(255, $exception->getCode());
static::assertStringContainsString('Call to undefined function write()', $exception->getErrorOutput());
static::assertStringContainsString('php', $exception->getCommand());
}
}
public function testItThrowsForNULLByte(): void
{
if (OS\is_windows()) {
static::markTestSkipped('Test can only be executed under *nix OS.');
}
$this->expectException(Shell\Exception\PossibleAttackException::class);
Shell\execute('php', ["\0"]);
}
public function testEnvironmentIsPassedDownToTheProcess(): void
{
static::assertSame(
'BAR',
Shell\execute(PHP_BINARY, ['-r', 'echo getenv("FOO");'], null, ['FOO' => 'BAR'])
);
}
public function testCurrentEnvironmentVariablesArePassedDownToTheProcess(): void
{
try {
Env\set_var('FOO', 'BAR');
static::assertSame(
'BAR',
Shell\execute(PHP_BINARY, ['-r', 'echo getenv("FOO");'])
);
} finally {
Env\remove_var('FOO');
}
}
public function testWorkingDirectoryIsUsed(): void
{
$temp = Env\temp_dir();
$result = Shell\execute(PHP_BINARY, ['-r', 'echo getcwd();'], $temp);
static::assertStringEndsWith($temp, $result);
}
public function testCurrentDirectoryIsUsedByDefault(): void
{
$dir = Env\current_dir();
static::assertSame(
$dir,
Shell\execute(PHP_BINARY, ['-r', 'echo getcwd();'])
);
}
public function testItThrowsWhenWorkingDirectoryDoesntExist(): void
{
$dir = Env\current_dir() . DIRECTORY_SEPARATOR . SecureRandom\string(6);
$this->expectException(Shell\Exception\RuntimeException::class);
$this->expectExceptionMessage('$working_directory does not exist.');
Shell\execute(PHP_BINARY, ['-r', 'echo getcwd();'], $dir);
}
public function testErrorOutputIsDiscarded(): void
{
$result = Shell\execute(PHP_BINARY, ['-r', 'fwrite(STDOUT, "hello"); fwrite(STDERR, " world");']);
static::assertSame('hello', $result);
}
public function testErrorOutputIsAppended(): void
{
$result = Shell\execute(PHP_BINARY, ['-r', 'fwrite(STDOUT, "hello"); fwrite(STDERR, " world");'], error_output_behavior: Shell\ErrorOutputBehavior::APPEND);
static::assertSame('hello world', $result);
}
public function testErrorOutputIsPrepended(): void
{
$result = Shell\execute(PHP_BINARY, ['-r', 'fwrite(STDOUT, "hello"); fwrite(STDERR, " world");'], error_output_behavior: Shell\ErrorOutputBehavior::PREPEND);
static::assertSame(' worldhello', $result);
}
public function testErrorOutputIsReplacingStandardOutput(): void
{
$result = Shell\execute(PHP_BINARY, ['-r', 'fwrite(STDOUT, "hello"); fwrite(STDERR, " world");'], error_output_behavior: Shell\ErrorOutputBehavior::REPLACE);
static::assertSame(' world', $result);
}
}