1
|
|
|
<?php |
2
|
|
|
namespace Slince\Config\Tests; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Slince\Config\Config; |
6
|
|
|
use Slince\Config\Exception\InvalidFileException; |
7
|
|
|
use Slince\Config\Exception\UnsupportedFormatException; |
8
|
|
|
use Slince\Config\Parser\ParserInterface; |
9
|
|
|
|
10
|
|
|
class ConfigTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testLoadPHPFile() |
13
|
|
|
{ |
14
|
|
|
$config = new Config(); |
15
|
|
|
$config->load(__DIR__ . '/Fixtures/config.php'); |
16
|
|
|
$this->assertEquals('bar', $config->get('foo')); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testLoadIniFile() |
20
|
|
|
{ |
21
|
|
|
$config = new Config(); |
22
|
|
|
$config->load(__DIR__ . '/Fixtures/config.ini'); |
23
|
|
|
$this->assertEquals('baz', $config['foo']['bar']); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testLoadJsonFile() |
27
|
|
|
{ |
28
|
|
|
$config = new Config(); |
29
|
|
|
$config->load(__DIR__ . '/Fixtures/config.json'); |
30
|
|
|
$this->assertEquals('bar', $config->get('foo')); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testLoadXmlFile() |
34
|
|
|
{ |
35
|
|
|
$config = new Config(); |
36
|
|
|
$config->load(__DIR__ . '/Fixtures/config.xml'); |
37
|
|
|
$this->assertEquals('bar', $config['foo']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testLoadYamlFile() |
41
|
|
|
{ |
42
|
|
|
$config = new Config(); |
43
|
|
|
$config->load(__DIR__ . '/Fixtures/config.yml'); |
44
|
|
|
$this->assertEquals('bar', $config->get('foo')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testDumpPHPFile() |
48
|
|
|
{ |
49
|
|
|
$config = new Config(); |
50
|
|
|
$config->merge([ |
51
|
|
|
'foo' => 'bar', |
52
|
|
|
'bar' => [ |
53
|
|
|
'foo', |
54
|
|
|
'bar', |
55
|
|
|
'baz' |
56
|
|
|
] |
57
|
|
|
]); |
58
|
|
|
$targetFile = __DIR__ . '/Tmp/config-dump.php'; |
59
|
|
|
$this->assertTrue($config->dump($targetFile)); |
60
|
|
|
$this->assertEquals($config->toArray(), (new Config($targetFile))->toArray()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testDumpIniFile() |
64
|
|
|
{ |
65
|
|
|
$config = new Config(); |
66
|
|
|
$config->merge([ |
67
|
|
|
'foo' => [ |
68
|
|
|
'bar' => [ |
69
|
|
|
'foo', |
70
|
|
|
'bar', |
71
|
|
|
'baz' |
72
|
|
|
] |
73
|
|
|
] |
74
|
|
|
]); |
75
|
|
|
$targetFile = __DIR__ . '/Tmp/config-dump.ini'; |
76
|
|
|
$this->assertTrue($config->dump($targetFile)); |
77
|
|
|
$this->assertEquals($config->toArray(), (new Config($targetFile))->toArray()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testDumpJsonFile() |
81
|
|
|
{ |
82
|
|
|
$config = new Config(); |
83
|
|
|
$config->merge([ |
84
|
|
|
'foo' => 'bar', |
85
|
|
|
'bar' => [ |
86
|
|
|
'foo', |
87
|
|
|
'bar', |
88
|
|
|
'baz' |
89
|
|
|
] |
90
|
|
|
]); |
91
|
|
|
$targetFile = __DIR__ . '/Tmp/config-dump.json'; |
92
|
|
|
$this->assertTrue($config->dump($targetFile)); |
93
|
|
|
$this->assertEquals($config->toArray(), (new Config($targetFile))->toArray()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testDumpXmlFile() |
97
|
|
|
{ |
98
|
|
|
$config = new Config(); |
99
|
|
|
$config->merge([ |
100
|
|
|
'foo' => 'bar', |
101
|
|
|
'bar' => [ |
102
|
|
|
'foo', |
103
|
|
|
'bar', |
104
|
|
|
'baz' |
105
|
|
|
] |
106
|
|
|
]); |
107
|
|
|
$targetFile = __DIR__ . '/Tmp/config-dump.xml'; |
108
|
|
|
$this->assertTrue($config->dump($targetFile)); |
109
|
|
|
$this->assertEquals($config->toArray(), (new Config($targetFile))->toArray()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testDumpYamlFile() |
113
|
|
|
{ |
114
|
|
|
$config = new Config(); |
115
|
|
|
$config->merge([ |
116
|
|
|
'foo' => 'bar', |
117
|
|
|
'bar' => [ |
118
|
|
|
'foo', |
119
|
|
|
'bar', |
120
|
|
|
'baz' |
121
|
|
|
] |
122
|
|
|
]); |
123
|
|
|
$targetFile = __DIR__ . '/Tmp/config-dump.yaml'; |
124
|
|
|
$this->assertTrue($config->dump($targetFile)); |
125
|
|
|
$this->assertEquals($config->toArray(), (new Config($targetFile))->toArray()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testLoadDirectory() |
129
|
|
|
{ |
130
|
|
|
$config = new Config(__DIR__ . '/Fixtures/config'); |
131
|
|
|
$this->assertCount(3, $config); |
132
|
|
|
$this->assertEquals('bar', $config->get('foo')); |
133
|
|
|
$this->assertEquals('baz', $config->get('bar')); |
134
|
|
|
$this->assertEquals('foo', $config->get('baz')); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testUnsupportedFormat() |
138
|
|
|
{ |
139
|
|
|
$this->setExpectedException(UnsupportedFormatException::class); |
140
|
|
|
new Config(__DIR__ . '/Fixtures/config.ext'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testInvalidFile() |
144
|
|
|
{ |
145
|
|
|
$this->setExpectedException(InvalidFileException::class); |
146
|
|
|
new Config(__DIR__ . '/Fixtures/file-not-exists'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testAddParser() |
150
|
|
|
{ |
151
|
|
|
Config::addParser(new FooParser()); |
152
|
|
|
$config = new Config(); |
153
|
|
|
$config->load(__DIR__ . '/Fixtures/config.ext'); |
154
|
|
|
$this->assertEquals('baz', $config->get('foo')); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
class FooParser implements ParserInterface |
159
|
|
|
{ |
160
|
|
|
public function parse($file) |
161
|
|
|
{ |
162
|
|
|
return [ |
163
|
|
|
'foo' => 'baz' |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function dump($file, array $data) |
168
|
|
|
{ |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public static function getSupportedExtensions() |
172
|
|
|
{ |
173
|
|
|
return ['ext']; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|