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)', $php_version->resultAsText()); |
62
|
|
|
$this->assertStringContainsString('The PHP Group', $php_version->resultAsText()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testCliOuputAsArray() |
66
|
|
|
{ |
67
|
|
|
$php_version = Executioner::make('php')->addArgument('-v')->execute(); |
68
|
|
|
$result = $php_version->resultAsArray(); |
69
|
|
|
$this->assertIsArray($result); |
70
|
|
|
$this->assertStringContainsString('PHP', $result[0]); |
71
|
|
|
$this->assertStringContainsString('Copyright (c)', $result[1]); |
72
|
|
|
|
73
|
|
|
$whoami = Executioner::make('whoami')->execute(); |
74
|
|
|
$this->assertCount(1, $whoami->resultAsArray()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testCliOuputAsJson() |
78
|
|
|
{ |
79
|
|
|
$php_version = Executioner::make('php')->addArgument('-v')->execute(); |
80
|
|
|
$result = $php_version->resultAsJSON(); |
81
|
|
|
$this->assertIsString($php_version->resultAsJSON($result)); |
|
|
|
|
82
|
|
|
$this->assertStringContainsString('PHP', $result); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testCliOuputAsSerialised() |
86
|
|
|
{ |
87
|
|
|
$php_version = Executioner::make('php')->addArgument('-v')->execute(); |
88
|
|
|
$result = $php_version->resultAsSerialized(); |
89
|
|
|
$this->assertStringContainsString('PHP', $result); |
90
|
|
|
$this->assertStringEndsWith('}', $result); |
91
|
|
|
$unserialised = unserialize($result); |
92
|
|
|
$this->assertIsArray($unserialised); |
93
|
|
|
$this->assertStringContainsString('PHP', $unserialised[0]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testCliOutputFromFluentApi() |
97
|
|
|
{ |
98
|
|
|
$run = new Executioner(); |
99
|
|
|
$run->setApplication('whoami'); |
100
|
|
|
$run->addArgument('--version'); |
101
|
|
|
$run->execute(); |
102
|
|
|
|
103
|
|
|
$output = $run->resultAsJSON(); |
104
|
|
|
$this->assertStringContainsString('Richard', $output); |
105
|
|
|
$this->assertStringContainsString('Free Software Foundation', $output); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGetErrorsFromExecution() |
109
|
|
|
{ |
110
|
|
|
$php_version = Executioner::make('php')->addArgument('-v')->execute(); |
111
|
|
|
$this->assertEquals(0, $php_version->getErrors()->count()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testFailedCommandExecution() |
115
|
|
|
{ |
116
|
|
|
$this->expectException(ExecutionException::class); |
117
|
|
|
$php_version = Executioner::make('phpZ')->execute(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
} |
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.