diff --git a/lib/Cake/Configure/IniReader.php b/lib/Cake/Configure/IniReader.php index b571c7004..debd685c6 100644 --- a/lib/Cake/Configure/IniReader.php +++ b/lib/Cake/Configure/IniReader.php @@ -128,6 +128,7 @@ class IniReader implements ConfigReaderInterface { if ($value === '') { $value = false; } + unset($values[$key]); if (strpos($key, '.') !== false) { $values = Hash::insert($values, $key, $value); } else { @@ -153,11 +154,31 @@ class IniReader implements ConfigReaderInterface { if (is_array($value)) { $keyValues = Hash::flatten($value, '.'); foreach ($keyValues as $k => $v) { - $result[] = "$k = " . trim(var_export($v, true), "'"); + $result[] = "$k = " . $this->_value($v); } } } $contents = join("\n", $result); return file_put_contents($this->_path . $filename, $contents); } + +/** + * Converts a value into the ini equivalent + * + * @param mixed $value to export. + * @return string String value for ini file. + */ + protected function _value($val) { + if ($val === null) { + return 'null'; + } + if ($val === true) { + return 'true'; + } + if ($val === false) { + return 'false'; + } + return (string)$val; + } + } diff --git a/lib/Cake/Test/Case/Configure/IniReaderTest.php b/lib/Cake/Test/Case/Configure/IniReaderTest.php index 0caaf92e2..e4b463450 100644 --- a/lib/Cake/Test/Case/Configure/IniReaderTest.php +++ b/lib/Cake/Test/Case/Configure/IniReaderTest.php @@ -21,11 +21,24 @@ App::uses('IniReader', 'Configure'); class IniReaderTest extends CakeTestCase { /** - * The test file that will be read. + * Test data to serialize and unserialize. * - * @var string + * @var array */ - public $file; + 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' + ), + ); /** * setup @@ -92,6 +105,8 @@ class IniReaderTest extends CakeTestCase { $this->assertTrue(isset($config['database']['db']['username'])); $this->assertEquals('mark', $config['database']['db']['username']); $this->assertEquals(3, $config['nesting']['one']['two']['three']); + $this->assertFalse(isset($config['database.db.username'])); + $this->assertFalse(isset($config['database']['db.username'])); } /** @@ -132,32 +147,41 @@ class IniReaderTest extends CakeTestCase { * @return void */ public function testDump() { - $reader = new IniReader($this->path); - $data = array( - 'One' => array( - 'two' => 'value', - 'three' => array( - 'four' => 'value four' - ) - ), - 'Asset' => array( - 'timestamp' => 'force' - ), - ); - $result = $reader->dump('test.ini', $data); + $reader = new IniReader(TMP); + $result = $reader->dump('test.ini', $this->testData); $this->assertTrue($result > 0); $expected = <<path . 'test.ini'; + $file = TMP . 'test.ini'; $result = file_get_contents($file); unlink($file); + $this->assertTextEquals($expected, $result); + } + +/** + * 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; + $this->assertEquals($expected, $result); } diff --git a/lib/Cake/Test/Case/Configure/PhpReaderTest.php b/lib/Cake/Test/Case/Configure/PhpReaderTest.php index 70a768501..3967db355 100644 --- a/lib/Cake/Test/Case/Configure/PhpReaderTest.php +++ b/lib/Cake/Test/Case/Configure/PhpReaderTest.php @@ -20,6 +20,26 @@ App::uses('PhpReader', 'Configure'); class PhpReaderTest extends CakeTestCase { +/** + * Test data to serialize and unserialize. + * + * @var array + */ + 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' + ), + ); + /** * setup * @@ -103,20 +123,8 @@ class PhpReaderTest extends CakeTestCase { * @return void */ public function testDump() { - $reader = new PhpReader($this->path); - $data = array( - 'One' => array( - 'two' => 'value', - 'three' => array( - 'four' => 'value four' - ), - 'null' => null - ), - 'Asset' => array( - 'timestamp' => 'force' - ), - ); - $result = $reader->dump('test.php', $data); + $reader = new PhpReader(TMP); + $result = $reader->dump('test.php', $this->testData); $this->assertTrue($result > 0); $expected = << 'value four', ), - 'null' => NULL, + 'is_null' => NULL, + 'bool_false' => false, + 'bool_true' => true, ), 'Asset' => array ( @@ -136,11 +146,25 @@ class PhpReaderTest extends CakeTestCase { ), ); PHP; - $file = $this->path . 'test.php'; + $file = TMP . 'test.php'; $contents = file_get_contents($file); unlink($file); - $this->assertEquals($expected, $contents); + $this->assertTextEquals($expected, $contents); + } + +/** + * Test that dump() makes files read() can read. + * + * @return void + */ + public function testDumpRead() { + $reader = new PhpReader(TMP); + $reader->dump('test.php', $this->testData); + $result = $reader->read('test.php'); + unlink(TMP . 'test.php'); + + $this->assertTextEquals($this->testData, $result); } }