forked from zendframework/zendframework
/
JsonTest.php
285 lines (246 loc) · 11 KB
/
JsonTest.php
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Config
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Config;
use Zend\Config\Json as JsonConfig;
/**
* @category Zend
* @package Zend_Config
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class JsonTest extends \PHPUnit_Framework_TestCase
{
protected $_iniFileConfig;
protected $_iniFileAllSectionsConfig;
protected $_iniFileCircularConfig;
public function setUp()
{
$this->_iniFileConfig = __DIR__ . '/_files/config.json';
$this->_iniFileAllSectionsConfig = __DIR__ . '/_files/allsections.json';
$this->_iniFileCircularConfig = __DIR__ . '/_files/circular.json';
$this->_iniFileMultipleInheritanceConfig = __DIR__ . '/_files/multipleinheritance.json';
$this->_nonReadableConfig = __DIR__ . '/_files/nonreadable.json';
$this->_iniFileNoSectionsConfig = __DIR__ . '/_files/nosections.json';
$this->_iniFileInvalid = __DIR__ . '/_files/invalid.json';
}
public function testLoadSingleSection()
{
$config = new JsonConfig($this->_iniFileConfig, 'all');
$this->assertEquals('all', $config->hostname);
$this->assertEquals('live', $config->db->name);
$this->assertEquals('multi', $config->one->two->three);
$this->assertNull(@$config->nonexistent); // property doesn't exist
}
public function testSectionInclude()
{
$config = new JsonConfig($this->_iniFileConfig, 'staging');
$this->assertEquals('', $config->debug); // only in staging
$this->assertEquals('thisname', $config->name); // only in all
$this->assertEquals('username', $config->db->user); // only in all (nested version)
$this->assertEquals('dbstaging', $config->db->name); // inherited and overridden
}
public function testTrueValues()
{
$config = new JsonConfig($this->_iniFileConfig, 'debug');
$this->assertTrue($config->debug);
$this->assertTrue($config->values->changed);
}
public function testEmptyValues()
{
$config = new JsonConfig($this->_iniFileConfig, 'debug');
$this->assertInternalType('string', $config->special->no);
$this->assertEquals('no', $config->special->no);
$this->assertNull($config->special->null);
$this->assertFalse($config->special->false);
}
/**
* @group review
*/
public function testMultiDepthExtends()
{
$config = new JsonConfig($this->_iniFileConfig, 'other_staging');
$this->assertEquals('otherStaging', $config->only_in); // only in other_staging
$this->assertEquals('', $config->debug); // 1 level down: only in staging
$this->assertEquals('thisname', $config->name); // 2 levels down: only in all
$this->assertEquals('username', $config->db->user); // 2 levels down: only in all (nested version)
$this->assertEquals('staging', $config->hostname); // inherited from two to one and overridden
$this->assertEquals('dbstaging', $config->db->name); // inherited from two to one and overridden
$this->assertEquals('anotherpwd', $config->db->pass); // inherited from two to other_staging and overridden
}
public function testRaisesExceptionWhenSectionNotFound()
{
$this->setExpectedException('Zend\Config\Exception\RuntimeException', 'cannot be found');
$config = new JsonConfig($this->_iniFileConfig, 'extendserror');
}
public function testRetrievesAndMergesMultipleSections()
{
$config = new JsonConfig($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
$this->assertEquals('otherStaging', $config->only_in);
$this->assertEquals('dbstaging', $config->db->name);
}
public function testCanRetrieveAllSections()
{
$config = new JsonConfig($this->_iniFileAllSectionsConfig, null);
$this->assertEquals('otherStaging', $config->other_staging->only_in);
$this->assertEquals('dbstaging', $config->staging->db->name);
}
public function testAllowsLoadingAllSectionsOrSomeSectionsSelectively()
{
$config = new JsonConfig($this->_iniFileAllSectionsConfig, null);
$this->assertEquals(null, $config->getSectionName());
$this->assertEquals(true, $config->areAllSectionsLoaded());
$config = new JsonConfig($this->_iniFileAllSectionsConfig, 'all');
$this->assertEquals('all', $config->getSectionName());
$this->assertEquals(false, $config->areAllSectionsLoaded());
$config = new JsonConfig($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
$this->assertEquals(array('staging','other_staging'), $config->getSectionName());
$this->assertEquals(false, $config->areAllSectionsLoaded());
}
public function testDetectsCircularInheritance()
{
$this->setExpectedException('Zend\Config\Exception\RuntimeException', 'circular inheritance');
$config = new JsonConfig($this->_iniFileCircularConfig, null);
}
public function testRaisesErrorWhenNoFileProvided()
{
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'not set');
$config = new JsonConfig('','');
}
public function testRaisesErrorOnAttemptsToExtendMultipleSectionsAtOnce()
{
$this->setExpectedException('Zend\Config\Exception\RuntimeException', 'Invalid');
$config = new JsonConfig($this->_iniFileMultipleInheritanceConfig, 'multiinherit');
}
public function testRaisesErrorWhenSectionNotFound()
{
try {
$config = new JsonConfig($this->_iniFileConfig,array('all', 'notthere'));
$this->fail('An expected exception has not been raised');
} catch (\Zend\Config\Exception $expected) {
$this->assertContains('cannot be found', $expected->getMessage());
}
try {
$config = new JsonConfig($this->_iniFileConfig,'notthere');
$this->fail('An expected exception has not been raised');
} catch (\Zend\Config\Exception $expected) {
$this->assertContains('cannot be found', $expected->getMessage());
}
}
public function testCanLoadConfigWithNoSections()
{
$config = new JsonConfig($this->_iniFileNoSectionsConfig);
$this->assertEquals('all', $config->hostname);
$this->assertEquals('two', $config->one->two);
$this->assertEquals('4', $config->one->three->four);
$this->assertEquals('5', $config->one->three->five);
}
public function testRaisesExceptionOnInvalidJsonMarkup()
{
$this->setExpectedException('Zend\Json\Exception', 'Syntax error');
$config = new JsonConfig($this->_iniFileInvalid);
}
public function testOptionsPassedAreHonored()
{
$config = new JsonConfig($this->_iniFileConfig, 'staging', array(
'skipExtends' => true,
'allowModifications' => true,
'bar' => 'foo', // ignored
));
$this->assertNull($config->name); // demonstrates extends were skipped
$config->foo = 'bar';
$this->assertEquals('bar', $config->foo); // demonstrates modifications were made
}
public function testZf2StyleOptionsAreHonored()
{
$config = new JsonConfig($this->_iniFileConfig, 'staging', array(
'skip_extends' => true,
'allow_modifications' => true,
'bar' => 'foo', // ignored
));
$this->assertNull($config->name); // demonstrates extends were skipped
$config->foo = 'bar';
$this->assertEquals('bar', $config->foo); // demonstrates modifications were made
}
public function testAllowsPassingJsonStringsToConstructor()
{
$json =<<<EOJ
{"all":{"foo":"bar"},"staging":{"_extends":"all","bar":"baz"},"debug":{"_extends":"all","debug":true}}
EOJ;
$config = new JsonConfig($json, 'debug');
$this->assertTrue($config->debug);
$this->assertEquals('bar', $config->foo);
$this->assertNull($config->bar);
}
public function testProcessesSectionsWithSingleValues()
{
$json = '{"all":"values"}';
$config = new JsonConfig($json, 'all');
$this->assertEquals('values', $config->all);
}
public function testReplacesConstantNamesWithValuesByDefault()
{
if (!defined('ZEND_CONFIG_JSON_ENV')) {
define('ZEND_CONFIG_JSON_ENV', 'testing');
}
if (!defined('ZEND_CONFIG_JSON_ENV_PATH')) {
define('ZEND_CONFIG_JSON_ENV_PATH', dirname(__FILE__));
}
if (!defined('ZEND_CONFIG_JSON_ENV_INT')) {
define('ZEND_CONFIG_JSON_ENV_INT', 42);
}
$json = '{"env":"ZEND_CONFIG_JSON_ENV","path":"ZEND_CONFIG_JSON_ENV_PATH/tests","int":ZEND_CONFIG_JSON_ENV_INT}';
$config = new JsonConfig($json);
$this->assertEquals(ZEND_CONFIG_JSON_ENV, $config->env);
$this->assertEquals(ZEND_CONFIG_JSON_ENV_PATH . '/tests', $config->path);
$this->assertEquals(ZEND_CONFIG_JSON_ENV_INT, $config->int);
}
public function testCanIgnoreConstantsWhenParsing()
{
if (!defined('ZEND_CONFIG_JSON_ENV')) {
define('ZEND_CONFIG_JSON_ENV', 'testing');
}
$json = '{"env":"ZEND_CONFIG_JSON_ENV"}';
$config = new JsonConfig($json, null, array('ignore_constants' => true));
$this->assertEquals('ZEND_CONFIG_JSON_ENV', $config->env);
}
public function testIgnoringConstantsCanLeadToParseErrors()
{
if (!defined('ZEND_CONFIG_JSON_ENV')) {
define('ZEND_CONFIG_JSON_ENV', 'testing');
}
if (!defined('ZEND_CONFIG_JSON_ENV_PATH')) {
define('ZEND_CONFIG_JSON_ENV_PATH', dirname(__FILE__));
}
if (!defined('ZEND_CONFIG_JSON_ENV_INT')) {
define('ZEND_CONFIG_JSON_ENV_INT', 42);
}
$json = '{"env":"ZEND_CONFIG_JSON_ENV","path":"ZEND_CONFIG_JSON_ENV_PATH/tests","int":ZEND_CONFIG_JSON_ENV_INT}';
$this->setExpectedException('Zend\Json\Exception', 'Syntax');
$config = new JsonConfig($json, null, array('ignore_constants' => true));
}
public function testNestedConfigSetsAreArrays()
{
$config = new JsonConfig(__DIR__ . '/_files/nested.json', 'testing');
$array = $config->toArray();
$this->assertInternalType('array', $array['definitions'][0]);
}
}