|
1
|
|
|
<?php |
|
2
|
|
|
namespace Robo; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
|
5
|
|
|
use Robo\Traits\TestTasksTrait; |
|
6
|
|
|
|
|
7
|
|
|
class ExecTest extends TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
use TestTasksTrait; |
|
10
|
|
|
use Task\Base\loadTasks; |
|
11
|
|
|
|
|
12
|
|
|
public function setup() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->initTestTasksTrait(); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function testExecLsCommand() |
|
18
|
|
|
{ |
|
19
|
|
|
$command = strncasecmp(PHP_OS, 'WIN', 3) == 0 ? 'dir' : 'ls'; |
|
20
|
|
|
$res = $this->taskExec($command)->interactive(false)->run(); |
|
|
|
|
|
|
21
|
|
|
$this->assertTrue($res->wasSuccessful()); |
|
22
|
|
|
$this->assertContains( |
|
23
|
|
|
'src', |
|
24
|
|
|
$res->getMessage()); |
|
25
|
|
|
$this->assertContains( |
|
26
|
|
|
'codeception.yml', |
|
27
|
|
|
$res->getMessage()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testMultipleEnvVars() |
|
31
|
|
|
{ |
|
32
|
|
|
$task = $this->taskExec('env')->interactive(false); |
|
|
|
|
|
|
33
|
|
|
$task->env('FOO', 'BAR'); |
|
34
|
|
|
$task->env('BAR', 'BAZ'); |
|
35
|
|
|
$result = $task->run(); |
|
36
|
|
|
$this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
|
37
|
|
|
// Verify that the text contains our environment variable. |
|
38
|
|
|
$this->assertContains( |
|
39
|
|
|
'FOO=BAR', |
|
40
|
|
|
$result->getMessage()); |
|
41
|
|
|
$this->assertContains( |
|
42
|
|
|
'BAR=BAZ', |
|
43
|
|
|
$result->getMessage()); |
|
44
|
|
|
|
|
45
|
|
|
// Now verify that we can reset a value that was previously set. |
|
46
|
|
|
$task = $this->taskExec('env')->interactive(false); |
|
47
|
|
|
$task->env('FOO', 'BAR'); |
|
48
|
|
|
$task->env('FOO', 'BAZ'); |
|
49
|
|
|
$result = $task->run(); |
|
50
|
|
|
$this->assertTrue($result->wasSuccessful()); |
|
51
|
|
|
// Verify that the text contains the most recent environment variable. |
|
52
|
|
|
$this->assertContains( |
|
53
|
|
|
'FOO=BAZ', |
|
54
|
|
|
$result->getMessage()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testInheritEnv() |
|
58
|
|
|
{ |
|
59
|
|
|
// Symfony < 3.2.1 does not inherit environment variables, so there's |
|
60
|
|
|
// nothing to test if the function doesn't exist. |
|
61
|
|
|
if (!method_exists('Symfony\Component\Process\Process', 'inheritEnvironmentVariables')) { |
|
62
|
|
|
throw new \PHPUnit_Framework_SkippedTestError( |
|
63
|
|
|
'Inheriting of environment variables is not supported.' |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
// With no environment variables set, count how many environment |
|
67
|
|
|
// variables are present. |
|
68
|
|
|
$task = $this->taskExec('env | wc -l')->interactive(false); |
|
|
|
|
|
|
69
|
|
|
$result = $task->run(); |
|
70
|
|
|
$this->assertTrue($result->wasSuccessful()); |
|
71
|
|
|
$start_count = (int) $result->getMessage(); |
|
72
|
|
|
$this->assertGreaterThan(0, $start_count); |
|
73
|
|
|
|
|
74
|
|
|
// Verify that we get the same amount of environment variables with |
|
75
|
|
|
// another exec call. |
|
76
|
|
|
$task = $this->taskExec('env | wc -l')->interactive(false); |
|
77
|
|
|
$result = $task->run(); |
|
78
|
|
|
$this->assertTrue($result->wasSuccessful()); |
|
79
|
|
|
$this->assertEquals( |
|
80
|
|
|
$start_count, |
|
81
|
|
|
(int) $result->getMessage()); |
|
82
|
|
|
|
|
83
|
|
|
// Now run the same command, but this time add another environment |
|
84
|
|
|
// variable, and see if our count increases by one. |
|
85
|
|
|
$task = $this->taskExec('env | wc -l')->interactive(false); |
|
86
|
|
|
$task->env('FOO', 'BAR'); |
|
87
|
|
|
$result = $task->run(); |
|
88
|
|
|
$this->assertTrue($result->wasSuccessful()); |
|
89
|
|
|
$this->assertEquals($start_count + 1, (int) $result->getMessage()); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: