1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bowerphp\Test\Config; |
4
|
|
|
|
5
|
|
|
use Bowerphp\Config\Config; |
6
|
|
|
use Bowerphp\Test\BowerphpTestCase; |
7
|
|
|
use Mockery; |
8
|
|
|
use Mockery\Exception\NoMatchingExpectationException; |
9
|
|
|
|
10
|
|
|
class ConfigTest extends BowerphpTestCase |
11
|
|
|
{ |
12
|
|
|
public function testConstructor() |
13
|
|
|
{ |
14
|
|
|
$json = '{"directory": "app/Resources/bower", "storage": { "packages": "/tmp/bower" }}'; |
15
|
|
|
|
16
|
|
|
$this->filesystem |
17
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(true) |
18
|
|
|
->shouldReceive('read')->with(getcwd() . '/.bowerrc')->andReturn($json) |
19
|
|
|
; |
20
|
|
|
|
21
|
|
|
$config = new Config($this->filesystem); |
22
|
|
|
|
23
|
|
|
$this->assertEquals('/tmp/bower', $config->getCacheDir()); |
24
|
|
|
$this->assertEquals(getcwd() . '/app/Resources/bower', $config->getInstallDir()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testDefaultOptions() |
28
|
|
|
{ |
29
|
|
|
$this->filesystem |
30
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
31
|
|
|
; |
32
|
|
|
|
33
|
|
|
$config = new Config($this->filesystem); |
34
|
|
|
|
35
|
|
|
$this->assertEquals(getcwd() . '/bower_components', $config->getInstallDir()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetHomeDirUnix() |
39
|
|
|
{ |
40
|
|
|
if (defined('PHP_WINDOWS_VERSION_MAJOR')) { |
41
|
|
|
$this->markTestSkipped('Only on Unix/Linux systems'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->filesystem |
45
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
46
|
|
|
; |
47
|
|
|
|
48
|
|
|
$config = new Config($this->filesystem); |
49
|
|
|
|
50
|
|
|
$method = $this->getMethod('Bowerphp\Config\Config', 'getHomeDir'); |
51
|
|
|
$this->assertContains('/', $method->invokeArgs($config, [])); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGetHomeDirWindows() |
55
|
|
|
{ |
56
|
|
|
if (!defined('PHP_WINDOWS_VERSION_MAJOR')) { |
57
|
|
|
$this->markTestSkipped('Only on Windows systems'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->filesystem |
61
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
62
|
|
|
; |
63
|
|
|
|
64
|
|
|
$config = new Config($this->filesystem); |
65
|
|
|
|
66
|
|
|
$method = $this->getMethod('Bowerphp\Config\Config', 'getHomeDir'); |
67
|
|
|
$this->assertContains('AppData', $method->invokeArgs($config, [])); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @expectedException \RuntimeException |
72
|
|
|
* @expectedExceptionMessage Invalid .bowerrc file. |
73
|
|
|
*/ |
74
|
|
|
public function testMalformedJson() |
75
|
|
|
{ |
76
|
|
|
$this->filesystem |
77
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(true) |
78
|
|
|
->shouldReceive('read')->with(getcwd() . '/.bowerrc')->andReturn('{invalid') |
79
|
|
|
; |
80
|
|
|
|
81
|
|
|
$config = new Config($this->filesystem); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGetBowerFileContent() |
85
|
|
|
{ |
86
|
|
|
$json = '{"name": "jquery-ui", "version": "1.10.4", "main": ["ui/jquery-ui.js"], "dependencies": {"jquery": ">=1.6"}}'; |
87
|
|
|
|
88
|
|
|
$this->filesystem |
89
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(true) |
90
|
|
|
->shouldReceive('read')->with(getcwd() . '/.bowerrc')->andReturn($json) |
91
|
|
|
->shouldReceive('exists')->with(getcwd() . '/bower.json')->andReturn(true) |
92
|
|
|
->shouldReceive('read')->with(getcwd() . '/bower.json')->andReturn($json) |
93
|
|
|
; |
94
|
|
|
|
95
|
|
|
$config = new Config($this->filesystem); |
96
|
|
|
|
97
|
|
|
$this->assertEquals(json_decode($json, true), $config->getBowerFileContent()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @expectedException \RuntimeException |
102
|
|
|
* @expectedExceptionMessage Malformed JSON in bower.json: {invalid. |
103
|
|
|
*/ |
104
|
|
|
public function testGetBowerFileContentWithExceptionOnInvalidJson() |
105
|
|
|
{ |
106
|
|
|
$json = '{invalid'; |
107
|
|
|
|
108
|
|
|
$filesystem = Mockery::mock('Bowerphp\Util\Filesystem'); |
109
|
|
|
|
110
|
|
|
$filesystem |
111
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
112
|
|
|
->shouldReceive('exists')->with(getcwd() . '/bower.json')->andReturn(true) |
113
|
|
|
->shouldReceive('read')->with(getcwd() . '/bower.json')->andReturn($json) |
114
|
|
|
; |
115
|
|
|
|
116
|
|
|
$config = new Config($filesystem); |
117
|
|
|
$config->getBowerFileContent(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @expectedException \RuntimeException |
122
|
|
|
* @expectedExceptionMessage No bower.json found. You can run "init" command to create it. |
123
|
|
|
*/ |
124
|
|
|
public function testGetBowerFileContentWithExceptionOnBowerJsonDoesNotExist() |
125
|
|
|
{ |
126
|
|
|
$filesystem = Mockery::mock('Bowerphp\Util\Filesystem'); |
127
|
|
|
|
128
|
|
|
$filesystem |
129
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
130
|
|
|
->shouldReceive('exists')->with(getcwd() . '/bower.json')->andReturn(false) |
131
|
|
|
; |
132
|
|
|
|
133
|
|
|
$config = new Config($filesystem); |
134
|
|
|
$config->getBowerFileContent(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testGetOverrideSection() |
138
|
|
|
{ |
139
|
|
|
$json = '{"name": "jquery-ui", "version": "1.10.4", "main": ["ui/jquery-ui.js"], ' . |
140
|
|
|
'"dependencies": {"jquery": ">=1.6"}, "overrides": {"jquery": {}}}'; |
141
|
|
|
|
142
|
|
|
$this->filesystem |
143
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(true) |
144
|
|
|
->shouldReceive('read')->with(getcwd() . '/.bowerrc')->andReturn($json) |
145
|
|
|
->shouldReceive('exists')->with(getcwd() . '/bower.json')->andReturn(true) |
146
|
|
|
->shouldReceive('read')->with(getcwd() . '/bower.json')->andReturn($json) |
147
|
|
|
; |
148
|
|
|
|
149
|
|
|
$config = new Config($this->filesystem); |
150
|
|
|
|
151
|
|
|
$this->assertEquals(['jquery' => []], $config->getOverridesSection()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testGetOverrideFor() |
155
|
|
|
{ |
156
|
|
|
$json = '{"name": "jquery-ui", "version": "1.10.4", "main": ["ui/jquery-ui.js"], ' . |
157
|
|
|
'"dependencies": {"jquery": ">=1.6"}, "overrides": {"jquery": {"foo": "bar"}}}'; |
158
|
|
|
|
159
|
|
|
$this->filesystem |
160
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(true) |
161
|
|
|
->shouldReceive('read')->with(getcwd() . '/.bowerrc')->andReturn($json) |
162
|
|
|
->shouldReceive('exists')->with(getcwd() . '/bower.json')->andReturn(true) |
163
|
|
|
->shouldReceive('read')->with(getcwd() . '/bower.json')->andReturn($json) |
164
|
|
|
; |
165
|
|
|
|
166
|
|
|
$config = new Config($this->filesystem); |
167
|
|
|
|
168
|
|
|
$this->assertEquals(['foo' => 'bar'], $config->getOverrideFor('jquery')); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testUpdateBowerJsonFile() |
172
|
|
|
{ |
173
|
|
|
$json = '{ |
174
|
|
|
"dependencies": { |
175
|
|
|
"foobar": "*" |
176
|
|
|
} |
177
|
|
|
}'; |
178
|
|
|
|
179
|
|
|
$package = Mockery::mock('Bowerphp\Package\PackageInterface'); |
180
|
|
|
|
181
|
|
|
$package |
|
|
|
|
182
|
|
|
->shouldReceive('getName')->andReturn('foobar') |
183
|
|
|
->shouldReceive('getRequiredVersion')->andReturn('*') |
184
|
|
|
; |
185
|
|
|
|
186
|
|
|
$this->filesystem |
187
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
188
|
|
|
->shouldReceive('exists')->with(getcwd() . '/bower.json')->andReturn(true) |
189
|
|
|
->shouldReceive('read')->with(getcwd() . '/bower.json')->andReturn($json) |
190
|
|
|
->shouldReceive('write')->with(getcwd() . '/bower.json', $json)->andReturn(123) |
191
|
|
|
; |
192
|
|
|
|
193
|
|
|
$config = new Config($this->filesystem); |
194
|
|
|
|
195
|
|
|
$this->assertEquals(0, $config->updateBowerJsonFile($package)); |
196
|
|
|
|
197
|
|
|
$config->setSaveToBowerJsonFile(true); |
198
|
|
|
|
199
|
|
|
$this->assertEquals(123, $config->updateBowerJsonFile($package)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testUpdateBowerJsonFile2() |
203
|
|
|
{ |
204
|
|
|
$json = "{\n \"foo\": 2,\n \"bar\": 3\n}"; |
205
|
|
|
|
206
|
|
|
$this->filesystem |
207
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
208
|
|
|
->shouldReceive('write')->with(getcwd() . '/bower.json', $json)->andReturn(456) |
209
|
|
|
; |
210
|
|
|
|
211
|
|
|
$config = new Config($this->filesystem); |
212
|
|
|
|
213
|
|
|
$this->assertEquals(456, $config->updateBowerJsonFile2(['foo' => 1], ['foo' => 2, 'bar' => 3])); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testGetPackageBowerFileContent() |
217
|
|
|
{ |
218
|
|
|
$package = Mockery::mock('Bowerphp\Package\PackageInterface'); |
219
|
|
|
|
220
|
|
|
$package |
|
|
|
|
221
|
|
|
->shouldReceive('getName')->andReturn('foobar') |
222
|
|
|
; |
223
|
|
|
|
224
|
|
|
$this->filesystem |
225
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
226
|
|
|
->shouldReceive('exists')->with(getcwd() . '/bower_components/foobar/.bower.json')->andReturn(true) |
227
|
|
|
->shouldReceive('read')->with(getcwd() . '/bower_components/foobar/.bower.json')->andReturn('{"name":"foobar","version":"1.2.3"}') |
228
|
|
|
; |
229
|
|
|
|
230
|
|
|
$config = new Config($this->filesystem); |
231
|
|
|
$this->assertEquals(['name' => 'foobar', 'version' => '1.2.3'], $config->getPackageBowerFileContent($package)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @expectedException \RuntimeException |
236
|
|
|
* @expectedExceptionMessage Could not find .bower.json file for package foobar. |
237
|
|
|
*/ |
238
|
|
|
public function testGetPackageBowerFileContentFileNotFound() |
239
|
|
|
{ |
240
|
|
|
$package = Mockery::mock('Bowerphp\Package\PackageInterface'); |
241
|
|
|
|
242
|
|
|
$package |
|
|
|
|
243
|
|
|
->shouldReceive('getName')->andReturn('foobar') |
244
|
|
|
; |
245
|
|
|
|
246
|
|
|
$this->filesystem |
247
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
248
|
|
|
->shouldReceive('exists')->with(getcwd() . '/bower_components/foobar/.bower.json')->andReturn(false) |
249
|
|
|
; |
250
|
|
|
|
251
|
|
|
$config = new Config($this->filesystem); |
252
|
|
|
$config->getPackageBowerFileContent($package); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @expectedException \RuntimeException |
257
|
|
|
* @expectedExceptionMessage Invalid content in .bower.json for package foobar. |
258
|
|
|
*/ |
259
|
|
|
public function testGetPackageBowerFileInvalidContent() |
260
|
|
|
{ |
261
|
|
|
$package = Mockery::mock('Bowerphp\Package\PackageInterface'); |
262
|
|
|
|
263
|
|
|
$package |
|
|
|
|
264
|
|
|
->shouldReceive('getName')->andReturn('foobar') |
265
|
|
|
; |
266
|
|
|
|
267
|
|
|
$this->filesystem |
268
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
269
|
|
|
->shouldReceive('exists')->with(getcwd() . '/bower_components/foobar/.bower.json')->andReturn(true) |
270
|
|
|
->shouldReceive('read')->with(getcwd() . '/bower_components/foobar/.bower.json')->andReturn('{invalid') |
271
|
|
|
; |
272
|
|
|
|
273
|
|
|
$config = new Config($this->filesystem); |
274
|
|
|
$config->getPackageBowerFileContent($package); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function testSetSaveToBowerJsonFile() |
278
|
|
|
{ |
279
|
|
|
$this->filesystem |
280
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
281
|
|
|
; |
282
|
|
|
|
283
|
|
|
$config = new Config($this->filesystem); |
284
|
|
|
|
285
|
|
|
$config->setSaveToBowerJsonFile(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function testInitBowerJsonFile() |
289
|
|
|
{ |
290
|
|
|
$json = '{ |
291
|
|
|
"name": "foo", |
292
|
|
|
"authors": [ |
293
|
|
|
"Beelab <[email protected]>", |
294
|
|
|
"bar" |
295
|
|
|
], |
296
|
|
|
"private": true, |
297
|
|
|
"dependencies": { |
298
|
|
|
|
299
|
|
|
} |
300
|
|
|
}'; |
301
|
|
|
|
302
|
|
|
$this->filesystem |
303
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
304
|
|
|
->shouldReceive('write')->with(getcwd() . '/bower.json', $json)->andReturn(123) |
305
|
|
|
; |
306
|
|
|
|
307
|
|
|
$config = new Config($this->filesystem); |
308
|
|
|
|
309
|
|
|
try { |
310
|
|
|
$this->assertEquals(123, $config->initBowerJsonFile(['name' => 'foo', 'author' => 'bar'])); |
311
|
|
|
} catch (NoMatchingExpectationException $e) { |
312
|
|
|
$this->markTestSkipped('Some json libs (e.g. Debian one) encode strings differently.'); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function testGetBasePackagesUrl() |
317
|
|
|
{ |
318
|
|
|
$this->filesystem |
319
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
320
|
|
|
; |
321
|
|
|
|
322
|
|
|
$config = new Config($this->filesystem); |
323
|
|
|
|
324
|
|
|
$this->assertEquals('http://registry.bower.io/packages/', $config->getBasePackagesUrl()); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function testBowerFileExists() |
328
|
|
|
{ |
329
|
|
|
$this->filesystem |
330
|
|
|
->shouldReceive('exists')->with(getcwd() . '/.bowerrc')->andReturn(false) |
331
|
|
|
->shouldReceive('exists')->with(getcwd() . '/bower.json')->andReturn(false, true) |
332
|
|
|
; |
333
|
|
|
|
334
|
|
|
$config = new Config($this->filesystem); |
335
|
|
|
|
336
|
|
|
$this->assertFalse($config->bowerFileExists()); |
337
|
|
|
$this->assertTrue($config->bowerFileExists()); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: