Passed
Push — test ( d838cf...cef4a5 )
by Tom
03:29
created

ArgsTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 173
rs 10
c 0
b 0
f 0
wmc 16

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetOptionArgumentOptionalOption() 0 9 1
A testMandatoryOptionArgumentWithParameters() 0 7 1
A testGetFirstRemainingOption() 0 4 1
A testUtility() 0 4 1
A testHasOption() 0 8 1
A testGetOptionArgument() 0 5 1
A provideFirstRemainingOptions() 0 11 1
A testMapOption() 0 10 1
A testMandatoryOptionArgument() 0 7 1
A testNonMandatoryOption() 0 4 1
A testMissingCommand() 0 6 1
A testCreation() 0 7 1
A testOptionConsumption() 0 7 1
A testGetStringOptionArgumentThrows() 0 10 1
A testMandatoryOption() 0 7 1
A testGetOptionOptionalArgument() 0 7 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Cli;
6
7
use Ktomk\Pipelines\TestCase;
8
9
/**
10
 * @covers \Ktomk\Pipelines\Cli\Args
11
 */
12
class ArgsTest extends TestCase
13
{
14
    public function testCreation()
15
    {
16
        $args = new Args(array('test'));
17
        self::assertInstanceOf('Ktomk\Pipelines\Cli\Args', $args);
18
19
        $args = Args::create(array('test'));
20
        self::assertInstanceOf('Ktomk\Pipelines\Cli\Args', $args);
21
    }
22
23
    public function testMissingCommand()
24
    {
25
        $this->expectException('InvalidArgumentException');
26
        $this->expectExceptionMessage('There must be at least one argument (the command name)');
27
28
        Args::create(array());
29
    }
30
31
    public function testUtility()
32
    {
33
        $args = Args::create(array('cmd'));
34
        self::assertSame('cmd', $args->getUtility());
35
    }
36
37
    public function testHasOption()
38
    {
39
        $args = Args::create(array('cmd', '--verbose', '-v', '--', '--operand'));
40
        self::assertFalse($args->hasOption('cmd'));
41
        self::assertFalse($args->hasOption('f'));
42
        self::assertTrue($args->hasOption('verbose'));
43
        self::assertTrue($args->hasOption(array('foo', 'v')));
44
        self::assertFalse($args->hasOption('operand'));
45
    }
46
47
    public function testMapOption()
48
    {
49
        $args = Args::create(array('cmd', '--verbose', '-v', '--', '--operand'));
50
        $actual = $args->mapOption(
51
            array('verbose' => null),
52
            function ($option, $parameter) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameter is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

52
            function ($option, /** @scrutinizer ignore-unused */ $parameter) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
                return $option;
54
            }
55
        );
56
        self::assertSame(array('verbose' => array(0 => true, 1 => 'verbose')), $actual);
57
    }
58
59
    public function testOptionConsumption()
60
    {
61
        $args = new Args(array('--verbose'));
62
        self::assertCount(1, $args->getRemaining());
63
64
        self::assertTrue($args->hasOption(array('v', 'verbose')));
65
        self::assertCount(0, $args->getRemaining());
66
    }
67
68
    public function provideFirstRemainingOptions()
69
    {
70
        return array(
71
            array(array('--verbose'), '--verbose'),
72
            array(array('test', '--verbose'), '--verbose'),
73
            array(array('verbose'), null),
74
            array(array('--'), null),
75
            array(array('--', '--me-is-parameter'), null),
76
            array(array('-'), null),
77
            array(array(''), null),
78
            array(array('', '--force'), '--force'),
79
        );
80
    }
81
82
    /**
83
     * @dataProvider provideFirstRemainingOptions
84
     *
85
     * @param array $arguments
86
     * @param string $expected first remaining option
87
     */
88
    public function testGetFirstRemainingOption(array $arguments, $expected)
89
    {
90
        $args = new Args($arguments);
91
        self::assertSame($expected, $args->getFirstRemainingOption());
92
    }
93
94
    /**
95
     * @throws ArgsException
96
     */
97
    public function testGetOptionArgument()
98
    {
99
        $args = new Args(array('--prefix', 'value'));
100
        $actual = $args->getOptionArgument('prefix');
101
        self::assertSame('value', $actual);
102
    }
103
104
    /**
105
     * @throws ArgsException
106
     */
107
    public function testGetOptionArgumentOptionalOption()
108
    {
109
        $args = new Args(array('--prefix', 'value'));
110
        $actual = $args->getOptionArgument('volume', 100);
111
        self::assertSame(100, $actual);
112
113
        $args = new Args(array('--prefix', 'value', '--', 'operand'));
114
        $actual = $args->getOptionArgument('volume', 100);
115
        self::assertSame(100, $actual);
116
    }
117
118
    public function testGetOptionOptionalArgument()
119
    {
120
        $args = new Args(array('--foo', '--bar=', '--baz=foo'));
121
        self::assertNull($args->getOptionOptionalArgument('faux', true));
122
        self::assertTrue($args->getOptionOptionalArgument('foo', true));
123
        self::assertSame('', $args->getOptionOptionalArgument('bar', true));
124
        self::assertSame('foo', $args->getOptionOptionalArgument('baz', true));
125
    }
126
127
    /**
128
     * @throws ArgsException
129
     */
130
    public function testMandatoryOption()
131
    {
132
        $this->expectException('Ktomk\Pipelines\Cli\ArgsException');
133
        $this->expectExceptionMessage('option --volume is not optional');
134
135
        $args = new Args(array('--prefix', 'value'));
136
        $args->getOptionArgument('volume', null, true);
137
    }
138
139
    /**
140
     * @throws ArgsException
141
     */
142
    public function testNonMandatoryOption()
143
    {
144
        $args = new Args(array('--prefix', 'value'));
145
        self::assertNull($args->getOptionArgument('volume'));
146
    }
147
148
    /**
149
     * @throws ArgsException
150
     */
151
    public function testMandatoryOptionArgument()
152
    {
153
        $this->expectException('Ktomk\Pipelines\Cli\ArgsException');
154
        $this->expectExceptionMessage('option --prefix requires an argument');
155
156
        $args = new Args(array('--prefix'));
157
        $args->getOptionArgument('prefix', 100);
158
    }
159
160
    /**
161
     * @throws ArgsException
162
     */
163
    public function testMandatoryOptionArgumentWithParameters()
164
    {
165
        $this->expectException('Ktomk\Pipelines\Cli\ArgsException');
166
        $this->expectExceptionMessage('option --prefix requires an argument');
167
168
        $args = new Args(array('--prefix', '--'));
169
        $args->getOptionArgument('prefix', 100);
170
    }
171
172
    /**
173
     * @throws ArgsException
174
     */
175
    public function testGetStringOptionArgumentThrows()
176
    {
177
        $args = new Args(array('--prefix', '--'));
178
179
        self::assertSame('default', $args->getStringOptionArgument('suffix', 'default'));
180
181
        $this->expectException('InvalidArgumentException');
182
        $this->expectExceptionMessage('default value must be string, integer given');
183
184
        $args->getStringOptionArgument('prefix', 100);
185
    }
186
}
187