Completed
Push — master ( 5e7886...331704 )
by Greg
03:33
created

tests/unit/RunnerTest.php (2 issues)

Severity

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