Completed
Push — develop ( 61d4f1...4b8b90 )
by
unknown
9s
created

BaseCommandTest::testGetBinaryVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * User: matteo
4
 * Date: 20/05/13
5
 * Time: 21.47
6
 * Just for fun...
7
 */
8
9
namespace GitElephant\Command;
10
11
use \GitElephant\TestCase;
12
use \ReflectionClass;
13
14
/**
15
 * Class BaseCommandTest
16
 *
17
 * @package GitElephant\Command
18
 */
19
class BaseCommandTest extends TestCase
20
{
21
    /**
22
     * test class constructor
23
     *
24
     * @covers GitElephant\Command\BaseCommand::__construct
25
     */
26
    public function testConstructor()
27
    {
28
        $bc1 = new BaseCommand();
29
        $this->assertInstanceOf("\\GitElephant\\Command\\BaseCommand", $bc1);
30
31
        $repo = $this->getRepository();
32
33
        $configs = ['one' => 1, 'two' => 2, 'three' => 3];
34
        $options = ['A' => 'a', 'B' => 'b', 'C' => 'c'];
35
        $arguments = ['first', 'second', 'third'];
36
37
        foreach ($configs as $configName => $configValue) {
38
            $repo->addGlobalConfig($configName, $configValue);
39
        }
40
        foreach ($options as $optionName => $optionValue) {
41
            $repo->addGlobalOption($optionName, $optionValue);
42
        }
43
        foreach ($arguments as $argumentValue) {
44
            $repo->addGlobalCommandArgument($argumentValue);
45
        }
46
47
        $bc2 = new BaseCommand($repo);
48
        $ref_bc = new \ReflectionClass($bc2);
49
50
        $ref_bc_cfg_prop = $ref_bc->getProperty('globalConfigs');
51
        $ref_bc_cfg_prop->setAccessible(true);
52
        $this->assertSame($configs, $ref_bc_cfg_prop->getValue($bc2));
53
54
        $ref_bc_opt_prop = $ref_bc->getProperty('globalOptions');
55
        $ref_bc_opt_prop->setAccessible(true);
56
        $this->assertSame($options, $ref_bc_opt_prop->getValue($bc2));
57
58
        $ref_bc_arg_prop = $ref_bc->getProperty('globalCommandArguments');
59
        $ref_bc_arg_prop->setAccessible(true);
60
        $this->assertSame($arguments, $ref_bc_arg_prop->getValue($bc2));
61
    }
62
63
    /**
64
     * test static factory
65
     *
66
     * @covers GitElephant\Command\BaseCommand::getInstance
67
     */
68
    public function testGetInstance()
69
    {
70
        $bc = BaseCommand::getInstance();
71
        $this->assertInstanceOf("\\GitElephant\\Command\\BaseCommand", $bc);
72
    }
73
74
    public function testAddGlobalConfigs()
75
    {
76
        $configs = ['one' => 1, 'two' => 2, 'three' => 3];
77
        $bc = BaseCommand::getInstance();
78
        $ref_bc = new \ReflectionClass($bc);
79
80
        $ref_bc_cfg_prop = $ref_bc->getProperty('globalConfigs');
81
        $ref_bc_cfg_prop->setAccessible(true);
82
        $this->assertEmpty($ref_bc_cfg_prop->getValue($bc));
83
84
        $ref_bc_cfg_meth = $ref_bc->getMethod('addGlobalConfigs');
85
        $ref_bc_cfg_meth->setAccessible(true);
86
        $ref_bc_cfg_meth->invoke($bc, $configs);
87
        $this->assertSame($configs, $ref_bc_cfg_prop->getValue($bc));
88
    }
89
90
    public function testAddGlobalOptions()
91
    {
92
        $options = ['one' => 1, 'two' => 2, 'three' => 3];
93
        $bc = BaseCommand::getInstance();
94
        $ref_bc = new \ReflectionClass($bc);
95
96
        $ref_bc_opt_prop = $ref_bc->getProperty('globalOptions');
97
        $ref_bc_opt_prop->setAccessible(true);
98
        $this->assertEmpty($ref_bc_opt_prop->getValue($bc));
99
100
        $ref_bc_opt_meth = $ref_bc->getMethod('addGlobalOptions');
101
        $ref_bc_opt_meth->setAccessible(true);
102
        $ref_bc_opt_meth->invoke($bc, $options);
103
        $this->assertSame($options, $ref_bc_opt_prop->getValue($bc));
104
    }
105
106
    public function testAddGlobalCommandArguments()
107
    {
108
        $arguments = ['one', 'two', 'three'];
109
        $bc = BaseCommand::getInstance();
110
        $ref_bc = new \ReflectionClass($bc);
111
112
        $ref_bc_arg_prop = $ref_bc->getProperty('globalCommandArguments');
113
        $ref_bc_arg_prop->setAccessible(true);
114
        $this->assertEmpty($ref_bc_arg_prop->getValue($bc));
115
116
        $ref_bc_arg_meth = $ref_bc->getMethod('addGlobalCommandArgument');
117
        $ref_bc_arg_meth->setAccessible(true);
118
        foreach ($arguments as $argument) {
119
            $ref_bc_arg_meth->invoke($bc, $argument);
120
        }
121
        $this->assertSame($arguments, $ref_bc_arg_prop->getValue($bc));
122
    }
123
124
    public function testGetCommand()
125
    {
126
        $name = 'command';
127
        $bc = BaseCommand::getInstance();
128
        $ref_bc = new \ReflectionClass($bc);
129
130
        $ref_bc_arg_prop = $ref_bc->getProperty('commandName');
131
        $ref_bc_arg_prop->setAccessible(true);
132
        $ref_bc_arg_prop->setValue($bc, $name);
133
134
        $ref_bc_cli_meth = $ref_bc->getMethod('getCommand');
135
        $ref_bc_cli_meth->setAccessible(true);
136
137
        $expected = $name;
138
        $actual = $ref_bc_cli_meth->invoke($bc);
139
        $this->assertSame($expected, $actual);
140
    }
141
142
    /**
143
     * testGetCommandException
144
     */
145
    public function testGetCommandException()
146
    {
147
        $bc = BaseCommand::getInstance();
148
        $this->expectException('RuntimeException');
149
        $this->fail($bc->getCommand());
150
    }
151
152
    public function testGetCLICommandArguments()
153
    {
154
        $args = ['--first', '--second', '--third'];
155
        $bc = BaseCommand::getInstance();
156
        $ref_bc = new \ReflectionClass($bc);
157
158
        $ref_bc_arg_prop = $ref_bc->getProperty('globalCommandArguments');
159
        $ref_bc_arg_prop->setAccessible(true);
160
        $ref_bc_arg_prop->setValue($bc, $args);
161
162
        $ref_bc_cli_meth = $ref_bc->getMethod('getCLICommandArguments');
163
        $ref_bc_cli_meth->setAccessible(true);
164
165
        $expected = '';
166
        foreach ($args as $argument) {
167
            $expected .= " '$argument'";
168
        }
169
        $actual = $ref_bc_cli_meth->invoke($bc);
170
        $this->assertSame($expected, $actual);
171
    }
172
173
    public function testGetCLICommandName()
174
    {
175
        $name = 'command';
176
        $bc = BaseCommand::getInstance();
177
        $ref_bc = new \ReflectionClass($bc);
178
179
        $ref_bc_arg_prop = $ref_bc->getProperty('commandName');
180
        $ref_bc_arg_prop->setAccessible(true);
181
        $ref_bc_arg_prop->setValue($bc, $name);
182
183
        $ref_bc_cli_meth = $ref_bc->getMethod('getCLICommandName');
184
        $ref_bc_cli_meth->setAccessible(true);
185
186
        $expected = " $name";
187
        $actual = $ref_bc_cli_meth->invoke($bc);
188
        $this->assertSame($expected, $actual);
189
    }
190
191
    public function testGetCLIConfigs()
192
    {
193
        $globals = ['global.first' => 'a', 'global.second' => 'b'];
194
        $locals = ['local.first' => 'c', 'local.second' => 'd'];
195
        $configs = array_merge($globals, $locals);
196
197
        $bc = BaseCommand::getInstance();
198
        $ref_bc = new \ReflectionClass($bc);
199
200
        $ref_bc_glob_cfg_prop = $ref_bc->getProperty('globalConfigs');
201
        $ref_bc_glob_cfg_prop->setAccessible(true);
202
        $ref_bc_glob_cfg_prop->setValue($bc, $globals);
203
204
        $ref_bc_loc_cfg_prop = $ref_bc->getProperty('configs');
205
        $ref_bc_loc_cfg_prop->setAccessible(true);
206
        $ref_bc_loc_cfg_prop->setValue($bc, $locals);
207
208
        $ref_bc_cli_meth = $ref_bc->getMethod('getCLIConfigs');
209
        $ref_bc_cli_meth->setAccessible(true);
210
211
        $expected = '';
212
        foreach ($configs as $name => $value) {
213
            $expected .= " '-c' '$name'='$value'";
214
        }
215
        $actual = $ref_bc_cli_meth->invoke($bc);
216
        $this->assertSame($expected, $actual);
217
    }
218
219
    public function testGetCLIGlobalOptions()
220
    {
221
        $options = ['first' => 'a', 'second' => 'b', 'third' => 'c'];
222
        $bc = BaseCommand::getInstance();
223
        $ref_bc = new \ReflectionClass($bc);
224
225
        $ref_bc_opt_prop = $ref_bc->getProperty('globalOptions');
226
        $ref_bc_opt_prop->setAccessible(true);
227
        $ref_bc_opt_prop->setValue($bc, $options);
228
229
        $ref_bc_cli_meth = $ref_bc->getMethod('getCLIGlobalOptions');
230
        $ref_bc_cli_meth->setAccessible(true);
231
232
        $expected = '';
233
        foreach ($options as $name => $value) {
234
            $expected .= " '$name'='$value'";
235
        }
236
        $actual = $ref_bc_cli_meth->invoke($bc);
237
        $this->assertSame($expected, $actual);
238
    }
239
240
    public function testGetCLIPath()
241
    {
242
        $path = '/path/to/something';
243
        $bc = BaseCommand::getInstance();
244
        $ref_bc = new \ReflectionClass($bc);
245
246
        $ref_bc_path_prop = $ref_bc->getProperty('path');
247
        $ref_bc_path_prop->setAccessible(true);
248
        $ref_bc_path_prop->setValue($bc, $path);
249
250
        $ref_bc_cli_meth = $ref_bc->getMethod('getCLIPath');
251
        $ref_bc_cli_meth->setAccessible(true);
252
253
        $expected = " -- '$path'";
254
        $actual = $ref_bc_cli_meth->invoke($bc);
255
        $this->assertSame($expected, $actual);
256
    }
257
258
    public function testGetCLISubjects()
259
    {
260
        $subject1 = 'first';
261
        $subject2 = 'second';
262
        $bc = BaseCommand::getInstance();
263
        $ref_bc = new \ReflectionClass($bc);
264
265
        $ref_bc_subj1_prop = $ref_bc->getProperty('commandSubject');
266
        $ref_bc_subj1_prop->setAccessible(true);
267
        $ref_bc_subj1_prop->setValue($bc, $subject1);
268
269
        $ref_bc_subj2_prop = $ref_bc->getProperty('commandSubject2');
270
        $ref_bc_subj2_prop->setAccessible(true);
271
        $ref_bc_subj2_prop->setValue($bc, $subject2);
272
273
        $ref_bc_cli_meth = $ref_bc->getMethod('getCLISubjects');
274
        $ref_bc_cli_meth->setAccessible(true);
275
276
        $expected = " '$subject1' '$subject2'";
277
        $actual = $ref_bc_cli_meth->invoke($bc);
278
        $this->assertSame($expected, $actual);
279
    }
280
281
    public function testGetBinaryVersion()
282
    {
283
        $repo = $this->getRepository();
284
        $bc = BaseCommand::getInstance($repo);
285
        $this->assertInternalType('string', $bc->getBinaryVersion());
286
    }
287
}
288