Passed
Push — master ( 28642b...43c607 )
by Théo
02:07
created

requirement-checker/tests/IOTest.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\RequirementChecker;
16
17
use Generator;
18
use function getenv;
19
use PHPUnit\Framework\TestCase;
20
use function putenv;
21
22
/**
23
 * @covers \KevinGH\RequirementChecker\IO
24
 */
25
class IOTest extends TestCase
26
{
27
    private static $defaultExpectedInteractive;
28
    
29
    private static function getDefaultInteractive(): bool
30
    {
31
        // @see https://github.com/travis-ci/travis-ci/issues/7967
32
        // When a secure env var is present, the TTY is not passed correctly. The output is no longer interactive and
33
        // colored.
34
        if (null === self::$defaultExpectedInteractive) {
35
            if (false !== getenv('CI')) {
36
                self::$defaultExpectedInteractive = true !== getenv('TRAVIS_SECURE_ENV_VARS');
37
            } else {
38
                self::$defaultExpectedInteractive = true;
39
            }
40
        }
41
42
        \file_put_contents('php://stderr', \var_export([
0 ignored issues
show
Are you sure the usage of var_export(array('CI' =>...VIS_SECURE_ENV_VARS'))) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
            'CI' => getenv('CI'),
44
            'TRAVIS_SECURE_ENV_VARS' => getenv('TRAVIS_SECURE_ENV_VARS'),
45
        ]));
46
        
47
        return self::$defaultExpectedInteractive;
48
    }
49
50
    /**
51
     * @dataProvider provideOptions
52
     */
53
    public function test_it_can_parse_the_options(array $argv, bool $interactive, int $verbosity): void
54
    {
55
        $_SERVER['argv'] = $argv;
56
57
        $io = new IO();
58
59
        $this->assertSame($interactive, $io->isInteractive());
60
        $this->assertSame($verbosity, $io->getVerbosity());
61
    }
62
63
    /**
64
     * @dataProvider provideOptionsWithShellVerbosity
65
     */
66
    public function test_it_uses_the_shell_verbosity_environment_variable_over_the_options(array $argv, string $putenv, bool $interactive, int $verbosity): void
67
    {
68
        $_SERVER['argv'] = $argv;
69
        putenv($putenv);
70
71
        $io = new IO();
72
73
        $this->assertSame($interactive, $io->isInteractive());
74
        $this->assertSame($verbosity, $io->getVerbosity());
75
    }
76
77
    public function provideOptions(): Generator
78
    {
79
        yield [
80
            ['cli.php', '--foo'],
81
            self::getDefaultInteractive(),
82
            IO::VERBOSITY_NORMAL,
83
        ];
84
85
        yield [
86
            ['cli.php', '--foo', '--verbose=0'],
87
            self::getDefaultInteractive(),
88
            IO::VERBOSITY_VERBOSE,
89
        ];
90
91
        yield [
92
            ['cli.php', '--foo', '--quiet'],
93
            false,
94
            IO::VERBOSITY_QUIET,
95
        ];
96
97
        yield [
98
            ['cli.php', '--foo', '-q'],
99
            false,
100
            IO::VERBOSITY_QUIET,
101
        ];
102
103
        yield [
104
            ['cli.php', '--foo', '-vvv'],
105
            self::getDefaultInteractive(),
106
            IO::VERBOSITY_DEBUG,
107
        ];
108
109
        yield [
110
            ['cli.php', '--foo', '--verbose=3'],
111
            self::getDefaultInteractive(),
112
            IO::VERBOSITY_DEBUG,
113
        ];
114
115
        yield [
116
            ['cli.php', '--foo', '--verbose  3'],
117
            self::getDefaultInteractive(),
118
            IO::VERBOSITY_DEBUG,
119
        ];
120
121
        yield [
122
            ['cli.php', '--foo', '-vv'],
123
            self::getDefaultInteractive(),
124
            IO::VERBOSITY_VERY_VERBOSE,
125
        ];
126
127
        yield [
128
            ['cli.php', '--foo', '--verbose=2'],
129
            self::getDefaultInteractive(),
130
            IO::VERBOSITY_VERY_VERBOSE,
131
        ];
132
133
        yield [
134
            ['cli.php', '--foo', '--verbose  2'],
135
            self::getDefaultInteractive(),
136
            IO::VERBOSITY_VERY_VERBOSE,
137
        ];
138
139
        yield [
140
            ['cli.php', '--foo', '-v'],
141
            self::getDefaultInteractive(),
142
            IO::VERBOSITY_VERBOSE,
143
        ];
144
145
        yield [
146
            ['cli.php', '--foo', '--verbose=1'],
147
            self::getDefaultInteractive(),
148
            IO::VERBOSITY_VERBOSE,
149
        ];
150
151
        yield [
152
            ['cli.php', '--foo', '--verbose  '],
153
            self::getDefaultInteractive(),
154
            IO::VERBOSITY_VERBOSE,
155
        ];
156
157
        yield [
158
            ['cli.php', '--no-interaction'],
159
            false,
160
            IO::VERBOSITY_NORMAL,
161
        ];
162
163
        yield [
164
            ['cli.php', '-n'],
165
            false,
166
            IO::VERBOSITY_NORMAL,
167
        ];
168
    }
169
170
    public function provideOptionsWithShellVerbosity(): Generator
171
    {
172
        yield [
173
            ['cli.php', '--foo'],
174
            'SHELL_VERBOSITY=-1',
175
            false,
176
            IO::VERBOSITY_QUIET,
177
        ];
178
179
        yield [
180
            ['cli.php', '--foo'],
181
            'SHELL_VERBOSITY=0',
182
            self::getDefaultInteractive(),
183
            IO::VERBOSITY_NORMAL,
184
        ];
185
186
        yield [
187
            ['cli.php', '--foo'],
188
            'SHELL_VERBOSITY=1',
189
            self::getDefaultInteractive(),
190
            IO::VERBOSITY_VERBOSE,
191
        ];
192
193
        yield [
194
            ['cli.php', '--foo'],
195
            'SHELL_VERBOSITY=2',
196
            self::getDefaultInteractive(),
197
            IO::VERBOSITY_VERY_VERBOSE,
198
        ];
199
200
        yield [
201
            ['cli.php', '--foo'],
202
            'SHELL_VERBOSITY=3',
203
            self::getDefaultInteractive(),
204
            IO::VERBOSITY_DEBUG,
205
        ];
206
    }
207
}
208