Completed
Push — master ( 6d0042...eaca05 )
by Greg
05:08
created

RunnerTest::testStyleInjector()   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
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
    public function testStyleInjector()
70
    {
71
        $argv = ['placeholder', 'test:style-injector'];
72
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
73
        $this->guy->seeInOutput('Some text in section one printed via injected io object.');
74
    }
75
76
    public function testSymfony()
77
    {
78
        $argv = ['placeholder', 'test:symfony', 'a', 'b', 'c', '--foo=bar', '--foo=baz', '--foo=boz'];
79
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
80
        $expected = <<<EOT
81
>  The parameters passed are:
82
array (
83
  0 => 'a',
84
  1 => 'b',
85
  2 => 'c',
86
)
87
>  The options passed via --foo are:
88
array (
89
  0 => 'bar',
90
  1 => 'baz',
91
  2 => 'boz',
92
)
93
94
EOT;
95
        $this->guy->seeOutputEquals($expected);
96
    }
97
98 View Code Duplication
    public function testCommandEventHook()
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...
99
    {
100
        $argv = ['placeholder', 'test:command-event'];
101
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
102
103
        $expected = <<<EOT
104
 This is the command-event hook for the test:command-event command.
105
 This is the main method for the test:command-event command.
106
 This is the post-command hook for the test:command-event command.
107
EOT;
108
        $this->guy->seeInOutput($expected);
109
    }
110
111 View Code Duplication
    public function testCustomEventHook()
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...
112
    {
113
        $argv = ['placeholder', 'test:custom-event'];
114
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
115
116
        $expected = 'one,two';
117
        $this->guy->seeInOutput($expected);
118
    }
119
120
    public function testRoboStaticRunMethod()
