Passed
Push — master ( e2c0d9...d72e2b )
by Théo
02:21
created

IOTest::setUpBeforeClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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