2010-11-28 17:54:32 +00:00
|
|
|
<?php
|
2010-11-29 02:11:18 +00:00
|
|
|
/**
|
2010-12-04 17:15:47 +00:00
|
|
|
* IniReaderTest
|
2010-11-29 02:11:18 +00:00
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2012-03-13 02:46:07 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-11-29 02:11:18 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2012-03-13 02:46:07 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Configure
|
2010-11-29 02:11:18 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2010-12-09 05:13:11 +00:00
|
|
|
App::uses('IniReader', 'Configure');
|
2010-11-28 17:54:32 +00:00
|
|
|
|
2010-12-04 17:15:47 +00:00
|
|
|
class IniReaderTest extends CakeTestCase {
|
2010-11-28 17:54:32 +00:00
|
|
|
|
2010-11-29 00:58:59 +00:00
|
|
|
/**
|
2012-05-02 01:43:01 +00:00
|
|
|
* Test data to serialize and unserialize.
|
2010-11-29 00:58:59 +00:00
|
|
|
*
|
2012-05-02 01:43:01 +00:00
|
|
|
* @var array
|
2010-11-29 00:58:59 +00:00
|
|
|
*/
|
2012-05-02 01:43:01 +00:00
|
|
|
public $testData = array(
|
|
|
|
'One' => array(
|
|
|
|
'two' => 'value',
|
|
|
|
'three' => array(
|
|
|
|
'four' => 'value four'
|
|
|
|
),
|
|
|
|
'is_null' => null,
|
|
|
|
'bool_false' => false,
|
|
|
|
'bool_true' => true,
|
|
|
|
),
|
|
|
|
'Asset' => array(
|
|
|
|
'timestamp' => 'force'
|
|
|
|
),
|
|
|
|
);
|
2010-11-28 17:54:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setup
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-12-04 21:27:51 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2012-03-11 03:30:56 +00:00
|
|
|
$this->path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
|
2010-11-28 17:54:32 +00:00
|
|
|
}
|
2010-11-29 00:58:59 +00:00
|
|
|
|
2010-11-28 17:54:32 +00:00
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* test construct
|
2010-11-28 17:54:32 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConstruct() {
|
2010-12-04 17:15:47 +00:00
|
|
|
$reader = new IniReader($this->path);
|
|
|
|
$config = $reader->read('acl.ini.php');
|
2010-11-29 00:58:59 +00:00
|
|
|
|
|
|
|
$this->assertTrue(isset($config['admin']));
|
|
|
|
$this->assertTrue(isset($config['paul']['groups']));
|
|
|
|
$this->assertEquals('ads', $config['admin']['deny']);
|
2010-11-28 17:54:32 +00:00
|
|
|
}
|
2010-11-29 01:27:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* no other sections should exist.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadingOnlyOneSection() {
|
2010-12-04 17:15:47 +00:00
|
|
|
$reader = new IniReader($this->path, 'admin');
|
|
|
|
$config = $reader->read('acl.ini.php');
|
2010-11-29 01:27:45 +00:00
|
|
|
|
|
|
|
$this->assertTrue(isset($config['groups']));
|
|
|
|
$this->assertEquals('administrators', $config['groups']);
|
|
|
|
}
|
|
|
|
|
2011-01-23 21:26:13 +00:00
|
|
|
/**
|
|
|
|
* test without section
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReadingWithoutSection() {
|
|
|
|
$reader = new IniReader($this->path);
|
|
|
|
$config = $reader->read('no_section.ini');
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'some_key' => 'some_value',
|
|
|
|
'bool_key' => true
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $config);
|
2011-01-23 21:26:13 +00:00
|
|
|
}
|
|
|
|
|
2010-12-04 17:56:37 +00:00
|
|
|
/**
|
|
|
|
* test that names with .'s get exploded into arrays.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadingValuesWithDots() {
|
2010-12-04 17:56:37 +00:00
|
|
|
$reader = new IniReader($this->path);
|
|
|
|
$config = $reader->read('nested.ini');
|
|
|
|
|
|
|
|
$this->assertTrue(isset($config['database']['db']['username']));
|
|
|
|
$this->assertEquals('mark', $config['database']['db']['username']);
|
|
|
|
$this->assertEquals(3, $config['nesting']['one']['two']['three']);
|
2012-05-02 01:43:01 +00:00
|
|
|
$this->assertFalse(isset($config['database.db.username']));
|
|
|
|
$this->assertFalse(isset($config['database']['db.username']));
|
2010-12-04 17:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test boolean reading
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBooleanReading() {
|
2010-12-04 17:56:37 +00:00
|
|
|
$reader = new IniReader($this->path);
|
|
|
|
$config = $reader->read('nested.ini');
|
2010-12-04 17:58:02 +00:00
|
|
|
|
2010-12-04 17:56:37 +00:00
|
|
|
$this->assertTrue($config['bools']['test_on']);
|
2010-12-04 17:58:02 +00:00
|
|
|
$this->assertFalse($config['bools']['test_off']);
|
|
|
|
|
2010-12-04 17:56:37 +00:00
|
|
|
$this->assertTrue($config['bools']['test_yes']);
|
|
|
|
$this->assertFalse($config['bools']['test_no']);
|
2010-12-04 17:58:02 +00:00
|
|
|
|
|
|
|
$this->assertTrue($config['bools']['test_true']);
|
|
|
|
$this->assertFalse($config['bools']['test_false']);
|
|
|
|
|
|
|
|
$this->assertFalse($config['bools']['test_null']);
|
2010-12-04 17:56:37 +00:00
|
|
|
}
|
2011-01-23 22:27:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test read file without extension
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReadingWithoutExtension() {
|
|
|
|
$reader = new IniReader($this->path);
|
|
|
|
$config = $reader->read('nested');
|
|
|
|
$this->assertTrue($config['bools']['test_on']);
|
|
|
|
}
|
2012-04-20 01:53:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test dump method.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDump() {
|
2012-05-02 01:43:01 +00:00
|
|
|
$reader = new IniReader(TMP);
|
|
|
|
$result = $reader->dump('test.ini', $this->testData);
|
2012-04-22 00:26:35 +00:00
|
|
|
$this->assertTrue($result > 0);
|
|
|
|
|
2012-04-20 01:53:45 +00:00
|
|
|
$expected = <<<INI
|
|
|
|
[One]
|
|
|
|
two = value
|
|
|
|
three.four = value four
|
2012-05-02 01:43:01 +00:00
|
|
|
is_null = null
|
|
|
|
bool_false = false
|
|
|
|
bool_true = true
|
2012-04-20 01:53:45 +00:00
|
|
|
[Asset]
|
|
|
|
timestamp = force
|
|
|
|
INI;
|
2012-05-02 01:43:01 +00:00
|
|
|
$file = TMP . 'test.ini';
|
2012-04-22 00:26:35 +00:00
|
|
|
$result = file_get_contents($file);
|
|
|
|
unlink($file);
|
|
|
|
|
2012-05-02 01:43:01 +00:00
|
|
|
$this->assertTextEquals($expected, $result);
|
2012-08-15 14:08:58 +00:00
|
|
|
|
|
|
|
$result = $reader->dump('test', $this->testData);
|
|
|
|
$this->assertTrue($result > 0);
|
|
|
|
|
|
|
|
$contents = file_get_contents($file);
|
|
|
|
$this->assertTextEquals($expected, $contents);
|
|
|
|
unlink($file);
|
2012-05-02 01:43:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that dump() makes files read() can read.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDumpRead() {
|
|
|
|
$reader = new IniReader(TMP);
|
|
|
|
$reader->dump('test.ini', $this->testData);
|
|
|
|
$result = $reader->read('test.ini');
|
|
|
|
unlink(TMP . 'test.ini');
|
|
|
|
|
|
|
|
$expected = $this->testData;
|
|
|
|
$expected['One']['is_null'] = false;
|
|
|
|
|
2012-04-20 01:53:45 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2011-04-17 10:35:21 +00:00
|
|
|
}
|