-
Notifications
You must be signed in to change notification settings - Fork 5
/
CLImaxControllerTest.php
337 lines (288 loc) · 12.7 KB
/
CLImaxControllerTest.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
require_once 'CLImax.php';
require_once 'TestClasses.php';
/**
* Test class for CLImaxController.
*/
class CLImaxControllerTest extends PHPUnit_Framework_TestCase
{
/**
* Generates the argv/argc vars that php creates when using CLI.
*
* This is useful for converting a shell command string into argv/argc for testing so it can be passed into run().
*
* @param string command
* @return array Assoc array with argv/argc.
*/
public function generateArgvArgc($cmd)
{
$cmd = "php -r 'var_export(array(\"argv\" => \$argv, \"argc\" => \$argc));' {$cmd}";
exec($cmd, $output, $retVal);
if ($retVal != 0)
{
throw new Exception("Error running argv/argc generator with {$cmd}");
}
$argvArgc = join("\n", $output);
$loadArgvArgcCmd = "return {$argvArgc};";
$argvArgc = eval($loadArgvArgcCmd);
if ($argvArgc === false)
{
throw new Exception("Error generating argv/argc for {$cmd}");
}
// fix "\\-" to -
for ($i = 0; $i < count($argvArgc['argv']); $i++) {
while (preg_match('/^[-]*[\\\\]+./', $argvArgc['argv'][$i])) {
$argvArgc['argv'][$i] = preg_replace('/^(-*)\\\\-/', '$1-', $argvArgc['argv'][$i]);
}
}
return $argvArgc;
}
/**
* @testdox Ensure generateArgvArgc() test helper works.
*/
public function testEnsureArgvArgcGeneratorWorks()
{
extract($this->generateArgvArgc("foo bar"));
$this->assertEquals(array('-', 'foo', 'bar'), $argv);
$this->assertEquals(3, $argc);
extract($this->generateArgvArgc("a=b c"));
$this->assertEquals(array('-', 'a=b', 'c'), $argv);
$this->assertEquals(3, $argc);
}
/**
* @testdox Has fluent constructor named CLImaxController::create()
*/
public function testHasFluentStaticConstructorNamedCreate()
{
$o = CLImaxController::create();
$this->assertTrue( $o instanceof CLImaxController );
}
/**
* @testdox Reads default environemnt from $_ENV
*/
public function testDefaultEnvironment()
{
$_ENV = array('foo' => 'bar');
$o = CLImaxController::create();
$this->assertTrue($o->hasEnvironment('foo'));
$this->assertEquals('bar', $o->getEnvironment('foo'));
}
public function testMergeEnvironmentDoesNotOverwriteKeysByDefault()
{
$_ENV = array('foo' => 'bar');
$o = CLImaxController::create();
// test merge w/no overwrite
$o->mergeEnvironment(array('foo' => 'baz does not overwrite bar without force'));
$this->assertEquals('bar', $o->getEnvironment('foo'));
}
/**
* @testdox Merge Environment Overwrites Keys if array('overwrite' => true) passed
*/
public function testMergeEnvironmentOverwritesKeysIfOverwriteEnabled()
{
$_ENV = array('foo' => 'bar');
$o = CLImaxController::create();
// test merge w/overwrite
$o->mergeEnvironment(array('foo' => 'baz overwrites bar'), array('overwrite' => true));
$this->assertEquals('baz overwrites bar', $o->getEnvironment('foo'));
}
public function testSetEnvironmentReplacesEntireEnvironment()
{
$env = array('foo' => 'bar', 'boo' => 'baz');
$o = CLImaxController::create();
// test merge w/no overwrite
$o->setEnvironment($env);
$this->assertEquals($env, $o->getEnvironment());
}
/**
* @testdox "cliapp cmd" runs "cmd" with no arguments
*/
public function testCommandRunsIfPresentInArgs()
{
extract($this->generateArgvArgc("hw"));
$mock = $this->getMock('CLIHelloWorld', array('run'));
$mock->expects($this->once())
->method('run')
->will($this->returnValue(0));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->addCommand($mock, array('hw'))
->run($argv, $argc);
}
/**
* @testdox "cliapp cmd 1 2 3 4 5" runs "cmd" with arguments array(1,2,3,4,5)
*/
public function testCommandGetsExpectedArguments()
{
extract($this->generateArgvArgc("repeat 1 2 3 4 5"));
$ar = new CLIArgRepeater;
// ensure proper arguments to run()
$ar = $this->getMock('CLIArgRepeater', array('testArguments'));
$ar->expects($this->once())
->method('testArguments')
->with($this->equalTo(array(1,2,3,4,5)));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->addCommand($ar, array('repeat'))
->run($argv, $argc);
$this->assertEquals(0, $o);
}
/**
* @testdox "cliapp" will print usage if no default command is specified
*/
public function testUsagePrintedIfNoCommandSpecifiedAndNoDefaultCommandSpecified()
{
extract($this->generateArgvArgc(""));
// ensure proper arguments to run()
$cli = $this->getMock('CLImaxController', array('usage'));
$cli->expects($this->once())
->method('usage');
$result = $cli->run($argv, $argc, array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true));
}
/**
* @testdox "cliapp" runs default command with no arguments if a command is specified with setDefaultCommand()
*/
public function testDefaultCommandRunsWithNoArgumentsIfNoCommandsOrArgumentsSpecified()
{
extract($this->generateArgvArgc(""));
$def = $this->getMock('CLIArgRepeater', array('testArguments'));
$def->expects($this->once())
->method('testArguments')
->with($this->equalTo(array()));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->setDefaultCommand($def)
->run($argv, $argc);
$this->assertEquals(0, $o);
}
/**
* @testdox "cliapp 1 2 3 4 5" runs default command with array(1,2,3,4,5)
*/
public function testDefaultCommandGetsAllArgumentsIfNoOtherCommandSpecified()
{
extract($this->generateArgvArgc("1 2 3 4 5"));
// ensure proper arguments to run()
$def = $this->getMock('CLIArgRepeater', array('testArguments'));
$def->expects($this->once())
->method('testArguments')
->with($this->equalTo(array(1,2,3,4,5)));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->setDefaultCommand($def)
->run($argv, $argc);
$this->assertEquals(0, $o);
}
/**
* @testdox "cliapp cmd 1 2 3 4 5" does not run default command by default
*/
public function testDefaultCommandDoesNotRunIfAlwaysRunOptionDisabled()
{
extract($this->generateArgvArgc("ar 1 2 3 4 5"));
$ar = new CLIArgRepeater;
$def = $this->getMock('CLIArgRepeater', array('run'));
$def->expects($this->never())
->method('run');
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->setDefaultCommand($def)
->addCommand($ar, 'ar')
->run($argv, $argc);
$this->assertEquals(0, $o);
}
/**
* @testdox "cliapp cmd 1 2 3 4 5" will run default command with no arguments if setDefaultCommand() is called with 'alwaysRuns' => true
*/
public function testDefaultCommandRunsIfAlwaysRunsOptionEnabled()
{
extract($this->generateArgvArgc("ar 1 2 3 4 5"));
$ar = new CLIArgRepeater;
// ensure run() is called
$def = $this->getMock('CLIArgRepeater', array('run'));
$def->expects($this->once())
->method('run')
->will($this->returnValue(0));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->setDefaultCommand($def, array('alwaysRuns' => true))
->addCommand($ar, 'ar')
->run($argv, $argc);
$this->assertEquals(0, $o);
// ensure proper arguments to run()
$def = $this->getMock('CLIArgRepeater', array('testArguments'));
$def->expects($this->once())
->method('testArguments')
->with($this->equalTo(array()));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->setDefaultCommand($def, array('alwaysRuns' => true))
->addCommand($ar, 'ar')
->run($argv, $argc);
$this->assertEquals(0, $o);
}
/**
* @testdox "cliapp 1 2 cmd 1 2 3 4 5" will run default command with arguments array(1,2) if setDefaultCommand() is called with 'alwaysRuns' => true
*/
public function testDefaultCommandGetsFirstSetOfArgumentsIfAlwaysRunOptionEnabledAndArgumentsSpecifiedBeforeDefaultCommand()
{
extract($this->generateArgvArgc("1 2 ar 1 2 3 4 5"));
$ar = new CLIArgRepeater;
// ensure proper arguments to run()
$def = $this->getMock('CLIArgRepeater', array('testArguments'));
$def->expects($this->once())
->method('testArguments')
->with($this->equalTo(array(1,2)));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->setDefaultCommand($def, array('alwaysRuns' => true))
->addCommand($ar, 'ar')
->run($argv, $argc);
$this->assertEquals(0, $o);
}
public function testReturnsUsageErrorIfUsageDisplayedDueToLackOfCommands()
{
extract($this->generateArgvArgc(""));
// ensure result code
ob_start();
$result = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->run($argv, $argc);
$this->assertEquals(CLImaxController::ERR_USAGE, $result);
ob_end_clean();
}
public function testAddEnvironmentFlagWithExactlyOneArgument()
{
extract($this->generateArgvArgc("\\\\-\\\\-flag 1"));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->addEnvironmentFlagWithExactlyOneArgument('foo', '--flag');
$res = $o->run($argv, $argc);
$this->assertEquals(0, $res);
$this->assertEquals(1, $o->getEnvironment('foo'));
}
public function testAddEnvironmentFlagSetsValue()
{
extract($this->generateArgvArgc("\\\\-\\\\-flag"));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->addEnvironmentFlagSetsValue('foo', 1, '--flag');
$res = $o->run($argv, $argc);
$this->assertEquals(0, $res);
$this->assertEquals(1, $o->getEnvironment('foo'));
}
public function testAddEnvironmentFlagAcceptsValidValue()
{
extract($this->generateArgvArgc("flag validVal"));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true))
->addEnvironmentFlagSetsValue('foo', 1, 'flag', array('allowedValues' => array('validVal')));
$res = $o->run($argv, $argc);
$this->assertEquals(0, $res);
$this->assertEquals('validVal', $o->getEnvironment('foo'));
}
public function testAddEnvironmentFlagRestrictInvalidValues()
{
extract($this->generateArgvArgc("flag illegalVal"));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true, CLImaxController::OPT_SLIENT => true))
->addEnvironmentFlagSetsValue('foo', 1, 'flag', array('allowedValues' => array('validVal')));
$res = $o->run($argv, $argc);
$this->assertEquals(-2, $res);
}
public function testTokenWithValueOf0DoesNotStopProcessing()
{
extract($this->generateArgvArgc("0 flag 25"));
$o = CLImaxController::create(array(CLImaxController::OPT_RETURN_INSTEAD_OF_EXIT => true, CLImaxController::OPT_SLIENT => true))
->addEnvironmentFlagWithExactlyOneArgument('foo', 'flag')
;
$res = $o->run($argv, $argc);
$this->assertEquals(0, $res);
$this->assertEquals(25, $o->getEnvironment('foo'));
}
}