-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathConfigServiceProviderTest.php
More file actions
254 lines (216 loc) · 8.8 KB
/
ConfigServiceProviderTest.php
File metadata and controls
254 lines (216 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/*
* This file is part of ConfigServiceProvider.
*
* (c) Igor Wiedler <igor@wiedler.ch>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Pimple\Container;
use Igorw\Silex\ConfigServiceProvider;
/**
* @author Igor Wiedler <igor@wiedler.ch>
* @author Jérôme Macias <jerome.macias@gmail.com>
*/
class ConfigServiceProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideFilenames
*/
public function testRegisterWithoutReplacement($filename)
{
$app = new Container();
$app->register(new ConfigServiceProvider($filename));
$this->assertSame(true, $app['debug']);
$this->assertSame('%data%', $app['data']);
}
/**
* @dataProvider provideFilenames
*/
public function testRegisterWithReplacement($filename)
{
$app = new Container();
$app->register(new ConfigServiceProvider($filename, array(
'data' => 'test-replacement'
)));
$this->assertSame(true, $app['debug']);
$this->assertSame('test-replacement', $app['data']);
}
/**
* @dataProvider provideEmptyFilenames
*/
public function testEmptyConfigs($filename)
{
$readConfigMethod = new \ReflectionMethod('Igorw\Silex\ConfigServiceProvider', 'readConfig');
$readConfigMethod->setAccessible(true);
$this->assertEquals(
array(),
$readConfigMethod->invoke(new ConfigServiceProvider($filename))
);
}
/**
* @dataProvider provideReplacementFilenames
*/
public function testInFileReplacements($filename)
{
$app = new Container();
$app->register(new ConfigServiceProvider($filename));
$this->assertSame('/var/www', $app['%path%']);
$this->assertSame('/var/www/web/images', $app['path.images']);
$this->assertSame('/var/www/upload', $app['path.upload']);
$this->assertSame('http://example.com', $app['%url%']);
$this->assertSame('http://example.com/images', $app['url.images']);
}
/**
* Currently not tested via testMergeConfigs as TOML seems to have problems
* to create 'db.options' keys
*/
public function testTomlMergeConfigs()
{
$app = new Container();
$filenameBase = __DIR__."/Fixtures/config_base.toml";
$filenameExtended = __DIR__."/Fixtures/config_extend.toml";
$app->register(new ConfigServiceProvider($filenameBase));
$app->register(new ConfigServiceProvider($filenameExtended));
$this->assertSame('pdo_mysql', $app['db']['driver']);
$this->assertSame('utf8', $app['db']['charset']);
$this->assertSame('127.0.0.1', $app['db']['host']);
$this->assertSame('mydatabase', $app['db']['dbname']);
$this->assertSame('root', $app['db']['user']);
$this->assertSame('', $app['db']['password']);
$this->assertSame('123', $app['myproject']['param1']);
$this->assertSame('456', $app['myproject']['param2']);
$this->assertSame('456', $app['myproject']['param3']);
$this->assertSame(array(4, 5, 6), $app['myproject']['param4']);
$this->assertSame('456', $app['myproject']['param5']);
$this->assertSame(array(1, 2, 3, 4), $app['keys']['set']);
}
/**
* @dataProvider provideFilenames
*/
public function testConfigWithPrefix($filename)
{
$app = new Container();
$app->register(new ConfigServiceProvider($filename, array(), null, 'prefix'));
$this->assertNotNull($app['prefix']);
$this->assertSame(true, $app['prefix']['debug']);
$this->assertSame('%data%', $app['prefix']['data']);
}
/**
* @dataProvider provideMergeFilenames
*/
public function testMergeConfigsWithPrefix($filenameBase, $filenameExtended)
{
$app = new Container();
$app->register(new ConfigServiceProvider($filenameBase, array(), null, 'prefix'));
$app->register(new ConfigServiceProvider($filenameExtended, array(), null, 'prefix'));
$this->assertNotNull($app['prefix']);
$this->assertSame('pdo_mysql', $app['prefix']['db.options']['driver']);
$this->assertSame(null, $app['prefix']['db.options']['password']);
$this->assertSame('123', $app['prefix']['myproject.test']['param1']);
$this->assertSame('123', $app['prefix']['myproject.test']['param3']['param2A']);
$this->assertSame(array(4, 5, 6), $app['prefix']['myproject.test']['param4']);
$this->assertSame(array(1,2,3,4), $app['prefix']['test.noparent.key']['test']);
}
/**
* @dataProvider provideMergeFilenames
*/
public function testConfigsWithMultiplePrefixes($filenameBase, $filenameExtended)
{
$app = new Container();
$app->register(new ConfigServiceProvider($filenameBase, array(), null, 'base'));
$app->register(new ConfigServiceProvider($filenameExtended, array(), null, 'extended'));
$this->assertSame(null, $app['extended']['db.options']['password']);
$this->assertSame('123', $app['base']['myproject.test']['param1']);
$this->assertSame('123', $app['base']['myproject.test']['param3']['param2A']);
$this->assertSame(array(4, 5, 6), $app['extended']['myproject.test']['param4']);
$this->assertSame(array(1,2,3,4), $app['extended']['test.noparent.key']['test']);
}
/**
* @dataProvider provideMergeFilenames
*/
public function testMergeConfigs($filenameBase, $filenameExtended)
{
$app = new Container();
$app->register(new ConfigServiceProvider($filenameBase));
$app->register(new ConfigServiceProvider($filenameExtended));
$this->assertSame('pdo_mysql', $app['db.options']['driver']);
$this->assertSame('utf8', $app['db.options']['charset']);
$this->assertSame('127.0.0.1', $app['db.options']['host']);
$this->assertSame('mydatabase', $app['db.options']['dbname']);
$this->assertSame('root', $app['db.options']['user']);
$this->assertSame(null, $app['db.options']['password']);
$this->assertSame('123', $app['myproject.test']['param1']);
$this->assertSame('456', $app['myproject.test']['param2']);
$this->assertSame('123', $app['myproject.test']['param3']['param2A']);
$this->assertSame('456', $app['myproject.test']['param3']['param2B']);
$this->assertSame('456', $app['myproject.test']['param3']['param2C']);
$this->assertSame(array(4, 5, 6), $app['myproject.test']['param4']);
$this->assertSame('456', $app['myproject.test']['param5']);
$this->assertSame(array(1,2,3,4), $app['test.noparent.key']['test']);
}
/**
* @test
* @expectedException RuntimeException
* @expectedExceptionMessage Invalid JSON provided "Syntax error" in
*/
public function invalidJsonShouldThrowException()
{
$app = new Container();
$app->register(new ConfigServiceProvider(__DIR__."/Fixtures/broken.json"));
}
/**
* @test
* @expectedException Symfony\Component\Yaml\Exception\ParseException
*/
public function invalidYamlShouldThrowException()
{
$app = new Container();
$app->register(new ConfigServiceProvider(__DIR__."/Fixtures/broken.yml"));
}
/**
* @test
* @expectedException \Exception
*/
public function invalidTomlShouldThrowException()
{
$app = new Container();
$app->register(new ConfigServiceProvider(__DIR__."/Fixtures/broken.toml"));
}
public function provideFilenames()
{
return array(
array(__DIR__."/Fixtures/config.php"),
array(__DIR__."/Fixtures/config.json"),
array(__DIR__."/Fixtures/config.yml"),
array(__DIR__."/Fixtures/config.toml"),
);
}
public function provideReplacementFilenames()
{
return array(
array(__DIR__."/Fixtures/config_replacement.php"),
array(__DIR__."/Fixtures/config_replacement.json"),
array(__DIR__."/Fixtures/config_replacement.yml"),
array(__DIR__."/Fixtures/config_replacement.toml"),
);
}
public function provideEmptyFilenames()
{
return array(
array(__DIR__."/Fixtures/config_empty.php"),
array(__DIR__."/Fixtures/config_empty.json"),
array(__DIR__."/Fixtures/config_empty.yml"),
array(__DIR__."/Fixtures/config_empty.toml"),
);
}
public function provideMergeFilenames()
{
return array(
array(__DIR__."/Fixtures/config_base.php", __DIR__."/Fixtures/config_extend.php"),
array(__DIR__."/Fixtures/config_base.json", __DIR__."/Fixtures/config_extend.json"),
array(__DIR__."/Fixtures/config_base.yml", __DIR__."/Fixtures/config_extend.yml"),
);
}
}