Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * IniReaderTest
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Configure
15
 * @since         CakePHP(tm) v 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('IniReader', 'Configure');
20
 
21
/**
22
 * Class IniReaderTest
23
 *
24
 * @package       Cake.Test.Case.Configure
25
 */
26
class IniReaderTest extends CakeTestCase {
27
 
28
/**
29
 * Test data to serialize and unserialize.
30
 *
31
 * @var array
32
 */
33
	public $testData = array(
34
		'One' => array(
35
			'two' => 'value',
36
			'three' => array(
37
				'four' => 'value four'
38
			),
39
			'is_null' => null,
40
			'bool_false' => false,
41
			'bool_true' => true,
42
		),
43
		'Asset' => array(
44
			'timestamp' => 'force'
45
		),
46
	);
47
 
48
/**
49
 * setup
50
 *
51
 * @return void
52
 */
53
	public function setUp() {
54
		parent::setUp();
55
		$this->path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
56
	}
57
 
58
/**
59
 * test construct
60
 *
61
 * @return void
62
 */
63
	public function testConstruct() {
64
		$reader = new IniReader($this->path);
65
		$config = $reader->read('acl.ini');
66
 
67
		$this->assertTrue(isset($config['admin']));
68
		$this->assertTrue(isset($config['paul']['groups']));
69
		$this->assertEquals('ads', $config['admin']['deny']);
70
	}
71
 
72
/**
73
 * Test reading files.
74
 *
75
 * @return void
76
 */
77
	public function testRead() {
78
		$reader = new IniReader($this->path);
79
		$config = $reader->read('nested');
80
		$this->assertTrue($config['bools']['test_on']);
81
 
82
		$config = $reader->read('nested.ini');
83
		$this->assertTrue($config['bools']['test_on']);
84
	}
85
 
86
/**
87
 * No other sections should exist.
88
 *
89
 * @return void
90
 */
91
	public function testReadOnlyOneSection() {
92
		$reader = new IniReader($this->path, 'admin');
93
		$config = $reader->read('acl.ini');
94
 
95
		$this->assertTrue(isset($config['groups']));
96
		$this->assertEquals('administrators', $config['groups']);
97
	}
98
 
99
/**
100
 * Test reading acl.ini.php.
101
 *
102
 * @return void
103
 */
104
	public function testReadSpecialAclIniPhp() {
105
		$reader = new IniReader($this->path);
106
		$config = $reader->read('acl.ini.php');
107
 
108
		$this->assertTrue(isset($config['admin']));
109
		$this->assertTrue(isset($config['paul']['groups']));
110
		$this->assertEquals('ads', $config['admin']['deny']);
111
	}
112
 
113
/**
114
 * Test without section.
115
 *
116
 * @return void
117
 */
118
	public function testReadWithoutSection() {
119
		$reader = new IniReader($this->path);
120
		$config = $reader->read('no_section.ini');
121
 
122
		$expected = array(
123
			'some_key' => 'some_value',
124
			'bool_key' => true
125
		);
126
		$this->assertEquals($expected, $config);
127
	}
128
 
129
/**
130
 * Test that names with .'s get exploded into arrays.
131
 *
132
 * @return void
133
 */
134
	public function testReadValuesWithDots() {
135
		$reader = new IniReader($this->path);
136
		$config = $reader->read('nested.ini');
137
 
138
		$this->assertTrue(isset($config['database']['db']['username']));
139
		$this->assertEquals('mark', $config['database']['db']['username']);
140
		$this->assertEquals(3, $config['nesting']['one']['two']['three']);
141
		$this->assertFalse(isset($config['database.db.username']));
142
		$this->assertFalse(isset($config['database']['db.username']));
143
	}
144
 
145
/**
146
 * Test boolean reading.
147
 *
148
 * @return void
149
 */
150
	public function testBooleanReading() {
151
		$reader = new IniReader($this->path);
152
		$config = $reader->read('nested.ini');
153
 
154
		$this->assertTrue($config['bools']['test_on']);
155
		$this->assertFalse($config['bools']['test_off']);
156
 
157
		$this->assertTrue($config['bools']['test_yes']);
158
		$this->assertFalse($config['bools']['test_no']);
159
 
160
		$this->assertTrue($config['bools']['test_true']);
161
		$this->assertFalse($config['bools']['test_false']);
162
 
163
		$this->assertFalse($config['bools']['test_null']);
164
	}
165
 
166
/**
167
 * Test an exception is thrown by reading files that exist without .ini extension.
168
 *
169
 * @expectedException ConfigureException
170
 * @return void
171
 */
172
	public function testReadWithExistentFileWithoutExtension() {
173
		$reader = new IniReader($this->path);
174
		$reader->read('no_ini_extension');
175
	}
176
 
177
/**
178
 * Test an exception is thrown by reading files that don't exist.
179
 *
180
 * @expectedException ConfigureException
181
 * @return void
182
 */
183
	public function testReadWithNonExistentFile() {
184
		$reader = new IniReader($this->path);
185
		$reader->read('fake_values');
186
	}
187
 
188
/**
189
 * Test reading an empty file.
190
 *
191
 * @return void
192
 */
193
	public function testReadEmptyFile() {
194
		$reader = new IniReader($this->path);
195
		$config = $reader->read('empty');
196
		$this->assertEquals(array(), $config);
197
	}
198
 
199
/**
200
 * Test reading keys with ../ doesn't work.
201
 *
202
 * @expectedException ConfigureException
203
 * @return void
204
 */
205
	public function testReadWithDots() {
206
		$reader = new IniReader($this->path);
207
		$reader->read('../empty');
208
	}
209
 
210
/**
211
 * Test reading from plugins.
212
 *
213
 * @return void
214
 */
215
	public function testReadPluginValue() {
216
		App::build(array(
217
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
218
		), App::RESET);
219
		CakePlugin::load('TestPlugin');
220
		$reader = new IniReader($this->path);
221
		$result = $reader->read('TestPlugin.nested');
222
 
223
		$this->assertTrue(isset($result['database']['db']['username']));
224
		$this->assertEquals('bar', $result['database']['db']['username']);
225
		$this->assertFalse(isset($result['database.db.username']));
226
		$this->assertFalse(isset($result['database']['db.username']));
227
 
228
		$result = $reader->read('TestPlugin.nested.ini');
229
		$this->assertEquals('foo', $result['database']['db']['password']);
230
		CakePlugin::unload();
231
	}
232
 
233
/**
234
 * Test reading acl.ini.php from plugins.
235
 *
236
 * @return void
237
 */
238
	public function testReadPluginSpecialAclIniPhpValue() {
239
		App::build(array(
240
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
241
		), App::RESET);
242
		CakePlugin::load('TestPlugin');
243
		$reader = new IniReader($this->path);
244
		$result = $reader->read('TestPlugin.acl.ini.php');
245
 
246
		$this->assertTrue(isset($result['admin']));
247
		$this->assertTrue(isset($result['paul']['groups']));
248
		$this->assertEquals('ads', $result['admin']['deny']);
249
		CakePlugin::unload();
250
	}
251
 
252
/**
253
 * Test dump method.
254
 *
255
 * @return void
256
 */
257
	public function testDump() {
258
		$reader = new IniReader(TMP);
259
		$result = $reader->dump('test.ini', $this->testData);
260
		$this->assertTrue($result > 0);
261
 
262
		$expected = <<<INI
263
[One]
264
two = value
265
three.four = value four
266
is_null = null
267
bool_false = false
268
bool_true = true
269
 
270
[Asset]
271
timestamp = force
272
INI;
273
		$file = TMP . 'test.ini';
274
		$result = file_get_contents($file);
275
		unlink($file);
276
 
277
		$this->assertTextEquals($expected, $result);
278
 
279
		$result = $reader->dump('test', $this->testData);
280
		$this->assertTrue($result > 0);
281
 
282
		$contents = file_get_contents($file);
283
		$this->assertTextEquals($expected, $contents);
284
		unlink($file);
285
	}
286
 
287
/**
288
 * Test that dump() makes files read() can read.
289
 *
290
 * @return void
291
 */
292
	public function testDumpRead() {
293
		$reader = new IniReader(TMP);
294
		$reader->dump('test.ini', $this->testData);
295
		$result = $reader->read('test.ini');
296
		unlink(TMP . 'test.ini');
297
 
298
		$expected = $this->testData;
299
		$expected['One']['is_null'] = false;
300
 
301
		$this->assertEquals($expected, $result);
302
	}
303
 
304
}