RunnerTest   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 289
Duplicated Lines 46.71 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 6
dl 135
loc 289
rs 10
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 6 1
A testThrowsExceptionWhenNoContainerAvailable() 0 9 1
A testRunnerNoSuchCommand() 0 6 1
A testRunnerList() 0 6 1
A testRunnerTryArgs() 0 16 1
A testSymfonyStyle() 0 6 1
A testStyleInjector() 0 6 1
A testSymfony() 0 21 1
A testCommandEventHook() 0 12 1
A testCustomEventHook() 0 8 1
A testRoboStaticRunMethod() 0 7 1
A testDeploy() 7 7 1
A testRunnerTryError() 8 8 1
A testRunnerTrySimulatedError() 8 8 1
A testRunnerTryException() 8 8 1
A testInitCommand() 0 16 1
A testTasksStopOnFail() 8 8 1
A testInvalidRoboDirectory() 9 9 1
A testUnloadableRoboFile() 11 11 1
A testRunnerQuietOutput() 14 14 1
A testRunnerVerboseOutput() 14 14 1
A testRunnerVeryVerboseOutput() 14 14 1
A testRunnerVerbosityThresholdVerbose() 10 10 1
A testRunnerVerbosityThresholdVeryVerbose() 10 10 1
A testRunnerDebugOutput() 14 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Robo;
3
4
use PHPUnit\Framework\TestCase;
5
use Robo\Traits\TestTasksTrait;
6
7
class RunnerTest extends TestCase
8
{
9
    use TestTasksTrait;
10
    use Task\Base\loadTasks;
11
12
    /**
13
     * @var \Robo\Runner
14
     */
15
    private $runner;
16
17
    public function setup()
18
    {
19
        $container = $this->initTestTasksTrait();
20
        $this->runner = new \Robo\Runner('\Robo\RoboFileFixture');
21
        $this->runner->setContainer($container);
22
    }
23
24
    public function testThrowsExceptionWhenNoContainerAvailable()
25
    {
26
        $this->expectException('\RuntimeException');
27
        $this->expectExceptionMessageRegExp(
28
            '/container is not initialized yet.*/'
29
        );
30
        Robo::unsetContainer();
31
        Robo::getContainer();
32
    }
33
34
    public function testRunnerNoSuchCommand()
35
    {
36
        $argv = ['placeholder', 'no-such-command'];
37
        $this->runner->execute($argv, null, null, $this->capturedOutputStream());
38
        $this->assertOutputContains('Command "no-such-command" is not defined.');
39
    }
40
41
    public function testRunnerList()
42
    {
43
        $argv = ['placeholder', 'list'];
44
        $this->runner->execute($argv, null, null, $this->capturedOutputStream());
45
        $this->assertOutputContains('test:array-args');
46
    }
47
48
    public function testRunnerTryArgs()
49
    {
50
        $argv = ['placeholder', 'test:array-args', 'a', 'b', 'c'];
51
        $this->runner->execute($argv, null, null, $this->capturedOutputStream());
52
53
        $expected = <<<EOT
54
>  The parameters passed are:
55
array (
56
  0 => 'a',
57
  1 => 'b',
58
  2 => 'c',
59
)
60
61
EOT;
62
        $this->assertOutputEquals($expected);
63
    }
64
65
    public function testSymfonyStyle()
66
    {
67
        $argv = ['placeholder', 'test:symfony-style'];
68
        $this->runner->execute($argv, null, null, $this->capturedOutputStream());
69
        $this->assertOutputContains('Some text in section one.');
70
    }
71
72
    public function testStyleInjector()
73
    {
74
        $argv = ['placeholder', 'test:style-injector'];
75
        $this->runner->execute($argv, null, null, $this->capturedOutputStream());
76
        $this->assertOutputContains('Some text in section one printed via injected io object.');
77
    }
78
79
    public function testSymfony()
80
    {
81
        $argv = ['placeholder', 'test:symfony', 'a', 'b', 'c', '--foo=bar', '--foo=baz', '--foo=boz'];
82
        $this->runner->execute($argv, null, null, $this->capturedOutputStream());
83
        $expected = <<<EOT
84
>  The parameters passed are:
85
array (
86
  0 => 'a',
87
  1 => 'b',
88
  2 => 'c',
89
)
90
>  The options passed via --foo are:
91
array (
92
  0 => 'bar',
93
  1 => 'baz',
94
  2 => 'boz',
95
)
96
97
EOT;
98
        $this->assertOutputEquals($expected);
99
    }
100
101
    public function testCommandEventHook()
102
    {
103
        $argv = ['placeholder', 'test:command-event'];
104
        $this->runner->execute($argv, null, null, $this->capturedOutputStream());
105
106
        $expected = <<<EOT
107
 This is the command-event hook for the test:command-event command.
108
 This is the main method for the test:command-event command.
109
 This is the post-command hook for the test:command-event command.
110
EOT;
111
        $this->assertOutputContains($expected);
112
    }
113
114
    public function testCustomEventHook()
115
    {
116
        $argv = ['placeholder', 'test:custom-event'];
117
        $this->runner->execute($argv, null, null, $this->capturedOutputStream());
118
119
        $expected = 'one,two';
120
        $this->assertOutputContains($expected);
121
    }
122
123
    public function testRoboStaticRunMethod()
124
    {
125
        $argv = ['placeholder', 'test:symfony-style'];
126
        $commandFiles = ['\Robo\RoboFileFixture'];
127
        Robo::run($argv, $commandFiles, 'MyApp', '1.2.3', $this->capturedOutputStream());
0 ignored issues
show
Documentation introduced by
$commandFiles is of type array<integer,string,{"0":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
128
        $this->assertOutputContains('Some text in section one.');
129
    }
130
131 View Code Duplication
    public function testDeploy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        $argv = ['placeholder', 'test:deploy', '--simulate'];
134
        $this->runner->execute($argv, null, null, $this->capturedOutputStream());
135
        $this->assertOutputContains('[Simulator] Simulating Remote\\Ssh(\'mysite.com\', null)');
136
        $this->assertOutputContains('[Simulator] Running ssh mysite.com \'cd "/var/www/somesite" && git pull\'');
137
    }
138
139 View Code Duplication
    public function testRunnerTryError()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        $argv = ['placeholder', 'test:error'];
142
        $result = $this->runner->execute($argv, null, null, $this->capturedOutputStream());
143
144
        $this->assertOutputContains('[Exec] Running ls xyzzy');
145
        $this->assertTrue($result > 0);
146
    }
