mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
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:
parent
7c4b7a2cbe
commit
e7153b5333
3 changed files with 104 additions and 35 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 = <<<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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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 = <<<PHP
|
||||
<?php
|
||||
|
@ -128,7 +136,9 @@ class PhpReaderTest extends CakeTestCase {
|
|||
array (
|
||||
'four' => '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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue