1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of CaptainHook. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace CaptainHook\App\Console\IO; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class DefaultIOTest extends \PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @return \Symfony\Component\Console\Input\InputInterface |
18
|
|
|
*/ |
19
|
|
|
public function getInputMock() |
20
|
|
|
{ |
21
|
|
|
return $this->getMockBuilder('\\Symfony\\Component\\Console\\Input\\InputInterface') |
22
|
|
|
->disableOriginalConstructor() |
23
|
|
|
->getMock(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return \Symfony\Component\Console\Output\ConsoleOutputInterface |
28
|
|
|
*/ |
29
|
|
|
public function getConsoleOutputMock() |
30
|
|
|
{ |
31
|
|
|
return $this->getMockBuilder('\\Symfony\\Component\\Console\\Output\\ConsoleOutputInterface') |
32
|
|
|
->disableOriginalConstructor() |
33
|
|
|
->getMock(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return \Symfony\Component\Console\Output\OutputInterface |
38
|
|
|
*/ |
39
|
|
|
public function getOutputMock() |
40
|
|
|
{ |
41
|
|
|
return $this->getMockBuilder('\\Symfony\\Component\\Console\\Output\\OutputInterface') |
42
|
|
|
->disableOriginalConstructor() |
43
|
|
|
->getMock(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return \Symfony\Component\Console\Helper\HelperSet |
48
|
|
|
*/ |
49
|
|
|
public function getHelperSetMock() |
50
|
|
|
{ |
51
|
|
|
return $this->getMockBuilder('\\Symfony\\Component\\Console\\Helper\\HelperSet') |
52
|
|
|
->disableOriginalConstructor() |
53
|
|
|
->getMock(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return \Symfony\Component\Console\Helper\QuestionHelper |
58
|
|
|
*/ |
59
|
|
|
public function getQuestionHelper() |
60
|
|
|
{ |
61
|
|
|
return $this->getMockBuilder('\\Symfony\\Component\\Console\\Helper\\QuestionHelper') |
62
|
|
|
->disableOriginalConstructor() |
63
|
|
|
->getMock(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Tests DefaultIO::isInteractive |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function testIsInteractive() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$input = $this->getInputMock(); |
72
|
|
|
$output = $this->getOutputMock(); |
73
|
|
|
$helper = $this->getHelperSetMock(); |
74
|
|
|
|
75
|
|
|
$input->expects($this->once())->method('isInteractive')->willReturn(false); |
|
|
|
|
76
|
|
|
$io = new DefaultIO($input, $output, $helper); |
77
|
|
|
|
78
|
|
|
$this->assertFalse($io->isInteractive()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Tests DefaultIO::isVerbose |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function testIsVerbose() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$input = $this->getInputMock(); |
87
|
|
|
$output = $this->getOutputMock(); |
88
|
|
|
$helper = $this->getHelperSetMock(); |
89
|
|
|
|
90
|
|
|
$output->expects($this->once())->method('getVerbosity')->willReturn(0); |
|
|
|
|
91
|
|
|
$io = new DefaultIO($input, $output, $helper); |
92
|
|
|
|
93
|
|
|
$this->assertFalse($io->isVerbose()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Tests DefaultIO::isVeryVerbose |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function testIsVeryVerbose() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$input = $this->getInputMock(); |
102
|
|
|
$output = $this->getOutputMock(); |
103
|
|
|
$helper = $this->getHelperSetMock(); |
104
|
|
|
|
105
|
|
|
$output->expects($this->once())->method('getVerbosity')->willReturn(0); |
|
|
|
|
106
|
|
|
$io = new DefaultIO($input, $output, $helper); |
107
|
|
|
|
108
|
|
|
$this->assertFalse($io->isVeryVerbose()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Tests DefaultIO::isDebug |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function testIsDebug() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$input = $this->getInputMock(); |
117
|
|
|
$output = $this->getOutputMock(); |
118
|
|
|
$helper = $this->getHelperSetMock(); |
119
|
|
|
|
120
|
|
|
$output->expects($this->once())->method('getVerbosity')->willReturn(0); |
|
|
|
|
121
|
|
|
$io = new DefaultIO($input, $output, $helper); |
122
|
|
|
|
123
|
|
|
$this->assertFalse($io->isDebug()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Tests DefaultIO::writeError |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
public function testWriteError() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$input = $this->getInputMock(); |
132
|
|
|
$output = $this->getOutputMock(); |
133
|
|
|
$helper = $this->getHelperSetMock(); |
134
|
|
|
|
135
|
|
|
$output->expects($this->once())->method('getVerbosity')->willReturn(OutputInterface::VERBOSITY_DEBUG); |
|
|
|
|
136
|
|
|
$io = new DefaultIO($input, $output, $helper); |
137
|
|
|
|
138
|
|
|
$io->writeError('foo'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Tests DefaultIO::ask |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function testAsk() |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$input = $this->getInputMock(); |
147
|
|
|
$output = $this->getOutputMock(); |
148
|
|
|
$helper = $this->getHelperSetMock(); |
149
|
|
|
$questionHelper = $this->getQuestionHelper(); |
150
|
|
|
|
151
|
|
|
$helper->expects($this->once())->method('get')->willReturn($questionHelper); |
|
|
|
|
152
|
|
|
$questionHelper->expects($this->once())->method('ask')->willReturn(true); |
|
|
|
|
153
|
|
|
|
154
|
|
|
$io = new DefaultIO($input, $output, $helper); |
155
|
|
|
$answer = $io->ask('foo'); |
156
|
|
|
$this->assertTrue($answer); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Tests DefaultIO::askConfirmation |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function testAskConfirmation() |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$input = $this->getInputMock(); |
165
|
|
|
$output = $this->getOutputMock(); |
166
|
|
|
$helper = $this->getHelperSetMock(); |
167
|
|
|
$questionHelper = $this->getQuestionHelper(); |
168
|
|
|
|
169
|
|
|
$helper->expects($this->once())->method('get')->willReturn($questionHelper); |
|
|
|
|
170
|
|
|
$questionHelper->expects($this->once())->method('ask')->willReturn(true); |
|
|
|
|
171
|
|
|
|
172
|
|
|
$io = new DefaultIO($input, $output, $helper); |
173
|
|
|
$answer = $io->askConfirmation('foo'); |
174
|
|
|
$this->assertTrue($answer); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Tests DefaultIO::askAbdValidate |
179
|
|
|
*/ |
180
|
|
View Code Duplication |
public function testAskAndValidate() |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
$input = $this->getInputMock(); |
183
|
|
|
$output = $this->getOutputMock(); |
184
|
|
|
$helper = $this->getHelperSetMock(); |
185
|
|
|
$questionHelper = $this->getQuestionHelper(); |
186
|
|
|
|
187
|
|
|
$helper->expects($this->once())->method('get')->willReturn($questionHelper); |
|
|
|
|
188
|
|
|
$questionHelper->expects($this->once())->method('ask')->willReturn(true); |
|
|
|
|
189
|
|
|
|
190
|
|
|
$io = new DefaultIO($input, $output, $helper); |
191
|
|
|
$answer = $io->askAndValidate('foo', function() { return true; }); |
192
|
|
|
$this->assertTrue($answer); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Tests DefaultIO::write |
197
|
|
|
*/ |
198
|
|
|
public function testWrite() |
199
|
|
|
{ |
200
|
|
|
$input = $this->getInputMock(); |
201
|
|
|
$output = $this->getConsoleOutputMock(); |
202
|
|
|
$helper = $this->getHelperSetMock(); |
203
|
|
|
|
204
|
|
|
$output->expects($this->once())->method('getVerbosity')->willReturn(OutputInterface::VERBOSITY_DEBUG); |
|
|
|
|
205
|
|
|
$output->expects($this->once())->method('getErrorOutput')->willReturn($this->getOutputMock()); |
|
|
|
|
206
|
|
|
|
207
|
|
|
$io = new DefaultIO($input, $output, $helper); |
208
|
|
|
$io->writeError('foo'); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.