-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShellCommandTest.php
More file actions
226 lines (205 loc) · 8.34 KB
/
ShellCommandTest.php
File metadata and controls
226 lines (205 loc) · 8.34 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
declare(strict_types=1);
namespace PHPSu\ShellCommandBuilder\Tests;
use PHPSu\ShellCommandBuilder\Definition\GroupType;
use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException;
use PHPSu\ShellCommandBuilder\ShellBuilder;
use PHPSu\ShellCommandBuilder\ShellCommand;
use PHPUnit\Framework\TestCase;
class ShellCommandTest extends TestCase
{
public function testShellCommand(): void
{
$command = new ShellCommand('mariadb-dump');
$command->addShortOption('u', 'username')
->addShortOption('p', 'password')
->addShortOption('h', '127.0.0.1')
->addArgument('database')
->addOption('skip-comments')
;
$this->assertEquals("mariadb-dump -u 'username' -p 'password' -h '127.0.0.1' 'database' --skip-comments", (string)$command);
}
public function testShellCommandWithEnvironmentVariables(): void
{
$command = new ShellCommand('grep');
$command->addEnv('grep_color', '1;35')
->addOption('color', 'always')
->addArgument('root')
->addArgument('/etc/passwd', false);
$this->assertEquals('GREP_COLOR=\'1;35\' grep --color \'always\' \'root\' /etc/passwd', (string)$command);
}
public function testShellCommandWithCommandSubstitution(): void
{
$command = new ShellCommand('ls');
$command->addShortOption('ld')
->addNoSpaceArgument(
(new ShellCommand('date'))
->addArgument('+%B', false)
->toggleCommandSubstitution()
)
->addArgument('.txt', false)
;
$this->assertEquals("ls -ld $(date +%B).txt", (string)$command);
}
public function testShellCommandWithProcessSubstitution(): void
{
$command = new ShellCommand('diff');
$command->addArgument((new ShellCommand('date'))
->addArgument('+%B', false)
->isProcessSubstitution(), false);
$this->assertEquals("diff <(date +%B)", (string)$command);
}
public function testSwitchSubstitutionType(): void
{
$subCommand = (new ShellCommand('date'))
->addArgument('+%B', false)
->isProcessSubstitution();
$command = new ShellCommand('diff');
$command->addArgument($subCommand, false);
$this->assertEquals("diff <(date +%B)", (string)$command);
$subCommand->toggleCommandSubstitution();
$this->assertEquals("diff date +%B", (string)$command);
$subCommand->isProcessSubstitution(false);
$this->assertEquals("diff $(date +%B)", (string)$command);
}
public function testShellCommandWithInvertedOutput(): void
{
$command = new ShellCommand('echo');
$command->invert()->addShortOption('e', 'hello world');
$this->assertEquals('! echo -e \'hello world\'', (string)$command);
}
public function testEscapeOptionWithAssignOperator(): void
{
$command = (string)(new ShellCommand('ls'))->addOption('color', 'true', true, true);
$this->assertEquals("ls --color='true'", $command);
}
public function testShellCommandToArray(): void
{
$command = (new ShellCommand('ls'))->addOption('color', 'true', true, true)->__toArray();
$this->assertEquals('ls', $command['executable']);
$this->assertEquals([
[
'isArgument' => false,
'isShortOption' => false,
'isOption' => true,
'isEnvironmentVariable' => false,
'isVariable' => false,
'escaped' => true,
'withAssign' => true,
'spaceAfterValue' => true,
'value' => '\'true\'',
'argument' => "color",
]
], $command['arguments']);
}
public function testShellCommandArgumentToArray(): void
{
$command = (new ShellCommand('ls'))->addArgument('test', false)->__toArray();
$this->assertEquals('ls', $command['executable']);
$this->assertEquals([
[
'isArgument' => true,
'isShortOption' => false,
'isOption' => false,
'isEnvironmentVariable' => false,
'isVariable' => false,
'escaped' => false,
'withAssign' => false,
'spaceAfterValue' => true,
'value' => "test",
'argument' => '',
]
], $command['arguments']);
}
public function testAccessBuilderBeforeCreatingIt(): void
{
$this->expectException(ShellBuilderException::class);
$this->expectExceptionMessage('You need to create a ShellBuilder first before you can use it within a command');
(new ShellCommand('hi'))->addToBuilder();
}
public function testShellCommandWithCommandSubstitutionToArray(): void
{
$shell = (new ShellCommand('ls'))
->addOption('color', 'true', true, true)
->addEnv('a', 'b')
;
$shell->toggleCommandSubstitution();
$this->assertEquals('$(A=\'b\' ls --color=\'true\')', (string)$shell);
$command = $shell->__toArray();
$this->assertEquals('ls', $command['executable']);
$this->assertEquals(true, $command['isCommandSubstitution']);
$this->assertEquals([
[
'isArgument' => false,
'isShortOption' => false,
'isOption' => false,
'isEnvironmentVariable' => true,
'isVariable' => false,
'escaped' => true,
'withAssign' => true,
'spaceAfterValue' => true,
'value' => '\'b\'',
'argument' => "A",
]
], $command['environmentVariables']);
$this->assertEquals([
[
'isArgument' => false,
'isShortOption' => false,
'isOption' => true,
'isEnvironmentVariable' => false,
'isVariable' => false,
'escaped' => true,
'withAssign' => true,
'spaceAfterValue' => true,
'value' => '\'true\'',
'argument' => "color",
]
], $command['arguments']);
}
public function testConditionalArguments()
{
$command = ShellBuilder::command('test')
->if(1 + 1 === 3, static function (ShellCommand $command) {
return $command->addOption('f', '1 + 1 = 3');
})
->if(1 + 1 === 2, static function (ShellCommand $command) {
return $command->addOption('t', '1 + 1 = 2');
});
static::assertEquals((string)$command, 'test --t \'1 + 1 = 2\'');
}
public function testUnEscapedOption(): void
{
$command = (new ShellCommand('ls'))->addOption('color', 'true', false, true);
$this->assertEquals('ls --color=true', (string)$command);
}
public function testUnEscapedNoAssignOperatorOption(): void
{
$command = (new ShellCommand('ls'))->addOption('color', 'true', false, false);
$this->assertEquals('ls --color true', (string)$command);
}
public function testShortOptionWithWrongType(): void
{
$this->expectException(ShellBuilderException::class);
$this->expectExceptionMessage('Provided the wrong type - only ShellCommand and ShellBuilder allowed');
(new ShellCommand('ls'))->addShortOption('la', false);
}
public function testOptionWithWrongType(): void
{
$this->expectException(ShellBuilderException::class);
$this->expectExceptionMessage('Provided the wrong type - only ShellCommand and ShellBuilder allowed');
(new ShellCommand('ls'))->addOption('la', 124343);
}
public function testArgumentWithWrongType(): void
{
$this->expectException(ShellBuilderException::class);
$this->expectExceptionMessage('Provided the wrong type - only ShellCommand and ShellBuilder allowed');
(new ShellCommand('ls'))->addArgument(new \DateTime());
}
public function testNoSpaceArgumentWithWrongType(): void
{
$this->expectException(ShellBuilderException::class);
$this->expectExceptionMessage('Provided the wrong type - only ShellCommand and ShellBuilder allowed');
(new ShellCommand('ls'))->addNoSpaceArgument(new GroupType());
}
}