Passed
Pull Request — master (#468)
by
unknown
02:43
created

IOTest::provideOptionsWithShellVerbosity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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