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->assertContains( |
22
|
|
|
'src', |
23
|
|
|
$res->getMessage()); |
24
|
|
|
$this->assertContains( |
25
|
|
|
'codeception.yml', |
26
|
|
|
$res->getMessage()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testMultipleEnvVars() |
30
|
|
|
{ |
31
|
|
|
$task = $this->taskExec('env')->interactive(false); |
|
|
|
|
32
|
|
|
$task->env('FOO', 'BAR'); |
33
|
|
|
$task->env('BAR', 'BAZ'); |
34
|
|
|
$result = $task->run(); |
35
|
|
|
// Verify that the text contains our environment variable. |
36
|
|
|
$this->assertContains( |
37
|
|
|
'FOO=BAR', |
38
|
|
|
$result->getMessage()); |
39
|
|
|
$this->assertContains( |
40
|
|
|
'BAR=BAZ', |
41
|
|
|
$result->getMessage()); |
42
|
|
|
|
43
|
|
|
// Now verify that we can reset a value that was previously set. |
44
|
|
|
$task = $this->taskExec('env')->interactive(false); |
45
|
|
|
$task->env('FOO', 'BAR'); |
46
|
|
|
$task->env('FOO', 'BAZ'); |
47
|
|
|
$result = $task->run(); |
48
|
|
|
// Verify that the text contains the most recent environment variable. |
49
|
|
|
$this->assertContains( |
50
|
|
|
'FOO=BAZ', |
51
|
|
|
$result->getMessage()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testInheritEnv() |
55
|
|
|
{ |
56
|
|
|
// Symfony < 3.2.1 does not inherit environment variables, so there's |
57
|
|
|
// nothing to test if the function doesn't exist. |
58
|
|
|
if (!method_exists('Symfony\Component\Process\Process', 'inheritEnvironmentVariables')) { |
59
|
|
|
throw new \PHPUnit_Framework_SkippedTestError( |
60
|
|
|
'Inheriting of environment variables is not supported.' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
// With no environment variables set, count how many environment |
64
|
|
|
// variables are present. |
65
|
|
|
$task = $this->taskExec('env | wc -l')->interactive(false); |
|
|
|
|
66
|
|
|
$result = $task->run(); |
67
|
|
|
$start_count = (int) $result->getMessage(); |
68
|
|
|
$this->assertGreaterThan(0, $start_count); |
69
|
|
|
|
70
|
|
|
// Verify that we get the same amount of environment variables with |
71
|
|
|
// another exec call. |
72
|
|
|
$task = $this->taskExec('env | wc -l')->interactive(false); |
73
|
|
|
$result = $task->run(); |
74
|
|
|
$this->assertEquals( |
75
|
|
|
$start_count, |
76
|
|
|
(int) $result->getMessage()); |
77
|
|
|
|
78
|
|
|
// Now run the same command, but this time add another environment |
79
|
|
|
// variable, and see if our count increases by one. |
80
|
|
|
$task = $this->taskExec('env | wc -l')->interactive(false); |
81
|
|
|
$task->env('FOO', 'BAR'); |
82
|
|
|
$result = $task->run(); |
83
|
|
|
$this->assertEquals($start_count + 1, (int) $result->getMessage()); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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: