Completed
Push — master ( 8b8afe...5f2bbe )
by Greg
02:21
created

tests/unit/RunnerTest.php (20 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
use Robo\Robo;
3
4
class RunnerTest extends \Codeception\TestCase\Test
5
{
6
    /**
7
     * @var \Robo\Runner
8
     */
9
    private $runner;
10
11
    /**
12
     * @var \CodeGuy
13
     */
14
    protected $guy;
15
16
    public function _before()
17
    {
18
        $this->runner = new \Robo\Runner('\Robo\RoboFileFixture');
19
    }
20
21
    public function testThrowsExceptionWhenNoContainerAvailable()
22
    {
23
        \PHPUnit_Framework_TestCase::setExpectedExceptionRegExp(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCa...pectedExceptionRegExp() has been deprecated with message: Method deprecated since Release 5.6.0; use expectExceptionMessageRegExp() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
24
            '\RuntimeException',
25
            '/container is not initialized yet.*/'
26
        );
27
        Robo::unsetContainer();
28
        Robo::getContainer();
29
    }
30
31
    public function testRunnerNoSuchCommand()
32
    {
33
        $argv = ['placeholder', 'no-such-command'];
34
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
35
        $this->guy->seeInOutput('Command "no-such-command" is not defined.');
36
    }
37
38
    public function testRunnerList()
39
    {
40
        $argv = ['placeholder', 'list'];
41
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
42
        $this->guy->seeInOutput('test:array-args');
43
    }
44
45
    public function testRunnerTryArgs()
46
    {
47
        $argv = ['placeholder', 'test:array-args', 'a', 'b', 'c'];
48
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
49
50
        $expected = <<<EOT
51
>  The parameters passed are:
52
array (
53
  0 => 'a',
54
  1 => 'b',
55
  2 => 'c',
56
)
57
58
EOT;
59
        $this->guy->seeOutputEquals($expected);
60
    }
61
62
    public function testSymfonyStyle()
63
    {
64
        $argv = ['placeholder', 'test:symfony-style'];
65
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
66
        $this->guy->seeInOutput('Some text in section one.');
67
    }
68
69 View Code Duplication
    public function testCommandEventHook()
0 ignored issues
show
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...
70
    {
71
        $argv = ['placeholder', 'test:command-event'];
72
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
73
74
        $expected = <<<EOT
75
 This is the command-event hook for the test:command-event command.
76
 This is the main method for the test:command-event command.
77
 This is the post-command hook for the test:command-event command.
78
EOT;
79
        $this->guy->seeInOutput($expected);
80
    }
81
82 View Code Duplication
    public function testCustomEventHook()
0 ignored issues
show
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...
83
    {
84
        $argv = ['placeholder', 'test:custom-event'];
85
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
86
87
        $expected = 'one,two';
88
        $this->guy->seeInOutput($expected);
89
    }
90
91
    public function testRoboStaticRunMethod()
92
    {
93
        $argv = ['placeholder', 'test:symfony-style'];
94
        $commandFiles = ['\Robo\RoboFileFixture'];
95
        Robo::run($argv, $commandFiles, 'MyApp', '1.2.3', $this->guy->capturedOutputStream());
0 ignored issues
show
$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...
96
        $this->guy->seeInOutput('Some text in section one.');
97
    }
98
99 View Code Duplication
    public function testDeploy()
0 ignored issues
show
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...
100
    {
101
        $argv = ['placeholder', 'test:deploy', '--simulate'];
102
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
103
        $this->guy->seeInOutput('[Simulator] Simulating Remote\\Ssh(\'mysite.com\', null)');
104
        $this->guy->seeInOutput('[Simulator] Running ssh mysite.com \'cd "/var/www/somesite" && git pull\'');
105
    }
106
107 View Code Duplication
    public function testRunnerTryError()
0 ignored issues
show
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...
108
    {
109
        $argv = ['placeholder', 'test:error'];
110
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
111
112
        $this->guy->seeInOutput('[Exec] Running ls xyzzy');
113
        $this->assertTrue($result > 0);
114
    }
115
116 View Code Duplication
    public function testRunnerTrySimulatedError()
0 ignored issues
show
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...
117
    {
118
        $argv = ['placeholder', 'test:error', '--simulate'];
119
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
120
121
        $this->guy->seeInOutput('Simulating Exec');
122
        $this->assertEquals(0, $result);
123
    }
124
125 View Code Duplication
    public function testRunnerTryException()
0 ignored issues
show
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...
126
    {
127
        $argv = ['placeholder', 'test:exception', '--task'];
128
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
129
130
        $this->guy->seeInOutput('Task failed with an exception');
131
        $this->assertEquals(1, $result);
132
    }
133
134
    public function testInitCommand()
135
    {
136
        $container = \Robo\Robo::getContainer();
137
        $app = $container->get('application');
138
        $app->addInitRoboFileCommand(getcwd() . '/testRoboFile.php', 'RoboTestClass');
139
140
        $argv = ['placeholder', 'init'];
141
        $status = $this->runner->run($argv, $this->guy->capturedOutputStream(), $app);
0 ignored issues
show
$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...
142
        $this->guy->seeInOutput('testRoboFile.php will be created in the current directory');
143
        $this->assertEquals(0, $status);
144
145
        $this->assertTrue(file_exists('testRoboFile.php'));
146
        $commandContents = file_get_contents('testRoboFile.php');
147
        unlink('testRoboFile.php');
148
        $this->assertContains('class RoboTestClass', $commandContents);
149
    }
150
151 View Code Duplication
    public function testTasksStopOnFail()
0 ignored issues
show
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...
152
    {
153
        $argv = ['placeholder', 'test:stop-on-fail'];
154
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
155
156
        $this->guy->seeInOutput('[');
157
        $this->assertTrue($result > 0);
158
    }
159
160 View Code Duplication
    public function testInvalidRoboDirectory()
0 ignored issues
show
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...
161
    {
162
        $runnerWithNoRoboFile = new \Robo\Runner();
163
164
        $argv = ['placeholder', 'no-such-command', '-f', 'no-such-directory'];
165
        $result = $runnerWithNoRoboFile->execute($argv, null, null, $this->guy->capturedOutputStream());
0 ignored issues
show
$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...
166
167
        $this->guy->seeInOutput('Path `no-such-directory` is invalid; please provide a valid absolute path to the Robofile to load.');
168
    }
169
170 View Code Duplication
    public function testUnloadableRoboFile()
0 ignored issues
show
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...
171
    {
172
        $runnerWithNoRoboFile = new \Robo\Runner();
173
174
        $argv = ['placeholder', 'help', 'test:custom-event', '-f', dirname(__DIR__) . '/src/RoboFileFixture.php'];
175
        $result = $runnerWithNoRoboFile->execute($argv, null, null, $this->guy->capturedOutputStream());
0 ignored issues
show
$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...
176
177
        // We cannot load RoboFileFixture.php via -f / --load-from because
178
        // it has a namespace, and --load-from does not support that.
179
        $this->guy->seeInOutput('Class RoboFileFixture was not loaded');
180
    }
181
182 View Code Duplication
    public function testRunnerQuietOutput()
0 ignored issues
show
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...
183
    {
184
        $argv = ['placeholder', 'test:verbosity', '--quiet'];
185
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
186
187
        $this->guy->doNotSeeInOutput('This command will print more information at higher verbosity levels');
188
        $this->guy->doNotSeeInOutput('This is a verbose message (-v).');
189
        $this->guy->doNotSeeInOutput('This is a very verbose message (-vv).');
190
        $this->guy->doNotSeeInOutput('This is a debug message (-vvv).');
191
        $this->guy->doNotSeeInOutput(' [warning] This is a warning log message.');
192
        $this->guy->doNotSeeInOutput(' [notice] This is a notice log message.');
193
        $this->guy->doNotSeeInOutput(' [debug] This is a debug log message.');
194
        $this->assertEquals(0, $result);
195
    }
196
197 View Code Duplication
    public function testRunnerVerboseOutput()
0 ignored issues
show
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...
198
    {
199
        $argv = ['placeholder', 'test:verbosity', '-v'];
200
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
201
202
        $this->guy->seeInOutput('This command will print more information at higher verbosity levels');
203
        $this->guy->seeInOutput('This is a verbose message (-v).');
204
        $this->guy->doNotSeeInOutput('This is a very verbose message (-vv).');
205
        $this->guy->doNotSeeInOutput('This is a debug message (-vvv).');
206
        $this->guy->seeInOutput(' [warning] This is a warning log message.');
207
        $this->guy->seeInOutput(' [notice] This is a notice log message.');
208
        $this->guy->doNotSeeInOutput(' [debug] This is a debug log message.');
209
        $this->assertEquals(0, $result);
210
    }
211
212 View Code Duplication
    public function testRunnerVeryVerboseOutput()
0 ignored issues
show
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...
213
    {
214
        $argv = ['placeholder', 'test:verbosity', '-vv'];
215
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
216
217
        $this->guy->seeInOutput('This command will print more information at higher verbosity levels');
218
        $this->guy->seeInOutput('This is a verbose message (-v).');
219
        $this->guy->seeInOutput('This is a very verbose message (-vv).');
220
        $this->guy->doNotSeeInOutput('This is a debug message (-vvv).');
221
        $this->guy->seeInOutput(' [warning] This is a warning log message.');
222
        $this->guy->seeInOutput(' [notice] This is a notice log message.');
223
        $this->guy->doNotSeeInOutput(' [debug] This is a debug log message.');
224
        $this->assertEquals(0, $result);
225
    }
226
227 View Code Duplication
    public function testRunnerVerbosityThresholdVerbose()
0 ignored issues
show
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...
228
    {
229
        $argv = ['placeholder', 'test:verbosity-threshold', '-v'];
230
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
231
232
        $this->guy->seeInOutput('This command will print more information at higher verbosity levels');
233
        $this->guy->seeInOutput("Running echo verbose or higher\nverbose or higher");
234
        $this->guy->doNotSeeInOutput('very verbose or higher');
235
        $this->assertEquals(0, $result);
236
    }
237
238 View Code Duplication
    public function testRunnerVerbosityThresholdVeryVerbose()
0 ignored issues
show
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...
239
    {
240
        $argv = ['placeholder', 'test:verbosity-threshold', '-vv'];
241
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
242
243
        $this->guy->seeInOutput('This command will print more information at higher verbosity levels');
244
        $this->guy->seeInOutput("Running echo verbose or higher\nverbose or higher");
245
        $this->guy->seeInOutput("Running echo very verbose or higher\nvery verbose or higher");
246
        $this->assertEquals(0, $result);
247
    }
248
249 View Code Duplication
    public function testRunnerDebugOutput()
0 ignored issues
show
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...
250
    {
251
        $argv = ['placeholder', 'test:verbosity', '-vvv'];
252
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
253
254
        $this->guy->seeInOutput('This command will print more information at higher verbosity levels');
255
        $this->guy->seeInOutput('This is a verbose message (-v).');
256
        $this->guy->seeInOutput('This is a very verbose message (-vv).');
257
        $this->guy->seeInOutput('This is a debug message (-vvv).');
258
        $this->guy->seeInOutput(' [warning] This is a warning log message.');
259
        $this->guy->seeInOutput(' [notice] This is a notice log message.');
260
        $this->guy->seeInOutput(' [debug] This is a debug log message.');
261
        $this->assertEquals(0, $result);
262
    }
263
}
264