Completed
Pull Request — master (#924)
by Greg
03:26
created

ExecTest::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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();
0 ignored issues
show
Bug introduced by
The method interactive does only exist in Robo\Task\Base\Exec, but not in Robo\Collection\CollectionBuilder.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method interactive does only exist in Robo\Task\Base\Exec, but not in Robo\Collection\CollectionBuilder.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
33
        $task->env('FOO', 'BAR');
34
        $task->env('BAR', 'BAZ');
35
        $result = $task->run();
36
        $this->assertTrue($result->wasSuccessful());
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);
0 ignored issues
show
Bug introduced by
The method interactive does only exist in Robo\Task\Base\Exec, but not in Robo\Collection\CollectionBuilder.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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