Skip to content

Commit

Permalink
Add and improve test cases
Browse files Browse the repository at this point in the history
- scalar types
- multidimentional arrays
- use of constants
- whitespace other than space (tab, cr, lf)

Reorder and regroup tests cases, add comments.
  • Loading branch information
dregad committed Apr 30, 2016
1 parent d95a097 commit dc96f5a
Showing 1 changed file with 181 additions and 48 deletions.
229 changes: 181 additions & 48 deletions tests/Mantis/ConfigParserTest.php
Expand Up @@ -31,29 +31,52 @@
# Mantis Core required for class autoloader and constants
require_mantis_core();

use PHPUnit_Framework_Constraint_IsType as PHPUnit_Type;


/**
* Test cases for config API parser
*
* A list of test strings (as entered in adm_config_report page) will be parsed
* by MantisBT core, and the result compared to PHP's native interpretation
* using eval().
*
* @package Tests
* @subpackage ConfigParser
*/
class Mantis_ConfigParserTest extends PHPUnit_Framework_TestCase {

/**
* @var array List of test cases: strings as entered in adm_config_report page.
* These will be parsed by MantisBT core, and the result compared to
* PHP's interpretation using eval().
*/
private $cases = array();

* @var array List of test cases for scalar types
*/
private $cases_scalar = array();

/**
* @var array List of test cases for arrays
*/
private $cases_array = array();

public function __construct() {
parent::__construct();

$this->initTestCases();
$this->initScalarTestCases();
$this->initArrayTestCases();
}

public function testScalarTypes() {
foreach( $this->cases_scalar as $t_string => $t_expected_type ) {
$t_reference_result = eval( 'return ' . $t_string . ';' );

$t_parser = new ConfigParser( $t_string );
$t_parsed_result = $t_parser->parse();

$this->assertInternalType( $t_expected_type, $t_parsed_result );
$this->assertEquals( $t_parsed_result, $t_reference_result );
}
}

public function testParserCorrectSyntax() {
foreach( $this->cases as $t_string ) {
foreach( $this->cases_array as $t_string ) {
$t_eval_result = eval( 'return ' . $t_string . ';' );
$this->checkParserArray( $t_string, $t_eval_result );
}
Expand Down Expand Up @@ -87,87 +110,187 @@ private function checkParserArray( $p_text, $p_expected_array ) {
*
* @param string $p_string
*/
private function addCase( $p_string ) {
$this->cases[] = $p_string;
private function addArrayCase( $p_string ) {
$this->cases_array[] = $p_string;
}

/**
* Adds a new scalar test case to the list
*
* @param string $p_string Value to check
* @param string $p_type Expected type
*/
private function addScalarCase( $p_string, $p_type ) {
$this->cases_scalar[$p_string] = $p_type;
}

/**
* Initialize the Scalar type test cases
*/
private function initScalarTestCases() {
# Integer
$this->addScalarCase( '1', PHPUnit_Type::TYPE_INT );
$this->addScalarCase( " 1\n", PHPUnit_Type::TYPE_INT );

# Float
$this->addScalarCase( '1.1', PHPUnit_Type::TYPE_FLOAT );

# String
$this->addScalarCase( '"1"', PHPUnit_Type::TYPE_STRING );

# Built-in string literals
$this->addScalarCase( 'null', PHPUnit_Type::TYPE_NULL );
$this->addScalarCase( 'false', PHPUnit_Type::TYPE_BOOL );
$this->addScalarCase( 'true', PHPUnit_Type::TYPE_BOOL );

# Constants
$this->addScalarCase( 'VERSION_ALL', PHPUnit_Type::TYPE_NULL ); # null
$this->addScalarCase( 'VERSION_FUTURE', PHPUnit_Type::TYPE_BOOL ); # false
$this->addScalarCase( 'VERSION_RELEASED', PHPUnit_Type::TYPE_BOOL ); # true
$this->addScalarCase( 'OFF', PHPUnit_Type::TYPE_INT ); # 0
$this->addScalarCase( 'DEVELOPER', PHPUnit_Type::TYPE_INT ); # int
$this->addScalarCase( " DEVELOPER\n", PHPUnit_Type::TYPE_INT );
$this->addScalarCase( 'MANTIS_VERSION', PHPUnit_Type::TYPE_STRING ); #string
$this->addScalarCase( " MANTIS_VERSION\n", PHPUnit_Type::TYPE_STRING );
}

/**
* Initialize the test cases list
*/
private function initTestCases() {
/*
* Template to add a new test case
* -------------------------------
private function initArrayTestCases() {
/**
* Template for new test cases
* ---------------------------
# comment
$this->addCase(
$this->addArrayCase(
<<<'EOT'
EOT
);
* -------------------------------
* ---------------------------
*/
$this->addCase( "array( 'a' => 1, 2 )" );


# no whitespace
$this->addCase( "array(1,2,3)" );

# formatted whitespace
$this->addCase( "array ( 1, 2, 3 )" );
/**
* Simple arrays
*/

# arbitrary whitespace
$this->addCase( " array( 1,2 , 3 ) " );
# empty
$this->addArrayCase( "array( )" );

# one element
$this->addCase( "array( 1 )" );
$this->addArrayCase( "array( 1 )" );

# several elements, trailing delimiter
$this->addCase( "array( 1, 2, )" );
$this->addArrayCase( "array( 1, 2, )" );

# empty
$this->addCase( "array( )" );
# formatted whitespace
$this->addArrayCase( "array ( 1, 2, 3 )" );

# no whitespace
$this->addArrayCase( "array(1,2,3)" );

# arbitrary whitespace
$this->addArrayCase( " array(\n1,\t2 , 3 )\r " );

# mixed types, quotes
$this->addCase(
$this->addArrayCase(
<<<'EOT'
array( 1, 'a', "b" )
EOT
);

# nested quotes
$this->addCase(
$this->addArrayCase(
<<<'EOT'
array( '"a""b"""', "'a''b'''" )
EOT
);

/**
* Associative arrays
*/

# associative
$this->addCase( "array( 0 => 'a', 1 => 'b' )" );
$this->addArrayCase( "array( 0 => 'a', 1 => 'b' )" );

# associative, unordered keys
$this->addCase( "array( 5 => 'a', 2 => 'b' )" );
$this->addArrayCase( "array( 5 => 'a', 2 => 'b' )" );

# associative, text keys
$this->addCase( "array( 'i' => 'a', 'j' => 'b' )" );
$this->addArrayCase( "array( 'i' => 'a', 'j' => 'b' )" );

# associative, mixed keys
$this->addCase( "array( 'i' => 'a', 1 => 'b', 'j' => 'c', 7 => 'd' )" );
$this->addArrayCase( "array( 'i' => 'a', 1 => 'b', 'j' => 'c', 7 => 'd' )" );

# mixed associative, omitting some keys
$this->addCase( "array( 'i' => 'a', 1 => 'b', 'c', 'j' => 'd' )" );
$this->addArrayCase( "array( 'i' => 'a', 1 => 'b', 'c', 'j' => 'd' )" );

# mixed associative, overwriting implicit keys
$this->addCase( "array( 0 => 'a0', 1 => 'a1', 'axx', 2 => 'a2' )" );
$this->addArrayCase( "array( 0 => 'a0', 1 => 'a1', 'axx', 2 => 'a2' )" );

$this->addCase(
$this->addArrayCase(
<<<'EOT'
array(
array ( 1, 'a', 3 => 1, 4 => 'b', 'x' => 'y' )
)
EOT
);

/**
* Use of constants
*/

# As array values (e.g. handle_bug_threshold)
$this->addArrayCase( "array( DEVELOPER, MANAGER )" );

# As array keys (e.g. status_enum_workflow)
$this->addArrayCase(
<<<'EOT'
array (
NEW_ => '20:feedback,30:acknowledged',
ACKNOWLEDGED => '40:confirmed',
)
EOT
);

# both (e.g. set_status_threshold)
$this->addArrayCase( 'array( NEW_ => REPORTER )' );

/**
* Multidimensional arrays
*/

# notify_flags sample
$this->addArrayCase(
<<<'EOT'
array(
'updated' => array (
'reporter' => ON,
'handler' => ON,
'monitor' => ON,
'bugnotes' => OFF,
'threshold_min' => DEVELOPER,
'threshold_max' => MANAGER,
),
)
EOT
);

$this->addArrayCase(
<<<'EOT'
array(
1 => array( 1, 2 => array() ),
array( 'a', 'b', array(3, 4, ) ),
'c' => array( 'd', 5 => 'e' ),
)
EOT
);

/**
* Test cases for specific issues reported on the bugtracker
*/

# Test case for issue #0020787
$this->addCase(
$this->addArrayCase(
<<<'EOT'
array (
'additional_info',
Expand All @@ -180,15 +303,8 @@ private function initTestCases() {
EOT
);

# Test case for issue #0020850
$this->addCase(
<<<'EOT'
array ( 0 => '""a"' )
EOT
);

# Test case for issue #0020812
$this->addCase(
$this->addArrayCase(
<<<'EOT'
array (
0 =>
Expand All @@ -201,8 +317,25 @@ private function initTestCases() {
EOT
);

# Test case for issue #0020813
$this->addArrayCase(
<<<'EOT'
array(
0 => "aa'aa",
1 => "bb\"bb"
)
EOT
);

# Test case for issue #0020850
$this->addArrayCase(
<<<'EOT'
array ( 0 => '""a"' )
EOT
);

# Test case for issue #0020851
$this->addCase(
$this->addArrayCase(
<<<'EOT'
array (
'a' => 'x1',
Expand Down

0 comments on commit dc96f5a

Please sign in to comment.