147
148 View Code Duplication
    public function testRunnerTrySimulatedError()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        $argv = ['placeholder', 'test:error', '--simulate'];
151
        $result = $this->runner->execute($argv, null, null, $this->capturedOutputStream());
152
153
        $this->assertOutputContains('Simulating Exec');
154
        $this->assertEquals(0, $result);
155
    }
156
157 View Code Duplication
    public function testRunnerTryException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $argv = ['placeholder', 'test:exception', '--task'];
160
        $result = $this->runner->execute($argv, null, null, $this->capturedOutputStream());
161
162
        $this->assertOutputContains('Task failed with an exception');
163
        $this->assertEquals(1, $result);
164
    }
165
166
    public function testInitCommand()
167
    {
168
        $container = \Robo\Robo::getContainer();
169
        $app = $container->get('application');
170
        $app->addInitRoboFileCommand(getcwd() . '/testRoboFile.php', 'RoboTestClass');
171
172
        $argv = ['placeholder', 'init'];
173
        $status = $this->runner->run($argv, $this->capturedOutputStream(), $app);
0 ignored issues
show
Documentation introduced by
$argv is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a null|object<Symfony\Comp...e\Input\InputInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
174
        $this->assertOutputContains('testRoboFile.php will be created in the current directory');
175
        $this->assertEquals(0, $status);
176
177
        $this->assertTrue(file_exists('testRoboFile.php'));
178
        $commandContents = file_get_contents('testRoboFile.php');
179
        unlink('testRoboFile.php');
180
        $this->assertContains('class RoboTestClass', $commandContents);
181
    }
182
183 View Code Duplication
    public function testTasksStopOnFail()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
    {
185
        $argv = ['placeholder', 'test:stop-on-fail'];
186
        $result = $this->runner->execute($argv, null, null, $this->capturedOutputStream());
187
188
        $this->assertOutputContains('[');
189
        $this->assertTrue($result > 0);
190
    }
191
192 View Code Duplication
    public function testInvalidRoboDirectory()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
    {
194
        $runnerWithNoRoboFile = new \Robo\Runner();
195
196
        $argv = ['placeholder', 'no-such-command', '-f', 'no-such-directory'];
197
        $result = $runnerWithNoRoboFile->execute($argv, null, null, $this->capturedOutputStream());
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
198
199
        $this->assertOutputContains('Path `no-such-directory` is invalid; please provide a valid absolute path to the Robofile to load.');
200
    }
201
202 View Code Duplication
    public function testUnloadableRoboFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
    {
204
        $runnerWithNoRoboFile = new \Robo\Runner();
205
206
        $argv = ['placeholder', 'help', 'test:custom-event', '-f', dirname(__DIR__) . '/src/RoboFileFixture.php'];
207
        $result = $runnerWithNoRoboFile->execute($argv, null, null, $this->capturedOutputStream());
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
208
209
        // We cannot load RoboFileFixture.php via -f / --load-from because
210
        // it has a namespace, and --load-from does not support that.
211
        $this->assertOutputContains('Class RoboFileFixture was not loaded');
212
    }
