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

IOTest::test_it_can_parse_the_options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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