Add more tests

Fix an issue in IniReader and add tests
to ensure that dumped files can be re-read.
This commit is contained in:
mark_story 2012-05-01 21:43:01 -04:00
parent 7c4b7a2cbe
commit e7153b5333
3 changed files with 104 additions and 35 deletions

View file

@ -128,6 +128,7 @@ class IniReader implements ConfigReaderInterface {
if ($value === '') { if ($value === '') {
$value = false; $value = false;
} }
unset($values[$key]);
if (strpos($key, '.') !== false) { if (strpos($key, '.') !== false) {
$values = Hash::insert($values, $key, $value); $values = Hash::insert($values, $key, $value);
} else { } else {
@ -153,11 +154,31 @@ class IniReader implements ConfigReaderInterface {
if (is_array($value)) { if (is_array($value)) {
$keyValues = Hash::flatten($value, '.'); $keyValues = Hash::flatten($value, '.');
foreach ($keyValues as $k => $v) { foreach ($keyValues as $k => $v) {
$result[] = "$k = " . trim(var_export($v, true), "'"); $result[] = "$k = " . $this->_value($v);
} }
} }
} }
$contents = join("\n", $result); $contents = join("\n", $result);
return file_put_contents($this->_path . $filename, $contents); 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;
}
} }

View file

@ -21,11 +21,24 @@ App::uses('IniReader', 'Configure');
class IniReaderTest extends CakeTestCase { 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 * setup
@ -92,6 +105,8 @@ class IniReaderTest extends CakeTestCase {
$this->assertTrue(isset($config['database']['db']['username'])); $this->assertTrue(isset($config['database']['db']['username']));
$this->assertEquals('mark', $config['database']['db']['username']); $this->assertEquals('mark', $config['database']['db']['username']);
$this->assertEquals(3, $config['nesting']['one']['two']['three']); $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 * @return void
*/ */
public function testDump() { public function testDump() {
$reader = new IniReader($this->path); $reader = new IniReader(TMP);
$data = array( $result = $reader->dump('test.ini', $this->testData);
'One' => array(
'two' => 'value',
'three' => array(
'four' => 'value four'
)
),
'Asset' => array(
'timestamp' => 'force'
),
);
$result = $reader->dump('test.ini', $data);
$this->assertTrue($result > 0); $this->assertTrue($result > 0);
$expected = <<<INI $expected = <<<INI
[One] [One]
two = value two = value
three.four = value four three.four = value four
is_null = null
bool_false = false
bool_true = true
[Asset] [Asset]
timestamp = force timestamp = force
INI; INI;
$file = $this->path . 'test.ini'; $file = TMP . 'test.ini';
$result = file_get_contents($file); $result = file_get_contents($file);
unlink($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); $this->assertEquals($expected, $result);
} }

View file

@ -20,6 +20,26 @@ App::uses('PhpReader', 'Configure');
class PhpReaderTest extends CakeTestCase { 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 * setup
* *
@ -103,20 +123,8 @@ class PhpReaderTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testDump() { public function testDump() {
$reader = new PhpReader($this->path); $reader = new PhpReader(TMP);
$data = array( $result = $reader->dump('test.php', $this->testData);
'One' => array(
'two' => 'value',
'three' => array(
'four' => 'value four'
),
'null' => null
),
'Asset' => array(
'timestamp' => 'force'
),
);
$result = $reader->dump('test.php', $data);
$this->assertTrue($result > 0); $this->assertTrue($result > 0);
$expected = <<<PHP $expected = <<<PHP
<?php <?php
@ -128,7 +136,9 @@ class PhpReaderTest extends CakeTestCase {
array ( array (
'four' => 'value four', 'four' => 'value four',
), ),
'null' => NULL, 'is_null' => NULL,
'bool_false' => false,
'bool_true' => true,
), ),
'Asset' => 'Asset' =>
array ( array (
@ -136,11 +146,25 @@ class PhpReaderTest extends CakeTestCase {
), ),
); );
PHP; PHP;
$file = $this->path . 'test.php'; $file = TMP . 'test.php';
$contents = file_get_contents($file); $contents = file_get_contents($file);
unlink($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);
} }
} }