-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathTest_Arguments.php
More file actions
322 lines (280 loc) · 9.46 KB
/
Test_Arguments.php
File metadata and controls
322 lines (280 loc) · 9.46 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use WP_CLI\Tests\TestCase;
/**
* Class Test_Arguments
* @todo add more tests to increase coverage
*
* @backupGlobals enabled
*/
class Test_Arguments extends TestCase
{
/**
* Array of expected settings
* @var array
*/
protected $settings = null;
/**
* Array of flags
* @var array
*/
protected $flags = null;
/**
* Array of expected options
* @var array
*/
protected $options = null;
/**
* Clear the $_SERVER['argv'] array
*/
public static function clearArgv()
{
$_SERVER['argv'] = array();
$_SERVER['argc'] = 0;
}
/**
* Add one or more element(s) at the end of the $_SERVER['argv'] array
*
* @param array $args: value(s) to add to the argv array
*/
public static function pushToArgv($args)
{
if (is_string($args)) {
$args = explode(' ', $args);
}
foreach ($args as $arg) {
array_push($_SERVER['argv'], $arg);
}
$_SERVER['argc'] += count($args);
}
/**
* Set up valid flags and options
*/
public function set_up()
{
self::clearArgv();
self::pushToArgv('my_script.php');
$this->flags = array(
'flag1' => array(
'aliases' => 'f',
'description' => 'Test flag 1'
),
'flag2' => array(
'description' => 'Test flag 2'
)
);
$this->options = array(
'option1' => array(
'aliases' => 'o',
'description' => 'Test option 1'
),
'option2' => array(
'aliases' => array('x', 'y'),
'description' => 'Test option 2 with default',
'default' => 'some default value'
)
);
$this->settings = array(
'strict' => true,
'flags' => $this->flags,
'options' => $this->options
);
set_error_handler(
static function ( $errno, $errstr ) {
throw new \Exception( $errstr, $errno );
},
E_ALL
);
}
/**
* Tear down fixtures
*/
public function tear_down()
{
$this->flags = null;
$this->options = null;
$this->settings = null;
self::clearArgv();
restore_error_handler();
}
/**
* Test adding a flag, getting a flag and getting all flags
*/
public function testAddFlags()
{
$args = new cli\Arguments($this->settings);
$expectedFlags = $this->flags;
$expectedFlags['flag1']['default'] = false;
$expectedFlags['flag1']['stackable'] = false;
$expectedFlags['flag2']['default'] = false;
$expectedFlags['flag2']['stackable'] = false;
$expectedFlags['flag2']['aliases'] = array();
$this->assertSame($expectedFlags, $args->getFlags());
$this->assertSame($expectedFlags['flag1'], $args->getFlag('flag1'));
$this->assertSame($expectedFlags['flag1'], $args->getFlag('f'));
$expectedFlag1Argument = new cli\arguments\Argument('-f');
$this->assertSame($expectedFlags['flag1'], $args->getFlag($expectedFlag1Argument));
}
/**
* Test adding a option, getting a option and getting all options
*/
public function testAddOptions()
{
$args = new cli\Arguments($this->settings);
$expectedOptions = $this->options;
$expectedOptions['option1']['default'] = null;
$this->assertSame($expectedOptions, $args->getOptions());
$this->assertSame($expectedOptions['option1'], $args->getOption('option1'));
$this->assertSame($expectedOptions['option1'], $args->getOption('o'));
$expectedOption1Argument = new cli\arguments\Argument('-o');
$this->assertSame($expectedOptions['option1'], $args->getOption($expectedOption1Argument));
}
/**
* Data provider with valid args and options
*
* @return array set of args and expected parsed values
*/
public static function settingsWithValidOptions()
{
return array(
array(
array('-o', 'option_value', '-f'),
array('option1' => 'option_value', 'flag1' => true)
),
array(
array('--option1', 'option_value', '--flag1'),
array('option1' => 'option_value', 'flag1' => true)
),
array(
array('-f', '--option1', 'option_value'),
array('flag1' => true, 'option1' => 'option_value')
)
);
}
/**
* Data provider with missing options
*
* @return array set of args and expected parsed values
*/
public static function settingsWithMissingOptions()
{
return array(
array(
array('-f', '--option1'),
array('flag1' => true, 'option1' => 'Error should be triggered')
),
array(
array('--option1', '-f'),
array('option1' => 'Error should be triggered', 'flag1' => true)
)
);
}
/**
* Data provider with missing options. The default value should be populated
*
* @return array set of args and expected parsed values
*/
public static function settingsWithMissingOptionsWithDefault()
{
return array(
array(
array('-f', '--option2'),
array('flag1' => true, 'option2' => 'some default value')
),
array(
array('--option2', '-f'),
array('option2' => 'some default value', 'flag1' => true)
)
);
}
public static function settingsWithNoOptionsWithDefault()
{
return array(
array(
array(),
array('flag1' => false, 'flag2' => false, 'option2' => 'some default value')
)
);
}
/**
* Generic private testParse method.
*
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
*/
private function _testParse($cliParams, $expectedValues)
{
self::pushToArgv($cliParams);
$args = new cli\Arguments($this->settings);
$args->parse();
foreach ($expectedValues as $name => $value) {
if ($args->isFlag($name)) {
$this->assertEquals($value, $args[$name]);
}
if ($args->isOption($name)) {
$this->assertEquals($value, $args[$name]);
}
}
}
/**
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
*
* @dataProvider settingsWithValidOptions
*/
#[DataProvider( 'settingsWithValidOptions' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound
public function testParseWithValidOptions($cliParams, $expectedValues)
{
$this->_testParse($cliParams, $expectedValues);
}
/**
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
* @dataProvider settingsWithMissingOptions
*/
#[DataProvider( 'settingsWithMissingOptions' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound
public function testParseWithMissingOptions($cliParams, $expectedValues)
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('no value given for --option1');
$this->_testParse($cliParams, $expectedValues);
}
/**
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
* @dataProvider settingsWithMissingOptionsWithDefault
*/
#[DataProvider( 'settingsWithMissingOptionsWithDefault' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound
public function testParseWithMissingOptionsWithDefault($cliParams, $expectedValues)
{
$this->_testParse($cliParams, $expectedValues);
}
/**
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
* @dataProvider settingsWithNoOptionsWithDefault
*/
#[DataProvider( 'settingsWithNoOptionsWithDefault' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound
public function testParseWithNoOptionsWithDefault($cliParams, $expectedValues) {
$this->_testParse($cliParams, $expectedValues);
}
public function testHelpScreenRender() {
$args = new \cli\Arguments( $this->settings );
$help = $args->getHelpScreen();
$output = $help->render();
// It should contain Flags and Options sections
$this->assertStringContainsString( 'Flags', $output );
$this->assertStringContainsString( 'Options', $output );
// Now test with ONLY flags
$settings = array(
'flags' => $this->flags,
);
$args = new \cli\Arguments( $settings );
$help = $args->getHelpScreen();
$output = $help->render();
$this->assertStringContainsString( 'Flags', $output );
$this->assertStringNotContainsString( 'Options', $output );
// It should NOT have leading/trailing newlines or empty sections
$this->assertSame( trim( $output ), $output, 'Output should not have leading/trailing whitespace' );
}
}