Completed
Push — master ( 8b8afe...5f2bbe )
by Greg
02:21
created

tests/unit/Common/CommandArgumentsTest.php (1 issue)

Check for implicit conversion of array to boolean.

Best Practice Bug Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Robo\Traits\Common\CommandArgumentsHost;
4
5
/**
6
 * Class CommandArgumentsTest.
7
 *
8
 * @coversDefaultClass \Robo\Common\CommandArguments
9
 */
10
class CommandArgumentsTest extends \Codeception\Test\Unit
11
{
12
    /**
13
     * @var \CodeGuy
14
     */
15
    protected $guy;
16
17
    public function casesArgs() {
18
        return [
19
            'no arguments' => [
20
                ' ',
21
                ' ',
22
                [],
23
            ],
24
            'empty string' => [
25
                " ''",
26
                ' ""',
27
                [''],
28
            ],
29
            'space' => [
30
                " ' '",
31
                ' " "',
32
                [' '],
33
            ],
34
            'no escape - a' => [
35
                " a",
36
                " a",
37
                ['a'],
38
            ],
39
            'no escape - A' => [
40
                " A",
41
                " A",
42
                ['A'],
43
            ],
44
            'no escape - 0' => [
45
                " 0",
46
                " 0",
47
                ['0'],
48
            ],
49
            'no escape - --' => [
50
                " --",
51
                " --",
52
                ['--'],
53
            ],
54
            'no escape - @_~.' => [
55
                " @_~.",
56
                " @_~.",
57
                ['@_~.'],
58
            ],
59
            '$' => [
60
                " 'a\$b'",
61
                ' "a$b"',
62
                ['a$b'],
63
            ],
64
            '*' => [
65
                " 'a*b'",
66
                ' "a*b"',
67
                ['a*b'],
68
            ],
69
            'multi' => [
70
                " '' a '\$PATH'",
71
                ' "" a "$PATH"',
72
                ['', 'a', '$PATH'],
73
            ],
74
        ];
75
    }
76
77
    /**
78
     * @dataProvider casesArgs
79
     *
80
     * @covers ::args
81
     *
82
     * @param string $expected
83
     * @param array $args
84
     */
85
    public function testArgs($expectedLinux, $expectedWindows, $args)
86
    {
87
        $expected = stripos(PHP_OS, 'WIN') === 0 ? $expectedWindows : $expectedLinux;
88
        $commandArguments = new CommandArgumentsHost();
89
        $commandArguments->args($args);
90
        $this->guy->assertEquals($expected, $commandArguments->getArguments());
91
92
        if ($args) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
93
            $commandArguments = new CommandArgumentsHost();
94
            call_user_func_array([$commandArguments, 'args'], $args);
95
            $this->guy->assertEquals($expected, $commandArguments->getArguments());
96
        }
97
    }
98
}
99