Passed
Push — master ( 2d3cbc...ef5130 )
by Bobby
07:22
created

ExecutionerTest::testRetrieveStandardCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
4
use Ballen\Executioner\Exceptions\ExecutionException;
5
use PHPUnit\Framework\TestCase;
6
use Ballen\Executioner\Executioner;
7
8
class ExecutionerTest extends TestCase
9
{
10
11
    public function testCanInitiateANewInstance()
12
    {
13
        try {
14
            $php_version = Executioner::make('php')
15
                ->addArgument('-v')
16
                ->execute()
17
                ->resultAsArray();
18
        } catch (ExecutionException $exception) {
19
            $this->fail('Could not execute the requested command.');
20
        }
21
        $this->assertTrue(count($php_version) > 0);
22
    }
23
24
    public function testRetrieveStandardCommand()
25
    {
26
        $php_version = Executioner::make('php')->addArgument('-v');
27
        $this->assertEquals('php -v', $php_version->getCommand());
28
    }
29
30
    public function testRetrieveStdErrorCommand()
31
    {
32
        $php_version = Executioner::make('php')->addArgument('-v')->stderr();
33
        $this->assertEquals('php -v 2>&1', $php_version->getCommand());
34
    }
35
36
    public function testRetrieveSudoCommand()
37
    {
38
        $php_version = Executioner::make('php')->addArgument('-v')->sudo();
39
        $this->assertEquals('sudo php -v', $php_version->getCommand());
40
    }
41
42
    public function testRetrieveStdErrorAndSudoCommand()
43
    {
44
        $php_version = Executioner::make('php')->addArgument('-v')->stderr()->sudo();
45
        $this->assertEquals('sudo php -v 2>&1', $php_version->getCommand());
46
    }
47
48
    public function testFileExecutionChecks()
49
    {
50
        $validCommandPath = Executioner::make('/usr/bin/whoami');
51
        $this->assertTrue($validCommandPath->isExecutable());
52
53
        $invalidCommandPath = Executioner::make('/usr/bin/random-invalid-command');
54
        $this->assertFalse($invalidCommandPath->isExecutable());
55
    }
56
57
    public function testCliOuputAsText()
58
    {
59
        $php_version = Executioner::make('php')->addArgument('-v')->execute();
60
        $this->assertStringContainsString('PHP', $php_version->resultAsText());
61
        $this->assertStringContainsString('Copyright (c) The PHP Group', $php_version->resultAsText());
62
    }
63
64
    public function testCliOuputAsArray()
65
    {
66
        $php_version = Executioner::make('php')->addArgument('-v')->execute();
67
        $result = $php_version->resultAsArray();
68
        $this->assertIsArray($result);
69
        $this->assertStringContainsString('PHP', $result[0]);
70
        $this->assertStringContainsString('Copyright (c)', $result[1]);
71
72
        $whoami = Executioner::make('whoami')->execute();
73
        $this->assertCount(1, $whoami->resultAsArray());
74
    }
75
76
    public function testCliOuputAsJson()
77
    {
78
        $php_version = Executioner::make('php')->addArgument('-v')->execute();
79
        $result = $php_version->resultAsJSON();
80
        $this->assertIsString($php_version->resultAsJSON($result));
0 ignored issues
show
Unused Code introduced by
The call to Ballen\Executioner\Executioner::resultAsJSON() has too many arguments starting with $result. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        $this->assertIsString($php_version->/** @scrutinizer ignore-call */ resultAsJSON($result));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
81
        $this->assertStringContainsString('PHP', $result);
82
    }
83
84
    public function testCliOuputAsSerialised()
85
    {
86
        $php_version = Executioner::make('php')->addArgument('-v')->execute();
87
        $result = $php_version->resultAsSerialized();
88
        $this->assertStringContainsString('PHP', $result);
89
        $this->assertStringEndsWith('}', $result);
90
        $unserialised = unserialize($result);
91
        $this->assertIsArray($unserialised);
92
        $this->assertStringContainsString('PHP', $unserialised[0]);
93
    }
94
95
    public function testCliOutputFromFluentApi()
96
    {
97
        $run = new Executioner();
98
        $run->setApplication('whoami');
99
        $run->addArgument('--version');
100
        $run->execute();
101
102
        $output = $run->resultAsJSON();
103
        $this->assertStringContainsString('Richard', $output);
104
        $this->assertStringContainsString('Free Software Foundation', $output);
105
    }
106
107
    public function testGetErrorsFromExecution()
108
    {
109
        $php_version = Executioner::make('php')->addArgument('-v')->execute();
110
        $this->assertEquals(0, $php_version->getErrors()->count());
111
    }
112
113
    public function testFailedCommandExecution()
114
    {
115
        $this->expectException(ExecutionException::class);
116
        $php_version = Executioner::make('phpZ')->execute();
0 ignored issues
show
Unused Code introduced by
The assignment to $php_version is dead and can be removed.
Loading history...
117
    }
118
119
120
}