From a55cb58cbaae34de509c7b56d1aea9f95361a119 Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Sat, 23 Apr 2016 14:00:11 +0200 Subject: [PATCH] Add test cases to cover various error cases - syntax error - extra tokens (both ignore and error) - invalid token - undefined constant More exceptions handling test cases --- tests/Mantis/ConfigParserTest.php | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/Mantis/ConfigParserTest.php b/tests/Mantis/ConfigParserTest.php index cd6c06b3cb..27062bedc0 100644 --- a/tests/Mantis/ConfigParserTest.php +++ b/tests/Mantis/ConfigParserTest.php @@ -91,6 +91,52 @@ public function testArrays() { } } + public function testExtraTokensError() { + $this->setExpectedExceptionRegExp('Exception', '/^Extra tokens found/'); + + $t_parser = new ConfigParser( '1; 2' ); + $t_parser->parse( ConfigParser::EXTRA_TOKENS_ERROR ); + + $t_parser = new ConfigParser( 'array(); 2' ); + $t_parser->parse( ConfigParser::EXTRA_TOKENS_ERROR ); + } + + public function testExtraTokensIgnore() { + $t_parser = new ConfigParser( '1; 2' ); + $t_result = $t_parser->parse( ConfigParser::EXTRA_TOKENS_ERROR ); + $this->assertEquals( $t_result, 1 ); + + $t_parser = new ConfigParser( 'array(); 2' ); + $t_result = $t_parser->parse( ConfigParser::EXTRA_TOKENS_IGNORE ); + $this->assertEquals( $t_result, array() ); + } + + public function testSyntaxError() { + $this->setExpectedExceptionRegExp('Exception', '/^Syntax error/'); + + $t_parser = new ConfigParser( 'array(' ); + $t_parser->parse(); + } + + public function testInvalidTokensError() { + $this->setExpectedExceptionRegExp('Exception', '/^Unexpected token/'); + + $t_parser = new ConfigParser( 'echo 1;' ); + $t_parser->parse(); + } + + public function testUnknownConstantError() { + $this->setExpectedExceptionRegExp('Exception', '/^Unknown string literal/'); + + # Make sure we have a string that is not a defined constant + $t_constant = 'UNDEFINED_CONSTANT'; + while( defined($t_constant) ) { + $t_constant .= '_' . rand(0, 9999); + } + $t_parser = new ConfigParser( $t_constant ); + $t_parser->parse(); + } + private function errorMessage( $p_text ) { return "Original input:\n" . ">>>------------------------\n"