Completed
Push — master ( 8d0ff5...88dfaf )
by Greg
02:36
created

ExecCest::testInheritEnv()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
class ExecCest
4
{
5
    // tests
6
    public function toExecLsCommand(CliGuy $I)
7
    {
8
        $command = strncasecmp(PHP_OS, 'WIN', 3) == 0 ? 'dir' : 'ls';
9
        $res = $I->taskExec($command)->interactive(false)->run();
10
        verify($res->getMessage())->contains('src');
11
        verify($res->getMessage())->contains('codeception.yml');
12
    }
13
14
    public function testMultipleEnvVars(CliGuy $I)
15
    {
16
        $task = $I->taskExec('env')->interactive(false);
17
        $task->env('FOO', 'BAR');
18
        $task->env('BAR', 'BAZ');
19
        $result = $task->run();
20
        // Verify that the text contains our environment variable.
21
        verify($result->getMessage())->contains('FOO=BAR');
22
        verify($result->getMessage())->contains('BAR=BAZ');
23
24
        // Now verify that we can reset a value that was previously set.
25
        $task = $I->taskExec('env')->interactive(false);
26
        $task->env('FOO', 'BAR');
27
        $task->env('FOO', 'BAZ');
28
        $result = $task->run();
29
        // Verify that the text contains the most recent environment variable.
30
        verify($result->getMessage())->contains('FOO=BAZ');
31
    }
32
33
    public function testInheritEnv(CliGuy $I)
34
    {
35
        // Symfony < 3.2.1 does not inherit environment variables, so there's
36
        // nothing to test if the function doesn't exist.
37
        if (!method_exists('Symfony\Component\Process\Process', 'inheritEnvironmentVariables')) {
38
            throw new \PHPUnit_Framework_SkippedTestError(
39
                'Inheriting of environment variables is not supported.'
40
            );
41
        }
42
        // With no environment variables set, count how many environment
43
        // variables are present.
44
        $task = $I->taskExec('env | wc -l')->interactive(false);
45
        $result = $task->run();
46
        $start_count = (int) $result->getMessage();
47
        verify($start_count)->greaterThan(0);
48
49
        // Verify that we get the same amount of environment variables with
50
        // another exec call.
51
        $task = $I->taskExec('env | wc -l')->interactive(false);
52
        $result = $task->run();
53
        verify((int) $result->getMessage())->equals($start_count);
54
55
        // Now run the same command, but this time add another environment
56
        // variable, and see if our count increases by one.
57
        $task = $I->taskExec('env | wc -l')->interactive(false);
58
        $task->env('FOO', 'BAR');
59
        $result = $task->run();
60
        verify((int) $result->getMessage())->equals($start_count + 1);
61
    }
62
}
63