121
    {
122
        $argv = ['placeholder', 'test:symfony-style'];
123
        $commandFiles = ['\Robo\RoboFileFixture'];
124
        Robo::run($argv, $commandFiles, 'MyApp', '1.2.3', $this->guy->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...
125
        $this->guy->seeInOutput('Some text in section one.');
126
    }
127
128 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...
129
    {
130
        $argv = ['placeholder', 'test:deploy', '--simulate'];
131
        $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
132
        $this->guy->seeInOutput('[Simulator] Simulating Remote\\Ssh(\'mysite.com\', null)');
133
        $this->guy->seeInOutput('[Simulator] Running ssh mysite.com \'cd "/var/www/somesite" && git pull\'');
134
    }
135
136 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...
137
    {
138
        $argv = ['placeholder', 'test:error'];
139
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
140
141
        $this->guy->seeInOutput('[Exec] Running ls xyzzy');
142
        $this->assertTrue($result > 0);
143
    }
144
145 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...
146
    {
147
        $argv = ['placeholder', 'test:error', '--simulate'];
148
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
149
150
        $this->guy->seeInOutput('Simulating Exec');
151
        $this->assertEquals(0, $result);
152
    }
153
154 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...
155
    {
156
        $argv = ['placeholder', 'test:exception', '--task'];
157
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
158
159
        $this->guy->seeInOutput('Task failed with an exception');
160
        $this->assertEquals(1, $result);
161
    }
162
163
    public function testInitCommand()
164
    {
165
        $container = \Robo\Robo::getContainer();
166
        $app = $container->get('application');
167
        $app->addInitRoboFileCommand(getcwd() . '/testRoboFile.php', 'RoboTestClass');
168
169
        $argv = ['placeholder', 'init'];
170
        $status = $this->runner->run($argv, $this->guy->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...
171
        $this->guy->seeInOutput('testRoboFile.php will be created in the current directory');
172
        $this->assertEquals(0, $status);
173
174
        $this->assertTrue(file_exists('testRoboFile.php'));
175
        $commandContents = file_get_contents('testRoboFile.php');
176
        unlink('testRoboFile.php');
177
        $this->assertContains('class RoboTestClass', $commandContents);
178
    }
179
180 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...
181
    {
182
        $argv = ['placeholder', 'test:stop-on-fail'];
183
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
184
185
        $this->guy->seeInOutput('[');
186
        $this->assertTrue($result > 0);
187
    }
188
189 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...
190
    {
191
        $runnerWithNoRoboFile = new \Robo\Runner();
192
193
        $argv = ['placeholder', 'no-such-command', '-f', 'no-such-directory'];
194
        $result = $runnerWithNoRoboFile->execute($argv, null, null, $this->guy->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...
195
196
        $this->guy->seeInOutput('Path `no-such-directory` is invalid; please provide a valid absolute path to the Robofile to load.');
197
    }
198
199 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...
200
    {
201
        $runnerWithNoRoboFile = new \Robo\Runner();
202
203
        $argv = ['placeholder', 'help', 'test:custom-event', '-f', dirname(__DIR__) . '/src/RoboFileFixture.php'];
204
        $result = $runnerWithNoRoboFile->execute($argv, null, null, $this->guy->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...
205
206
        // We cannot load RoboFileFixture.php via -f / --load-from because
207
        // it has a namespace, and --load-from does not support that.
208
        $this->guy->seeInOutput('Class RoboFileFixture was not loaded');
209
    }
210
211 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...
212
    {
213
        $argv = ['placeholder', 'test:verbosity', '--quiet'];
214
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
215
216
        $this->guy->doNotSeeInOutput('This command will print more information at higher verbosity levels');
217
        $this->guy->doNotSeeInOutput('This is a verbose message (-v).');
218
        $this->guy->doNotSeeInOutput('This is a very verbose message (-vv).');
219
        $this->guy->doNotSeeInOutput('This is a debug message (-vvv).');
220
        $this->guy->doNotSeeInOutput(' [warning] This is a warning log message.');
221
        $this->guy->doNotSeeInOutput(' [notice] This is a notice log message.');
222
        $this->guy->doNotSeeInOutput(' [debug] This is a debug log message.');
223
        $this->assertEquals(0, $result);
224
    }
225
226 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...
227
    {
228
        $argv = ['placeholder', 'test:verbosity', '-v'];
229
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
230
231
        $this->guy->seeInOutput('This command will print more information at higher verbosity levels');
232
        $this->guy->seeInOutput('This is a verbose message (-v).');
233
        $this->guy->doNotSeeInOutput('This is a very verbose message (-vv).');
234
        $this->guy->doNotSeeInOutput('This is a debug message (-vvv).');
235
        $this->guy->seeInOutput(' [warning] This is a warning log message.');
236
        $this->guy->seeInOutput(' [notice] This is a notice log message.');
237
        $this->guy->doNotSeeInOutput(' [debug] This is a debug log message.');
238
        $this->assertEquals(0, $result);
239
    }
240
241 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...
242
    {
243
        $argv = ['placeholder', 'test:verbosity', '-vv'];
244
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
245
246
        $this->guy->seeInOutput('This command will print more information at higher verbosity levels');
247
        $this->guy->seeInOutput('This is a verbose message (-v).');
248
        $this->guy->seeInOutput('This is a very verbose message (-vv).');
249
        $this->guy->doNotSeeInOutput('This is a debug message (-vvv).');
250
        $this->guy->seeInOutput(' [warning] This is a warning log message.');
251
        $this->guy->seeInOutput(' [notice] This is a notice log message.');
252
        $this->guy->doNotSeeInOutput(' [debug] This is a debug log message.');
253
        $this->assertEquals(0, $result);
254
    }
255
256 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...
257
    {
258
        $argv = ['placeholder', 'test:verbosity-threshold', '-v'];
259
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
260
261
        $this->guy->seeInOutput('This command will print more information at higher verbosity levels');
262
        $this->guy->seeInOutput("Running echo verbose or higher\nverbose or higher");
263
        $this->guy->doNotSeeInOutput('very verbose or higher');
264
        $this->assertEquals(0, $result);
265
    }
266
267 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...
268
    {
269
        $argv = ['placeholder', 'test:verbosity-threshold', '-vv'];
270
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
271
272
        $this->guy->seeInOutput('This command will print more information at higher verbosity levels');
273
        $this->guy->seeInOutput("Running echo verbose or higher\nverbose or higher");
274
        $this->guy->seeInOutput("Running echo very verbose or higher\nvery verbose or higher");
275
        $this->assertEquals(0, $result);
276
    }
277
278 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...
279
    {
280
        $argv = ['placeholder', 'test:verbosity', '-vvv'];
281
        $result = $this->runner->execute($argv, null, null, $this->guy->capturedOutputStream());
282
283
        $this->guy->seeInOutput('This command will print more information at higher verbosity levels');
284
        $this->guy->seeInOutput('This is a verbose message (-v).');
285
        $this->guy->seeInOutput('This is a very verbose message (-vv).');
286
        $this->guy->seeInOutput('This is a debug message (-vvv).');
287
        $this->guy->seeInOutput(' [warning] This is a warning log message.');
288
        $this->guy->seeInOutput(' [notice] This is a notice log message.');
289
        $this->guy->seeInOutput(' [debug] This is a debug log message.');
290
        $this->assertEquals(0, $result);
291
    }
292
}
293