Passed
Push — main ( c1e8d7...13f864 )
by Sebastian
03:58
created

DefaultIOTest::testAskNoHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Console\IO;
13
14
use CaptainHook\App\Console\IO;
15
use Exception;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\ConsoleOutputInterface;
19
use Symfony\Component\Console\Helper\HelperSet;
20
use Symfony\Component\Console\Helper\QuestionHelper;
21
use PHPUnit\Framework\MockObject\MockObject;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\MockObject\MockObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use PHPUnit\Framework\TestCase;
23
24
class DefaultIOTest extends TestCase
25
{
26
    /**
27
     * @return \Symfony\Component\Console\Input\InputInterface&MockObject
28
     */
29
    public function getInputMock()
30
    {
31
        return $this->getMockBuilder(InputInterface::class)
32
                    ->disableOriginalConstructor()
33
                    ->getMock();
34
    }
35
36
    /**
37
     * @return \Symfony\Component\Console\Output\ConsoleOutputInterface&MockObject
38
     */
39
    public function getConsoleOutputMock()
40
    {
41
        return $this->getMockBuilder(ConsoleOutputInterface::class)
42
                    ->disableOriginalConstructor()
43
                    ->getMock();
44
    }
45
46
    /**
47
     * @return \Symfony\Component\Console\Output\OutputInterface&MockObject
48
     */
49
    public function getOutputMock()
50
    {
51
        return $this->getMockBuilder(OutputInterface::class)
52
                     ->disableOriginalConstructor()
53
                     ->getMock();
54
    }
55
56
    /**
57
     * @return \Symfony\Component\Console\Helper\HelperSet&MockObject
58
     */
59
    public function getHelperSetMock()
60
    {
61
        return $this->getMockBuilder(HelperSet::class)
62
                    ->disableOriginalConstructor()
63
                    ->getMock();
64
    }
65
66
    /**
67
     * @return \Symfony\Component\Console\Helper\QuestionHelper&MockObject
68
     */
69
    public function getQuestionHelper()
70
    {
71
        return $this->getMockBuilder(QuestionHelper::class)
72
                    ->disableOriginalConstructor()
73
                    ->getMock();
74
    }
75
76
    /**
77
     * Tests DefaultIO::getArguments
78
     */
79
    public function testGetArguments(): void
80
    {
81
        $input  = $this->getInputMock();
82
        $output = $this->getOutputMock();
83
        $helper = $this->getHelperSetMock();
84
85
        $input->expects($this->once())->method('getArguments')->willReturn(['foo' => 'bar']);
86
        $io = new DefaultIO($input, $output, $helper);
87
88
        $this->assertEquals(['foo' => 'bar'], $io->getArguments());
89
    }
90
91
    /**
92
     * Tests DefaultIO::getArgument
93
     */
94
    public function testGetArgument(): void
95
    {
96
        $input  = $this->getInputMock();
97
        $output = $this->getOutputMock();
98
        $helper = $this->getHelperSetMock();
99
100
        $input->expects($this->exactly(2))->method('getArguments')->willReturn(['foo' => 'bar']);
101
        $io = new DefaultIO($input, $output, $helper);
102
103
        $this->assertEquals('bar', $io->getArgument('foo'));
104
        $this->assertEquals('bar', $io->getArgument('fiz', 'bar'));
105
    }
106
107
108
    /**
109
     * Tests DefaultIO::getStandardInput
110
     */
111
    public function testGetStandardInput(): void
112
    {
113
        $input  = $this->getInputMock();
114
        $output = $this->getOutputMock();
115
        $helper = $this->getHelperSetMock();
116
117
        $input->expects($this->atLeastOnce())->method('getOption')->willReturn(
118
            file_get_contents(CH_PATH_FILES . '/input/stdin.txt')
119
        );
120
121
        $io = new DefaultIO($input, $output, $helper);
122
123
        $this->assertCount(3, $io->getStandardInput());
124
    }
125
126
    /**
127
     * Tests DefaultIO::isInteractive
128
     */
129
    public function testIsInteractive(): void
130
    {
131
        $input  = $this->getInputMock();
132
        $output = $this->getOutputMock();
133
        $helper = $this->getHelperSetMock();
134
135
        $input->expects($this->once())->method('isInteractive')->willReturn(false);
136
        $io = new DefaultIO($input, $output, $helper);
137
138
        $this->assertFalse($io->isInteractive());
139
    }
140
141
    /**
142
     * Tests DefaultIO::isVerbose
143
     */
144
    public function testIsVerbose(): void
145
    {
146
        $input  = $this->getInputMock();
147
        $output = $this->getOutputMock();
148
        $helper = $this->getHelperSetMock();
149
150
        $output->expects($this->once())->method('getVerbosity')->willReturn(0);
151
        $io = new DefaultIO($input, $output, $helper);
152
153
        $this->assertFalse($io->isVerbose());
154
    }
155
156
    /**
157
     * Tests DefaultIO::isVeryVerbose
158
     */
159
    public function testIsVeryVerbose(): void
160
    {
161
        $input  = $this->getInputMock();
162
        $output = $this->getOutputMock();
163
        $helper = $this->getHelperSetMock();
164
165
        $output->expects($this->once())->method('getVerbosity')->willReturn(0);
166
        $io = new DefaultIO($input, $output, $helper);
167
168
        $this->assertFalse($io->isVeryVerbose());
169
    }
170
171
    /**
172
     * Tests DefaultIO::isDebug
173
     */
174
    public function testIsDebug(): void
175
    {
176
        $input  = $this->getInputMock();
177
        $output = $this->getOutputMock();
178
        $helper = $this->getHelperSetMock();
179
180
        $output->expects($this->once())->method('getVerbosity')->willReturn(0);
181
        $io = new DefaultIO($input, $output, $helper);
182
183
        $this->assertFalse($io->isDebug());
184
    }
185
186
    /**
187
     * Tests DefaultIO::writeError
188
     */
189
    public function testWriteError(): void
190
    {
191
        $input  = $this->getInputMock();
192
        $output = $this->getOutputMock();
193
        $helper = $this->getHelperSetMock();
194
195
        $output->expects($this->once())->method('getVerbosity')->willReturn(OutputInterface::VERBOSITY_DEBUG);
196
        $io = new DefaultIO($input, $output, $helper);
197
198
        $io->writeError('foo');
199
    }
200
201
    /**
202
     * Tests DefaultIO::ask
203
     */
204
    public function testAsk(): void
205
    {
206
        $input          = $this->getInputMock();
207
        $output         = $this->getOutputMock();
208
        $helper         = $this->getHelperSetMock();
209
        $questionHelper = $this->getQuestionHelper();
210
211
        $helper->expects($this->once())->method('get')->willReturn($questionHelper);
212
        $questionHelper->expects($this->once())->method('ask')->willReturn('y');
213
214
        $io     = new DefaultIO($input, $output, $helper);
215
        $answer = $io->ask('foo');
216
        $this->assertEquals('y', $answer);
217
    }
218
219
    public function testAskNoHelper(): void
220
    {
221
        $this->expectException(Exception::class);
222
223
        $input  = $this->getInputMock();
224
        $output = $this->getOutputMock();
225
        $io     = new DefaultIO($input, $output);
226
        $io->ask('foo');
227
    }
228
229
    public function testAskConfirmationNoHelper(): void
230
    {
231
        $this->expectException(Exception::class);
232
233
        $input  = $this->getInputMock();
234
        $output = $this->getOutputMock();
235
        $io     = new DefaultIO($input, $output);
236
        $io->askConfirmation('foo');
237
    }
238
239
240
    public function testAskAndValidateNoHelper(): void
241
    {
242
        $this->expectException(Exception::class);
243
244
        $input  = $this->getInputMock();
245
        $output = $this->getOutputMock();
246
        $io     = new DefaultIO($input, $output);
247
        $io->askAndValidate('foo', function() { return true; });
248
    }
249
250
    /**
251
     * Tests DefaultIO::askConfirmation
252
     */
253
    public function testAskConfirmation(): void
254
    {
255
        $input          = $this->getInputMock();
256
        $output         = $this->getOutputMock();
257
        $helper         = $this->getHelperSetMock();
258
        $questionHelper = $this->getQuestionHelper();
259
260
        $helper->expects($this->once())->method('get')->willReturn($questionHelper);
261
        $questionHelper->expects($this->once())->method('ask')->willReturn('y');
262
263
        $io     = new DefaultIO($input, $output, $helper);
264
        $answer = $io->askConfirmation('foo');
265
        $this->assertTrue($answer);
266
    }
267
268
    /**
269
     * Tests DefaultIO::askAbdValidate
270
     *
271
     * @throws \Exception
272
     */
273
    public function testAskAndValidate(): void
274
    {
275
        $input          = $this->getInputMock();
276
        $output         = $this->getOutputMock();
277
        $helper         = $this->getHelperSetMock();
278
        $questionHelper = $this->getQuestionHelper();
279
280
        $helper->expects($this->once())->method('get')->willReturn($questionHelper);
281
        $questionHelper->expects($this->once())->method('ask')->willReturn('y');
282
283
        $io     = new DefaultIO($input, $output, $helper);
284
        $answer = $io->askAndValidate(
285
            'foo',
286
            function () {
287
                return true;
288
            }
289
        );
290
        $this->assertEquals('y', $answer);
291
    }
292
293
    /**
294
     * Tests DefaultIO::write
295
     */
296
    public function testWrite(): void
297
    {
298
        $input          = $this->getInputMock();
299
        $output         = $this->getConsoleOutputMock();
300
        $helper         = $this->getHelperSetMock();
301
302
        $output->expects($this->once())->method('getVerbosity')->willReturn(OutputInterface::VERBOSITY_DEBUG);
303
        $output->expects($this->once())->method('getErrorOutput')->willReturn($this->getOutputMock());
304
305
        $io = new DefaultIO($input, $output, $helper);
306
        $io->writeError('foo');
307
    }
308
309
310
    /**
311
     * Tests DefaultIO::write
312
     */
313
    public function testWriteSkipped(): void
314
    {
315
        $input          = $this->getInputMock();
316
        $output         = $this->getConsoleOutputMock();
317
        $helper         = $this->getHelperSetMock();
318
319
        $output->expects($this->once())->method('getVerbosity')->willReturn(OutputInterface::VERBOSITY_NORMAL);
320
        $output->expects($this->exactly(0))->method('getErrorOutput');
321
322
        $io = new DefaultIO($input, $output, $helper);
323
        $io->writeError('foo', false, IO::DEBUG);
324
    }
325
}
326