Passed
Branch master (b27be8)
by Zach
01:12
created

SignatureParserTest::signature_parser_parses_option_description()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Tests;
4
5
use Artisanize\SignatureParser;
6
use Tests\data\NullCommand;
7
8
class SignatureParserTest extends BaseTestCase
9
{
10
    /**
11
     * Test command instance.
12
     *
13
     * @var NullCommand
14
     */
15
    protected $command;
16
17
    /**
18
     * Signature parser.
19
     *
20
     * @var SignatureParser
21
     */
22
    protected $parser;
23
24
    /**
25
     * Setup the test class.
26
     *
27
     * @return void
28
     */
29
    public function setUp()
30
    {
31
        parent::setUp();
32
33
        $this->command = new NullCommand();
34
35
        $this->parser = new SignatureParser($this->command);
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function signature_parser_parses_signature_name()
42
    {
43
        $signature = 'example:command {arg} {--opt}';
44
45
        $this->parser->parse($signature);
46
47
        $this->assertEquals('example:command', $this->command->getName());
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function signature_parser_parses_standard_argument()
54
    {
55
        $signature = 'example:command {arg} {--opt}';
56
57
        $this->parser->parse($signature);
58
59
        $args = $this->command->getArguments();
60
61
        $this->assertCount(1, $args);
62
63
        $this->assertEquals('arg', $args[0]['name']);
64
65
        $this->assertEquals(1, $args[0]['mode']);
66
67
        $this->assertNull($args[0]['description']);
68
69
        $this->assertNull($args[0]['default']);
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function signature_parser_parses_argument_description()
76
    {
77
        $signature = 'example:command {arg : description} {--opt}';
78
79
        $this->parser->parse($signature);
80
81
        $args = $this->command->getArguments();
82
83
        $this->assertCount(1, $args);
84
85
        $this->assertEquals('arg', $args[0]['name']);
86
87
        $this->assertEquals(1, $args[0]['mode']);
88
89
        $this->assertEquals('description', $args[0]['description']);
90
91
        $this->assertNull($args[0]['default']);
92
    }
93
94
    /**
95
     * @test
96
     */
97
    public function signature_parser_parses_optional_argument()
98
    {
99
        $signature = 'example:command {arg?} {--opt}';
100
101
        $this->parser->parse($signature);
102
103
        $args = $this->command->getArguments();
104
105
        $this->assertCount(1, $args);
106
107
        $this->assertEquals('arg', $args[0]['name']);
108
109
        $this->assertEquals(2, $args[0]['mode']);
110
111
        $this->assertNull($args[0]['description']);
112
113
        $this->assertNull($args[0]['default']);
114
    }
115
116
    /**
117
     * @test
118
     */
119
    public function signature_parser_parses_optional_argument_with_description()
120
    {
121
        $signature = 'example:command {arg? : description} {--opt}';
122
123
        $this->parser->parse($signature);
124
125
        $args = $this->command->getArguments();
126
127
        $this->assertCount(1, $args);
128
129
        $this->assertEquals('arg', $args[0]['name']);
130
131
        $this->assertEquals(2, $args[0]['mode']);
132
133
        $this->assertEquals('description', $args[0]['description']);
134
135
        $this->assertNull($args[0]['default']);
136
    }
137
138
    /**
139
     * @test
140
     */
141
    public function signature_parser_parses_default_argument_value()
142
    {
143
        $signature = 'example:command {arg=default} {--opt}';
144
145
        $this->parser->parse($signature);
146
147
        $args = $this->command->getArguments();
148
149
        $this->assertCount(1, $args);
150
151
        $this->assertEquals('arg', $args[0]['name']);
152
153
        $this->assertEquals(2, $args[0]['mode']);
154
155
        $this->assertNull($args[0]['description']);
156
157
        $this->assertEquals('default', $args[0]['default']);
158
    }
159
160
    /**
161
     * @test
162
     */
163
    public function signature_parser_parses_default_argument_value_with_description()
164
    {
165
        $signature = 'example:command {arg=default : description} {--opt}';
166
167
        $this->parser->parse($signature);
168
169
        $args = $this->command->getArguments();
170
171
        $this->assertCount(1, $args);
172
173
        $this->assertEquals('arg', $args[0]['name']);
174
175
        $this->assertEquals(2, $args[0]['mode']);
176
177
        $this->assertEquals('description', $args[0]['description']);
178
179
        $this->assertEquals('default', $args[0]['default']);
180
    }
181
182
    /**
183
     * @test
184
     */
185
    public function signature_parser_parses_argument_array_with_required_argument()
186
    {
187
        $signature = 'example:command {arg*} {--opt}';
188
189
        $this->parser->parse($signature);
190
191
        $args = $this->command->getArguments();
192
193
        $this->assertCount(1, $args);
194
195
        $this->assertEquals('arg', $args[0]['name']);
196
197
        $this->assertEquals(5, $args[0]['mode']);
198
199
        $this->assertNull($args[0]['description']);
200
201
        $this->assertNull($args[0]['default']);
202
    }
203
204
    /**
205
     * @test
206
     */
207
    public function signature_parser_parses_argument_array_with_optional_argument()
208
    {
209
        $signature = 'example:command {arg?*} {--opt}';
210
211
        $this->parser->parse($signature);
212
213
        $args = $this->command->getArguments();
214
215
        $this->assertCount(1, $args);
216
217
        $this->assertEquals('arg', $args[0]['name']);
218
219
        $this->assertEquals(6, $args[0]['mode']);
220
221
        $this->assertNull($args[0]['description']);
222
223
        $this->assertNull($args[0]['default']);
224
    }
225
226
    /**
227
     * @test
228
     */
229
    public function signature_parser_parses_standard_option()
230
    {
231
        $signature = 'example:command {arg} {--opt}';
232
233
        $this->parser->parse($signature);
234
235
        $options = $this->command->getOptions();
236
237
        $this->assertCount(1, $options);
238
239
        $this->assertEquals('opt', $options[0]['name']);
240
241
        $this->assertNull($options[0]['shortcut']);
242
243
        $this->assertEquals(1, $options[0]['mode']);
244
245
        $this->assertNull($options[0]['description']);
246
247
        $this->assertNull($options[0]['default']);
248
    }
249
250
    /**
251
     * @test
252
     */
253
    public function signature_parser_parses_option_description()
254
    {
255
        $signature = 'example:command {arg} {--opt : description}';
256
257
        $this->parser->parse($signature);
258
259
        $options = $this->command->getOptions();
260
261
        $this->assertCount(1, $options);
262
263
        $this->assertEquals('opt', $options[0]['name']);
264
265
        $this->assertNull($options[0]['shortcut']);
266
267
        $this->assertEquals(1, $options[0]['mode']);
268
269
        $this->assertEquals('description', $options[0]['description']);
270
271
        $this->assertNull($options[0]['default']);
272
    }
273
274
    /**
275
     * @test
276
     */
277
    public function signature_parser_parses_option_with_required_value()
278
    {
279
        $signature = 'example:command {arg} {--opt=}';
280
281
        $this->parser->parse($signature);
282
283
        $options = $this->command->getOptions();
284
285
        $this->assertCount(1, $options);
286
287
        $this->assertEquals('opt', $options[0]['name']);
288
289
        $this->assertNull($options[0]['shortcut']);
290
291
        $this->assertEquals(2, $options[0]['mode']);
292
293
        $this->assertNull($options[0]['description']);
294
295
        $this->assertNull($options[0]['default']);
296
    }
297
298
    /**
299
     * @test
300
     */
301
    public function signature_parser_parses_option_with_default_value()
302
    {
303
        $signature = 'example:command {arg} {--opt=default}';
304
305
        $this->parser->parse($signature);
306
307
        $options = $this->command->getOptions();
308
309
        $this->assertCount(1, $options);
310
311
        $this->assertEquals('opt', $options[0]['name']);
312
313
        $this->assertNull($options[0]['shortcut']);
314
315
        $this->assertEquals(4, $options[0]['mode']);
316
317
        $this->assertNull($options[0]['description']);
318
319
        $this->assertEquals('default', $options[0]['default']);
320
    }
321
322
    /**
323
     * @test
324
     */
325
    public function signature_parser_parses_option_with_default_value_and_description()
326
    {
327
        $signature = 'example:command {arg} {--opt=default : description}';
328
329
        $this->parser->parse($signature);
330
331
        $options = $this->command->getOptions();
332
333
        $this->assertCount(1, $options);
334
335
        $this->assertEquals('opt', $options[0]['name']);
336
337
        $this->assertNull($options[0]['shortcut']);
338
339
        $this->assertEquals(4, $options[0]['mode']);
340
341
        $this->assertEquals('description', $options[0]['description']);
342
343
        $this->assertEquals('default', $options[0]['default']);
344
    }
345
346
    /**
347
     * @test
348
     */
349
    public function signature_parser_parses_option_with_shortcut()
350
    {
351
        $signature = 'example:command {arg} {--o|opt}';
352
353
        $this->parser->parse($signature);
354
355
        $options = $this->command->getOptions();
356
357
        $this->assertCount(1, $options);
358
359
        $this->assertEquals('opt', $options[0]['name']);
360
361
        $this->assertEquals('o', $options[0]['shortcut']);
362
363
        $this->assertEquals(1, $options[0]['mode']);
364
365
        $this->assertNull($options[0]['description']);
366
367
        $this->assertNull($options[0]['default']);
368
    }
369
370
    /**
371
     * @test
372
     */
373
    public function signature_parser_parses_option_array_with_required_value()
374
    {
375
        $signature = 'example:command {arg} {--opt=*}';
376
377
        $this->parser->parse($signature);
378
379
        $options = $this->command->getOptions();
380
381
        $this->assertCount(1, $options);
382
383
        $this->assertEquals('opt', $options[0]['name']);
384
385
        $this->assertNull($options[0]['shortcut']);
386
387
        $this->assertEquals(10, $options[0]['mode']);
388
389
        $this->assertNull($options[0]['description']);
390
391
        $this->assertNull($options[0]['default']);
392
    }
393
}
394