These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Redaxscript\Tests\Console\Command; |
||
3 | |||
4 | use org\bovigo\vfs\vfsStream as Stream; |
||
5 | use org\bovigo\vfs\vfsStreamFile as StreamFile; |
||
6 | use org\bovigo\vfs\vfsStreamWrapper as StreamWrapper; |
||
7 | use Redaxscript\Console\Command; |
||
8 | use Redaxscript\Tests\TestCaseAbstract; |
||
9 | |||
10 | /** |
||
11 | * ConfigTest |
||
12 | * |
||
13 | * @since 3.0.0 |
||
14 | * |
||
15 | * @package Redaxscript |
||
16 | * @category Tests |
||
17 | * @author Henry Ruhs |
||
18 | * |
||
19 | * @covers Redaxscript\Console\Command\Config |
||
20 | * @covers Redaxscript\Console\Command\CommandAbstract |
||
21 | */ |
||
22 | |||
23 | class ConfigTest extends TestCaseAbstract |
||
24 | { |
||
25 | /** |
||
26 | * array to restore config |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | |||
31 | protected $_configArray = []; |
||
32 | |||
33 | /** |
||
34 | * setUp |
||
35 | * |
||
36 | * @since 3.0.0 |
||
37 | */ |
||
38 | |||
39 | public function setUp() |
||
40 | { |
||
41 | parent::setUp(); |
||
42 | Stream::setup('root'); |
||
43 | $file = new StreamFile('config.php'); |
||
44 | StreamWrapper::getRoot()->addChild($file); |
||
0 ignored issues
–
show
|
|||
45 | $this->_configArray = $this->_config->get(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * tearDown |
||
50 | * |
||
51 | * @since 3.1.0 |
||
52 | */ |
||
53 | |||
54 | public function tearDown() |
||
55 | { |
||
56 | $this->_config->set('dbType', $this->_configArray['dbType']); |
||
57 | $this->_config->set('dbHost', $this->_configArray['dbHost']); |
||
58 | $this->_config->set('dbPrefix', $this->_configArray['dbPrefix']); |
||
59 | $this->_config->set('dbName', $this->_configArray['dbName']); |
||
60 | $this->_config->set('dbUser', $this->_configArray['dbUser']); |
||
61 | $this->_config->set('dbPassword', $this->_configArray['dbPassword']); |
||
62 | $this->_config->set('env', $this->_configArray['env']); |
||
63 | $this->_request->setServer('argv', null); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * testNoArgument |
||
68 | * |
||
69 | * @since 3.0.0 |
||
70 | */ |
||
71 | |||
72 | public function testNoArgument() |
||
73 | { |
||
74 | /* setup */ |
||
75 | |||
76 | $configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config); |
||
77 | |||
78 | /* expect and actual */ |
||
79 | |||
80 | $expect = $configCommand->getHelp(); |
||
81 | $actual = $configCommand->run('cli'); |
||
82 | |||
83 | /* compare */ |
||
84 | |||
85 | $this->assertEquals($expect, $actual); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * testList |
||
90 | * |
||
91 | * @since 3.0.0 |
||
92 | */ |
||
93 | |||
94 | public function testList() |
||
95 | { |
||
96 | /* setup */ |
||
97 | |||
98 | $this->_config->set('dbPassword', 'test'); |
||
99 | $this->_request->setServer('argv', |
||
100 | [ |
||
101 | 'console.php', |
||
102 | 'config', |
||
103 | 'list' |
||
104 | ]); |
||
105 | $configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config); |
||
106 | |||
107 | /* actual */ |
||
108 | |||
109 | $actual = $configCommand->run('cli'); |
||
110 | |||
111 | /* compare */ |
||
112 | |||
113 | $this->assertString($actual); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * testSet |
||
118 | * |
||
119 | * @since 3.0.0 |
||
120 | */ |
||
121 | |||
122 | public function testSet() |
||
123 | { |
||
124 | /* setup */ |
||
125 | |||
126 | $this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php')); |
||
127 | $this->_request->setServer('argv', |
||
128 | [ |
||
129 | 'console.php', |
||
130 | 'config', |
||
131 | 'set', |
||
132 | '--db-type', |
||
133 | 'sqlite', |
||
134 | '--db-host', |
||
135 | '127.0.0.1' |
||
136 | ]); |
||
137 | $configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config); |
||
138 | |||
139 | /* expect and actual */ |
||
140 | |||
141 | $expect = $configCommand->success(); |
||
142 | $actual = $configCommand->run('cli'); |
||
143 | |||
144 | /* compare */ |
||
145 | |||
146 | $this->assertEquals($expect, $actual); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * testSetInvalid |
||
151 | * |
||
152 | * @since 3.0.0 |
||
153 | */ |
||
154 | |||
155 | public function testSetInvalid() |
||
156 | { |
||
157 | /* setup */ |
||
158 | |||
159 | $this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php')); |
||
160 | $this->_request->setServer('argv', |
||
161 | [ |
||
162 | 'console.php', |
||
163 | 'config', |
||
164 | 'set', |
||
165 | '--no-interaction' |
||
166 | ]); |
||
167 | $configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config); |
||
168 | |||
169 | /* expect and actual */ |
||
170 | |||
171 | $expect = $configCommand->error(); |
||
172 | $actual = $configCommand->run('cli'); |
||
173 | |||
174 | /* compare */ |
||
175 | |||
176 | $this->assertEquals($expect, $actual); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * testParse |
||
181 | * |
||
182 | * @since 3.0.0 |
||
183 | */ |
||
184 | |||
185 | public function testParse() |
||
186 | { |
||
187 | /* setup */ |
||
188 | |||
189 | $this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php')); |
||
190 | $this->_request->setServer('argv', |
||
191 | [ |
||
192 | 'console.php', |
||
193 | 'config', |
||
194 | 'parse', |
||
195 | '--db-url', |
||
196 | 'mysql://root:[email protected]/test' |
||
197 | ]); |
||
198 | $configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config); |
||
199 | |||
200 | /* expect and actual */ |
||
201 | |||
202 | $expect = $configCommand->success(); |
||
203 | $actual = $configCommand->run('cli'); |
||
204 | |||
205 | /* compare */ |
||
206 | |||
207 | $this->assertEquals($expect, $actual); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * testParseEnv |
||
212 | * |
||
213 | * @since 3.0.0 |
||
214 | */ |
||
215 | |||
216 | public function testParseEnv() |
||
217 | { |
||
218 | /* setup */ |
||
219 | |||
220 | $dbUrl = getenv('DB_URL'); |
||
221 | putenv('DB_URL=mysql://root:[email protected]/test'); |
||
222 | $this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php')); |
||
223 | $this->_request->setServer('argv', |
||
224 | [ |
||
225 | 'console.php', |
||
226 | 'config', |
||
227 | 'parse', |
||
228 | '--db-url', |
||
229 | 'DB_URL', |
||
230 | '--db-env' |
||
231 | ]); |
||
232 | $configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config); |
||
233 | |||
234 | /* restore */ |
||
235 | |||
236 | if ($dbUrl) |
||
237 | { |
||
238 | putenv('DB_URL=' . $dbUrl); |
||
239 | } |
||
240 | |||
241 | /* expect and actual */ |
||
242 | |||
243 | $expect = $configCommand->success(); |
||
244 | $actual = $configCommand->run('cli'); |
||
245 | |||
246 | /* compare */ |
||
247 | |||
248 | $this->assertEquals($expect, $actual); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * testParseInvalid |
||
253 | * |
||
254 | * @since 3.0.0 |
||
255 | */ |
||
256 | |||
257 | public function testParseInvalid() |
||
258 | { |
||
259 | /* setup */ |
||
260 | |||
261 | $this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php')); |
||
262 | $this->_request->setServer('argv', |
||
263 | [ |
||
264 | 'console.php', |
||
265 | 'config', |
||
266 | 'parse', |
||
267 | '--no-interaction' |
||
268 | ]); |
||
269 | $configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config); |
||
270 | |||
271 | /* expect and actual */ |
||
272 | |||
273 | $expect = $configCommand->error(); |
||
274 | $actual = $configCommand->run('cli'); |
||
275 | |||
276 | /* compare */ |
||
277 | |||
278 | $this->assertEquals($expect, $actual); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * testLock |
||
283 | * |
||
284 | * @since 3.0.0 |
||
285 | */ |
||
286 | |||
287 | public function testLock() |
||
288 | { |
||
289 | /* setup */ |
||
290 | |||
291 | $this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php')); |
||
292 | $this->_request->setServer('argv', |
||
293 | [ |
||
294 | 'console.php', |
||
295 | 'config', |
||
296 | 'lock' |
||
297 | ]); |
||
298 | $configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config); |
||
299 | |||
300 | /* expect and actual */ |
||
301 | |||
302 | $expect = $configCommand->success(); |
||
303 | $actual = $configCommand->run('cli'); |
||
304 | |||
305 | /* compare */ |
||
306 | |||
307 | $this->assertEquals($expect, $actual); |
||
308 | } |
||
309 | } |
||
310 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: