Browse files

Tests: improve require_mantis_core() function

New version no longer requires use of eval() to declare globals.

Also fixes a system warning caused by $g_queries_array not being
declared as global in database api.

Backport from master branch 0b53ba9fb21a77358591ad5c5a48e272e733db9f,
added workaround to avoid warnings caused by lack of require_api()
function.
  • Loading branch information...
1 parent 593e34d commit fe2b85a149935c95cd964fac52718c5ad919fc1e @dregad dregad committed May 26, 2013
Showing with 57 additions and 29 deletions.
  1. +1 −1 tests/Mantis/HelperTest.php
  2. +56 −28 tests/TestConfig.php
View
2 tests/Mantis/HelperTest.php
@@ -29,7 +29,7 @@
/**
* MantisBT Core API
*/
-eval( require_mantis_core() );
+require_mantis_core();
/**
View
84 tests/TestConfig.php
@@ -34,49 +34,77 @@
/**
+ * Parse file and retrieve distinct T_VARIABLE tokens with 'g_' prefix
+ * @param string $p_file
+ * @param array $p_var_list
+ * @return bool false if file can't be parsed
+ */
+function parse_config_global_vars( $p_file, &$p_var_list ) {
+ # Parse the file
+ $t_contents = file_get_contents( $p_file, true );
+ if( false === $t_contents ) {
+ return false;
+ }
+ $t_tokens = token_get_all( $t_contents );
+
+ if( !is_array( $p_var_list ) ) {
+ $p_var_list = array();
+ }
+
+ # Store all distinct T_VARIABLE tokens with 'g_' prefix
+ foreach( $t_tokens as $t ) {
+ if( is_array($t) && $t[0] == T_VARIABLE ) {
+ $t_var = ltrim( $t[1], '$' );
+ if( substr( $t_var, 0, 2 ) == 'g_' ) {
+ $p_var_list[$t_var] = $t_var;
+ }
+ }
+ }
+
+ return true;
+}
+
+/**
* Initializes MantisBT core and bypasses the http headers for PHPUnit tests
*
- * When the Mantis Core is needed for Unit Tests, this function's return value
- * should be passed to eval() to define the variables in the global scope and
- * include core.php, like this:
- *
- * eval( require_mantis_core() );
+ * When the Mantis Core is needed for Unit Tests, this function should
+ * be called instead of a standard "require_once( 'core.php' );"
*
* This is required because when running PHPUnit, config_defaults_inc.php is
* not in the global scope, therefore 'global' variables are not properly
* initialized.
- *
- * @return string
*/
function require_mantis_core() {
- # Parse config to retrieve all distinct T_VARIABLE tokens with 'g_' prefix
- $t_tokens = token_get_all(
- file_get_contents( 'config_defaults_inc.php', true )
- );
- $t_var_list = array();
- foreach( $t_tokens as $t ) {
- if( is_array($t) && $t[0] == T_VARIABLE ) {
- $t_var_list[$t[1]] = $t[1];
- }
- }
- $t_var_list = array_filter(
- $t_var_list,
- function($v) { return substr( $v, 0, 3 ) == '$g_'; }
- );
+ parse_config_global_vars( 'config_defaults_inc.php', $t_var_list );
+ parse_config_global_vars( 'config_inc.php', $t_var_list );
- # Add the http headers bypass
- $t_bypass = '$g_bypass_headers';
- $t_var_list[] = $t_bypass;
+ # HTTP headers bypass
+ $t_bypass_headers = 'g_bypass_headers';
+ $t_var_list[] = $t_bypass_headers;
- # Build global declaration
+ # Statically add other globals defined in core
+ # @FIXME This is a hack to avoid system warnings when referencing
+ # global variables declared in individual api modules. This issue
+ # does not exist in 1.3 thanks to new require_api() function.
+ # @TODO it is possible that not globally defining other such variables
+ # here (i.e. before including core.php) might cause issues, but as
+ # there are currently no unit tests worth the name for core api,
+ # we can probably address that if and when such tests are written.
+ $t_var_list[] = 'g_queries_array';
+
+ # Global declaration for all variables
$t_decl = '';
foreach( $t_var_list as $v ) {
+ global $$v;
$t_decl .= "global $v;\n";
}
- $t_decl .= "$t_bypass = true;\n"
- . "require_once( 'core.php' );\n";
- return $t_decl;
+ $$t_bypass_headers = true;
+ require_once( 'core.php' );
+
+ # @TODO check if we need to declare globals for newly defined
+ # variables here, similar to what require_api() does in 1.3
+ # (see also above comment).
}

0 comments on commit fe2b85a

Please sign in to comment.