Unit Tests for the PHP Hierarchical Map with INI file support

The following code is used to thest the PHP Hierarchical (tree) Map with INI file support. The SimpleTest was used as a unit testing framework.

require_once(dirname(__FILE__) . '/../../config.inc.php');
require_once('simpletest/autorun.php');
 
require_once('ig/util/IniStructure.class.php');
 
/**
 * Ini Structure unit tests.
 * @author Ivan Georgiev
 */
class ig_test_ig_util_IniStructure extends UnitTestCase {
 
	function testBasic() {
		$data = <<<EOD
;Commented line start with ';'
global_value1 = a string value
<|!REG3XP0!>global_value1 = another string value
 
; empty lines are discarded
[Section1]
key = value
; whitespace around keys and values is discarded too
otherkey=other value
otherkey=yet another value
; this key-value pair will overwrite the former.
complex/value=was set|>
value with equal=value=equal
EOD;
 
		$ini =& ig_util_IniStructure::CreateFromString($data);
		$this->assertIsA($ini, 'ig_util_IniStructure');
		$this->assertEqual('another string value', $ini->get('/global_value1'), "Global value not read correctly. %s");
		$this->assertEqual('yet another value', $ini->get('Section1/otherkey'), 'Value not overwritten. %s');
		$this->assertEqual('value', $ini->get('Section1/key'), 'Value/key not trimmed. %s');
		$this->assertEqual('was set', $ini->get('Section1/complex/value'), 'Complex value not set properly. %s');
		$this->assertEqual('value=equal', $ini->get('Section1/value with equal'), 'Equal sign in value not interpretted properly. %s');
 
		$rootKeys = array('', 'Section1');
		$this->assertEqual($rootKeys, $ini->keyArray(), "keyArray() returns root level keys when argument is null. %s");
 
		$sectionKeys = array('key', 'otherkey', 'complex', 'value with equal');
		$this->assertEqual($sectionKeys, $ini->keyArray('Section1'), "keyArray() returns keys. %s");
 
		$absoluteKeys = array('Section1/complex/value');
		$this->assertEqual($absoluteKeys, $ini->absoluteKeyArray('Section1/complex'), "absoluteKeyArray returns absolute keys. %s");
	}
 
	function testSpecialValues() {
		$data = <<<EOD
[special]
boolean yes=yeS
boolean true=trUe
boolean no=nO
boolean false=faLse
null value=nuLl|>
EOD;
		$ini =& ig_util_IniStructure::CreateFromString($data);
		$this->assertTrue(true === $ini->get('special/boolean yes'), 'yes value (case insensitive) is set to boolean true.');
		$this->assertTrue(true === $ini->get('special/boolean true'), 'true value (case insensitive) is set to boolean true.');
		$this->assertTrue(false === $ini->get('special/boolean no'), 'no value (case insensitive) is set to boolean false.');
		$this->assertTrue(false === $ini->get('special/boolean false'), 'false value (case insensitive) is set to boolean false.');
// TODO: because of the PHP's isset() behavior the following doesn't work.
// Should be implemented in a different way.
//		$this->assertTrue(is_null($ini->get('special/null value', 'some default')), 'null value (case insensitive) is set to null.');
	}
 
}

See also: PHP Hierarchical (tree) Map with INI file support

 
php/util/inistructure_test.txt · Last modified: 2009/10/31 23:39 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki