Skip to content
Browse files

Tests: added function for globals declaration

MantisBT configs (defined in config_defaults_inc.php) are not
defined in the global scope when running PHPUnit. Because of this,
including 'core.php' does not work.

All 'global' configs must therefore be specifically declared as such
when API functions are needed to run tests.
  • Loading branch information...
1 parent acb9021 commit 367fc283a43433f218f05a17ac5887c45eb585e1 @dregad dregad committed May 24, 2013
Showing with 56 additions and 6 deletions.
  1. +4 −3 tests/Mantis/HelperTest.php
  2. +5 −3 tests/Mantis/StringTest.php
  3. +47 −0 tests/TestConfig.php
View
7 tests/Mantis/HelperTest.php
@@ -26,9 +26,10 @@
*/
require_once dirname( dirname(__FILE__) ) . DIRECTORY_SEPARATOR . 'TestConfig.php';
-require_once( 'constant_inc.php' );
-require_once( 'config_defaults_inc.php' );
-require_once( 'helper_api.php' );
+/**
+ * MantisBT Core API
+ */
+eval( require_mantis_core() );
/**
View
8 tests/Mantis/StringTest.php
@@ -26,9 +26,11 @@
*/
require_once dirname( dirname(__FILE__) ) . DIRECTORY_SEPARATOR . 'TestConfig.php';
-require_once( 'constant_inc.php' );
-require_once( 'config_defaults_inc.php' );
-require_once( 'string_api.php' );
+/**
+ * MantisBT Core API
+ */
+eval( require_mantis_core() );
+
/**
* String API tests
View
47 tests/TestConfig.php
@@ -33,6 +33,53 @@
@include_once 'PHPUnit/Framework.php';
+/**
+ * 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() );
+ *
+ * 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_'; }
+ );
+
+ # Add the http headers bypass
+ $t_bypass = '$g_bypass_headers';
+ $t_var_list[] = $t_bypass;
+
+ # Build global declaration
+ $t_decl = '';
+ foreach( $t_var_list as $v ) {
+ $t_decl .= "global $v;\n";
+ }
+ $t_decl .= "$t_bypass = true;\n"
+ . "require_once( 'core.php' );\n";
+
+ return $t_decl;
+}
+
+
# Set error reporting to the level to which Zend Framework code must comply.
error_reporting( E_ALL | E_STRICT );

0 comments on commit 367fc28

Please sign in to comment.
Something went wrong with that request. Please try again.