213
214 View Code Duplication
    public function testRunnerQuietOutput()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
    {
216
        $argv = ['placeholder', 'test:verbosity', '--quiet'];
217
        $result = $this->runner->execute($argv, null, null, $this->capturedOutputStream());
218
219
        $this->assertOutputNotContains('This command will print more information at higher verbosity levels');
220
        $this->assertOutputNotContains('This is a verbose message (-v).');
221
        $this->assertOutputNotContains('This is a very verbose message (-vv).');
222
        $this->assertOutputNotContains('This is a debug message (-vvv).');
223
        $this->assertOutputNotContains(' [warning] This is a warning log message.');
224
        $this->assertOutputNotContains(' [notice] This is a notice log message.');
225
        $this->assertOutputNotContains(' [debug] This is a debug log message.');
226
        $this->assertEquals(0, $result);
227
    }
228
229 View Code Duplication
    public function testRunnerVerboseOutput()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
    {
231
        $argv = ['placeholder', 'test:verbosity', '-v'];
232
        $result = $this->runner->execute($argv, null, null, $this->capturedOutputStream());
233
234
        $this->assertOutputContains('This command will print more information at higher verbosity levels');
235
        $this->assertOutputContains('This is a verbose message (-v).');
236
        $this->assertOutputNotContains('This is a very verbose message (-vv).');
237
        $this->assertOutputNotContains('This is a debug message (-vvv).');
238
        $this->assertOutputContains(' [warning] This is a warning log message.');
239
        $this->assertOutputContains(' [notice] This is a notice log message.');
240
        $this->assertOutputNotContains(' [debug] This is a debug log message.');
241
        $this->assertEquals(0, $result);
242
    }
243
244 View Code Duplication
    public function testRunnerVeryVerboseOutput()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
245
    {
246
        $argv = ['placeholder', 'test:verbosity', '-vv'];
247
        $result = $this->runner->execute($argv, null, null, $this->capturedOutputStream());
248
249
        $this->assertOutputContains('This command will print more information at higher verbosity levels');
250
        $this->assertOutputContains('This is a verbose message (-v).');
251
        $this->assertOutputContains('This is a very verbose message (-vv).');
252
        $this->assertOutputNotContains('This is a debug message (-vvv).');
253
        $this->assertOutputContains(' [warning] This is a warning log message.');
254
        $this->assertOutputContains(' [notice] This is a notice log message.');
255
        $this->assertOutputNotContains(' [debug] This is a debug log message.');
256
        $this->assertEquals(0, $result);
257
    }
258
259 View Code Duplication
    public function testRunnerVerbosityThresholdVerbose()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
260
    {
261
        $argv = ['placeholder', 'test:verbosity-threshold', '-v'];
262
        $result = $this->runner->execute($argv, null, null, $this->capturedOutputStream());
263
264
        $this->assertOutputContains('This command will print more information at higher verbosity levels');
265
        $this->assertOutputContains("Running echo verbose or higher\nverbose or higher");
266
        $this->assertOutputNotContains('very verbose or higher');
267
        $this->assertEquals(0, $result);
268
    }
269
270 View Code Duplication
    public function testRunnerVerbosityThresholdVeryVerbose()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271
    {
272
        $argv = ['placeholder', 'test:verbosity-threshold', '-vv'];
273
        $result = $this->runner->execute($argv, null, null, $this->capturedOutputStream());
274
275
        $this->assertOutputContains('This command will print more information at higher verbosity levels');
276
        $this->assertOutputContains("Running echo verbose or higher\nverbose or higher");
277
        $this->assertOutputContains("Running echo very verbose or higher\nvery verbose or higher");
278
        $this->assertEquals(0, $result);
279
    }
280
281 View Code Duplication
    public function testRunnerDebugOutput()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
282
    {
283
        $argv = ['placeholder', 'test:verbosity', '-vvv'];
284
        $result = $this->runner->execute($argv, null, null, $this->capturedOutputStream());
285
286
        $this->assertOutputContains('This command will print more information at higher verbosity levels');
287
        $this->assertOutputContains('This is a verbose message (-v).');
288
        $this->assertOutputContains('This is a very verbose message (-vv).');
289
        $this->assertOutputContains('This is a debug message (-vvv).');
290
        $this->assertOutputContains(' [warning] This is a warning log message.');
291
        $this->assertOutputContains(' [notice] This is a notice log message.');
292
        $this->assertOutputContains(' [debug] This is a debug log message.');
293
        $this->assertEquals(0, $result);
294
    }
295
}
296