setOptions( array ( 'useComments' => true ) ); $backend->setOptions( array ( 'useComments' => false ) ); } public function testConfigSettingUseCommentsWrongType() { $backend = new ezcConfigurationIniReader(); try { $backend->setOptions( array ( 'useComments' => 'tests/translations' ) ); $this->fail( 'Expected exception was not thrown' ); } catch ( ezcBaseConfigException $e ) { self::assertEquals( ezcBaseConfigException::VALUE_OUT_OF_RANGE, $e->getCode() ); } } public function testGetOptions() { $backend = new ezcConfigurationIniReader(); $backend->setOptions( array ( 'useComments' => true ) ); $this->assertEquals( array ( 'useComments' => true ), $backend->getOptions() ); } public function testConfigSettingBroken() { $backend = new ezcConfigurationIniReader(); try { $backend->setOptions( array ( 'lOcAtIOn' => 'tests/translations' ) ); $this->fail( 'Expected exception was not thrown' ); } catch ( ezcBaseConfigException $e ) { self::assertEquals( ezcBaseConfigException::UNKNOWN_CONFIG_SETTING, $e->getCode() ); } } public function testInitCtor1() { $backend = new ezcConfigurationIniReader( 'files/basic.ini' ); $this->assertEquals( 'files', $backend->getLocation() ); $this->assertEquals( 'basic', $backend->getName() ); } public function testInitCtor2() { $backend = new ezcConfigurationIniReader( 'files.foo/basic.ini' ); $this->assertEquals( 'files.foo', $backend->getLocation() ); $this->assertEquals( 'basic', $backend->getName() ); } public function testInitCtor3() { try { $backend = new ezcConfigurationIniReader( 'files.foo/basic.f' ); $this->fail( 'Expected exception not thrown' ); } catch ( ezcConfigurationException $e ) { $this->assertEquals( ezcConfigurationException::LOCATION_INVALID, $e->getCode() ); $this->assertEquals( "The path has an invalid suffix (should be <.ini>)", $e->getMessage() ); } } public function testInitCtor4() { $backend = new ezcConfigurationIniReader( 'files' . DIRECTORY_SEPARATOR . 'basic.ini' ); $this->assertEquals( 'files', $backend->getLocation() ); $this->assertEquals( 'basic', $backend->getName() ); } public function testInitCtor5() { $backend = new ezcConfigurationIniReader( 'initctor5.ini' ); $this->assertEquals( '.', $backend->getLocation() ); $this->assertEquals( 'initctor5', $backend->getName() ); } public function testInitMethod1() { $backend = new ezcConfigurationIniReader(); $backend->init( 'files', 'basic' ); $this->assertEquals( 'files', $backend->getLocation() ); $this->assertEquals( 'basic', $backend->getName() ); } public function testConfigExistsCtor() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/basic.ini' ); $this->assertEquals( true, $backend->configExists() ); } public function testConfigExistsInitMethod() { $backend = new ezcConfigurationIniReader(); $backend->init( 'Configuration/trunk/tests/files', 'basic' ); $this->assertEquals( true, $backend->configExists() ); } public function testConfigNotExistsCtor() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/non-existent.ini' ); $this->assertEquals( false, $backend->configExists() ); } public function testConfigNotExistsInitMethod() { $backend = new ezcConfigurationIniReader(); $backend->init( 'Configuration/trunk/tests/files', 'non-existent' ); $this->assertEquals( false, $backend->configExists() ); } public function testUnexistingFile() { try { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/non-existent.ini' ); $backend->load(); $this->fail( 'Expected exception not thrown' ); } catch ( ezcConfigurationFileException $e ) { $this->assertEquals( ezcConfigurationFileException::FILE_NOT_FOUND, $e->getCode() ); } } public function testEmptyFile() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/empty.ini' ); $return = $backend->load(); $this->assertEquals( new ezcConfiguration(), $return ); } public function testOneGroup() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/one-group.ini' ); $return = $backend->load(); $settings = array( 'TheOnlyGroup' => array( 'Setting1' => true, 'SettingNoComment' => 42, 'MultiRow' => false, ) ); $comments = array( 'TheOnlyGroup' => array( '#' => "Just one group", 'Setting1' => " This setting sucks", 'MultiRow' => " Multi\n row\n comment", ) ); $expected = new ezcConfiguration( $settings, $comments ); $this->assertEquals( $expected, $return ); } public function testOneGroupWithGetConfig() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/one-group.ini' ); $backend->load(); $return = $backend->getConfig(); $settings = array( 'TheOnlyGroup' => array( 'Setting1' => true, 'SettingNoComment' => 42, 'MultiRow' => false, ) ); $comments = array( 'TheOnlyGroup' => array( '#' => "Just one group", 'Setting1' => " This setting sucks", 'MultiRow' => " Multi\n row\n comment", ) ); $expected = new ezcConfiguration( $settings, $comments ); $this->assertEquals( $expected, $return ); } public function testTwoGroups() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/two-groups.ini' ); $return = $backend->load(); $settings = array( 'NotTheOnlyGroup' => array( 'Setting1' => true, ), 'TheSecond' => array( 'Setting1' => false, ), ); $comments = array( 'NotTheOnlyGroup' => array( '#' => " Not just one group", ), 'TheSecond' => array( '#' => " The second group", ), ); $expected = new ezcConfiguration( $settings, $comments ); $this->assertEquals( $expected, $return ); } public function testKake() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/kake.ini' ); $return = $backend->load(); $settings = array( 'SettingsBlock' => array( 'Kake' => array( 'mann' => array( 'sjokolade', 'blåbær' ), 'fyll' => array( 'pære', 'øl' ), ), ), ); $comments = array( ); $expected = new ezcConfiguration( $settings, $comments ); $this->assertEquals( $expected, $return ); } public function testFormats() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/formats.ini' ); $return = $backend->load(); $settings = array( 'FormatTest' => array( 'Decimal1' => 42, 'Decimal2' => 0, 'MaxSize' => 400, 'MinSize' => 0, 'Hex1' => 11189196, 'Hex2' => 11189196, 'Hex3' => 11189196, 'Hex4' => 11189196, 'TextColor' => 66302, 'Octal1' => 1, 'Octal2' => 458, 'Permission' => 438, 'Float1' => 0.2, 'Float2' => .8123, 'Float3' => 42., 'Float4' => 314e-2, 'Float5' => 3.141592654e1, 'Price' => 10.4, 'Seed' => 10e5, 'String1' => 'Blah blah blah', 'String2' => 'Derick "Tiger" Rethans', 'String3' => 'Foo \\ Bar', 'String4' => 'Foo \\', ) ); $comments = array( ); $expected = new ezcConfiguration( $settings, $comments ); $this->assertEquals( $expected, $return ); } public function test2D() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/multi-dim.ini' ); $return = $backend->load(); $settings = array( '2D-numbered' => array( 'Decimal' => array( 42, 0 ), 'Mixed' => array( 42, 0.812, false, "Derick \"Tiger\" Rethans" ), ), '2D-associative' => array( 'Decimal' => array( 'a' => 42, 'b' => 0 ), 'Mixed' => array( 'a' => 42, 1 => 0.812, 'b' => false, 2 => "Derick \"Tiger\" Rethans" ), ), ); $comments = array( ); $expected = new ezcConfiguration( $settings, $comments ); $this->assertEquals( $expected, $return ); } public function test3D() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/multi-dim2.ini' ); $return = $backend->load(); $settings = array( '3D' => array( 'Decimal' => array( 42, 0 ), 'Array' => array( 'Decimal' => array( 'a' => 42, 'b' => 0 ), 'Mixed' => array( 'b' => false, 2 => "Derick \"Tiger\" Rethans" ), ), 'Quote' => array( "quo\nted" => 'string' ), ), ); $comments = array( '3D' => array( 'Decimal' => array( " One with a comment", " Second one with a comment" ), 'Array' => array( 'Mixed' => array( 2 => " One with a comment" ), ), 'Quote' => array( "quo\nted" => ' One with a quoted hash key' ), ), ); $expected = new ezcConfiguration( $settings, $comments ); $this->assertEquals( $expected, $return ); } public function testTimestamp() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/empty.ini' ); touch( 'Configuration/trunk/tests/files/empty.ini', 1130859680 ); $ts = $backend->timestamp(); $this->assertEquals( 1130859680, $ts ); } public function testTimestampNonExistingFile() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/non-existent.ini' ); $ts = $backend->timestamp(); $this->assertEquals( false, $ts ); } public function testSimpleErrors() { $backend = new ezcConfigurationIniReader( 'Configuration/trunk/tests/files/simple-errors.ini' ); try { $return = $backend->load(); $this->fail( 'Expected exception was not thrown' ); } catch ( ezcConfigurationException $e ) { $this->assertEquals( "Invalid data: in , line <8>", $e->getMessage() ); $this->assertEquals( ezcConfigurationException::PARSE_ERROR, $e->getCode() ); } } /* Validation with a non-strict validator */ public function testValidationNonStrict() { $path = 'Configuration/trunk/tests/files/simple-errors.ini'; $backend = new ezcConfigurationIniReader( $path ); $return = $backend->validate( false ); $expected = new ezcConfigurationValidationResult( $backend->getLocation(), $backend->getName(), $path ); $expected->isValid = false; $item = new ezcConfigurationValidationItem( ezcConfigurationValidationItem::ERROR, $path, 8, false, "Invalid data: ", "Invalid data: " ); $expected->appendItem( $item ); $this->assertEquals( $expected, $return ); } public function testValidationNonStrictGetResultList() { $path = 'Configuration/trunk/tests/files/simple-errors.ini'; $backend = new ezcConfigurationIniReader( $path ); $return = $backend->validate( false ); $resultList = $return->getResultList(); $expected = array( new ezcConfigurationValidationItem( ezcConfigurationValidationItem::ERROR, $path, 8, false, "Invalid data: ", "Invalid data: " ) ); $this->assertEquals( $expected, $resultList ); } public function testValidationNonStrictGetResultList2() { $path = 'Configuration/trunk/tests/files/more-errors.ini'; $backend = new ezcConfigurationIniReader( $path ); $return = $backend->validate( false ); $resultList = $return->getResultList(); $expected = array( new ezcConfigurationValidationItem( ezcConfigurationValidationItem::ERROR, $path, 2, false, "Group ID has invalid characters", "Group ID has invalid characters" ), new ezcConfigurationValidationItem( ezcConfigurationValidationItem::ERROR, $path, 3, false, "Invalid data: ", "Invalid data: " ), ); $this->assertEquals( $expected, $resultList ); } public function testValidationNonStrictWarningErrorCount1() { $path = 'Configuration/trunk/tests/files/simple-errors.ini'; $backend = new ezcConfigurationIniReader( $path ); $return = $backend->validate( false ); $this->assertEquals( 1, $return->getErrorCount() ); $this->assertEquals( 0, $return->getWarningCount() ); } public function testValidationNonStrictWarningErrorCount2() { $path = 'Configuration/trunk/tests/files/more-errors.ini'; $backend = new ezcConfigurationIniReader( $path ); $return = $backend->validate( false ); $this->assertEquals( 2, $return->getErrorCount() ); $this->assertEquals( 0, $return->getWarningCount() ); } /* Validation with a strict validator */ public function testValidationStrict() { $path = 'Configuration/trunk/tests/files/simple-errors.ini'; $backend = new ezcConfigurationIniReader( $path ); $return = $backend->validate( true ); $expected = new ezcConfigurationValidationResult( $backend->getLocation(), $backend->getName(), $path ); $expected->isValid = false; $item = new ezcConfigurationValidationItem( ezcConfigurationValidationItem::ERROR, $path, 8, false, "Invalid data: ", "Invalid data: " ); $expected->appendItem( $item ); $this->assertEquals( $expected, $return ); } public function testValidationStrictGetResultList() { $path = 'Configuration/trunk/tests/files/simple-errors.ini'; $backend = new ezcConfigurationIniReader( $path ); $return = $backend->validate( true ); $resultList = $return->getResultList(); $expected = array( new ezcConfigurationValidationItem( ezcConfigurationValidationItem::ERROR, $path, 8, false, "Invalid data: ", "Invalid data: " ) ); $this->assertEquals( $expected, $resultList ); } public function testValidationStrictGetResultList2() { $path = 'Configuration/trunk/tests/files/more-errors.ini'; $backend = new ezcConfigurationIniReader( $path ); $return = $backend->validate( true ); $resultList = $return->getResultList(); $expected = array( new ezcConfigurationValidationItem( ezcConfigurationValidationItem::ERROR, $path, 2, false, "Group ID has invalid characters", "Group ID has invalid characters" ), new ezcConfigurationValidationItem( ezcConfigurationValidationItem::ERROR, $path, 3, false, "Invalid data: ", "Invalid data: " ), ); $this->assertEquals( $expected, $resultList ); } public function testValidationStrictWarningErrorCount1() { $path = 'Configuration/trunk/tests/files/simple-errors.ini'; $backend = new ezcConfigurationIniReader( $path ); $return = $backend->validate( true ); $this->assertEquals( 1, $return->getErrorCount() ); $this->assertEquals( 0, $return->getWarningCount() ); } public function testValidationStrictWarningErrorCount2() { $path = 'Configuration/trunk/tests/files/more-errors.ini'; $backend = new ezcConfigurationIniReader( $path ); $return = $backend->validate( true ); $this->assertEquals( 2, $return->getErrorCount() ); $this->assertEquals( 0, $return->getWarningCount() ); } public static function suite() { return new ezcTestSuite( 'ezcConfigurationIniReaderTest' ); } } ?>