Passed
Push — master ( d72e2b...6ee475 )
by Théo
02:15
created

IOTest::getDefaultInteractive()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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