FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ShellCommandBuilder/tests/ShellCommandTest.php at main · phpsu/ShellCommandBuilder · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
phpsu
/
ShellCommandBuilder
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
3
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ShellCommandBuilder
/
tests
/
ShellCommandTest.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
195 lines (178 loc) · 7.05 KB
Breadcrumbs
ShellCommandBuilder
/
tests
/
ShellCommandTest.php
Copy path
File metadata and controls
195 lines (178 loc) · 7.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
declare
(strict_types=
1
);
namespace
PHPSu
\
ShellCommandBuilder
\
Tests
;
use
DateTime
;
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
(
'
mysql
'
);
$
command
->
addShortOption
(
'
u
'
,
'
username
'
)
->
addShortOption
(
'
p
'
,
'
password
'
)
->
addShortOption
(
'
h
'
,
'
127.0.0.1
'
)
->
addArgument
(
'
database
'
)
->
addOption
(
'
skip-comments
'
)
;
$
this
->
assertEquals
(
"
mysql -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
():
void
{
$
command
= ShellBuilder::
command
(
'
test
'
)
->
if
(
false
,
static
fn
(
ShellCommand
$
command
):
ShellCommand
=>
$
command
->
addOption
(
'
f
'
,
'
false
'
))
->
if
(
true
,
static
fn
(
ShellCommand
$
command
):
ShellCommand
=>
$
command
->
addOption
(
'
t
'
,
'
true
'
));
self
::
assertEquals
((
string
)
$
command
,
"
test --t 'true'
"
);
}
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
);
}
}
Back
|
FazBrowse Home
|
New Git URL