Permalink
Browse files

Add more tests

Fix an issue in IniReader and add tests
to ensure that dumped files can be re-read.
  • Loading branch information...
1 parent 7c4b7a2 commit e7153b533392548a9b3ee90719dc2f88827b0e07 @markstory markstory committed May 2, 2012
@@ -128,6 +128,7 @@ protected function _parseNestedValues($values) {
if ($value === '') {
$value = false;
}
+ unset($values[$key]);
if (strpos($key, '.') !== false) {
$values = Hash::insert($values, $key, $value);
} else {
@@ -153,11 +154,31 @@ public function dump($filename, $data) {
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;
+ }
+
}
@@ -21,11 +21,24 @@
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 @@ public function testReadingValuesWithDots() {
$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 @@ public function testReadingWithoutExtension() {
* @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 = <<<INI
[One]
two = value
three.four = value four
+is_null = null
+bool_false = false
+bool_true = true
[Asset]
timestamp = force
INI;
- $file = $this->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);
}
@@ -21,6 +21,26 @@
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
*
* @return void
@@ -103,20 +123,8 @@ public function testReadPluginValue() {
* @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 = <<<PHP
<?php
@@ -128,19 +136,35 @@ public function testDump() {
array (
'four' => 'value four',
),
- 'null' => NULL,
+ 'is_null' => NULL,
+ 'bool_false' => false,
+ 'bool_true' => true,
),
'Asset' =>
array (
'timestamp' => 'force',
),
);
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);
}
}

0 comments on commit e7153b5

Please sign in to comment.