Closed lingfengchencn closed 8 years ago
Because ci-phpunit-test does not set global variable $RTR
.
How about changing your code like this?
$RTR =& load_class('Router');
[Edited]
In this file "_ci_phpunit_test/replaceing/core/CodeIgniter" , I see
$RTR =& load_class('Router', 'core', isset($routing) ? $routing : NULL);
and we require it in CIPHPUnitTest.php , the same code in "index.php".
if can't use global , i need to change all of them : :(
$BM $CFG $EXT $OUT $RTR $UNI $URI
Okay, how about this?
--- a/application/tests/_ci_phpunit_test/CIPHPUnitTest.php
+++ b/application/tests/_ci_phpunit_test/CIPHPUnitTest.php
@@ -66,6 +66,20 @@ class CIPHPUnitTest
* And away we go...
*/
require __DIR__ . '/replacing/core/CodeIgniter.php';
+
+ // Set global variables
+ CIPHPUnitTestSuperGlobal::set_Global('BM', $BM);
+ CIPHPUnitTestSuperGlobal::set_Global('EXT', $EXT);
+ CIPHPUnitTestSuperGlobal::set_Global('CFG', $CFG);
+ CIPHPUnitTestSuperGlobal::set_Global('UNI', $UNI);
+ CIPHPUnitTestSuperGlobal::set_Global('URI', $URI);
+ CIPHPUnitTestSuperGlobal::set_Global('RTR', $RTR);
+ CIPHPUnitTestSuperGlobal::set_Global('OUT', $OUT);
+ CIPHPUnitTestSuperGlobal::set_Global('SEC', $SEC);
+ CIPHPUnitTestSuperGlobal::set_Global('IN', $IN);
+ CIPHPUnitTestSuperGlobal::set_Global('LANG', $LANG);
+
+ // Create CodeIgniter instance
new CI_Controller();
// This code is here, not to cause errors with HMVC
diff --git a/application/tests/_ci_phpunit_test/CIPHPUnitTestSuperGlobal.php b/application/tests/_ci_phpunit_test/CIPHPUnitTestSuperGlobal.php
index e582752..2cd36b3 100644
--- a/application/tests/_ci_phpunit_test/CIPHPUnitTestSuperGlobal.php
+++ b/application/tests/_ci_phpunit_test/CIPHPUnitTestSuperGlobal.php
@@ -10,6 +10,11 @@
class CIPHPUnitTestSuperGlobal
{
+ public static function set_Global($name, $value)
+ {
+ $GLOBALS[$name] = $value;
+ }
+
public function set_POST($params)
{
if (is_array($params))
diff --git a/application/tests/_ci_phpunit_test/functions.php b/application/tests/_ci_phpunit_test/functions.php
index b353b2f..6f0cbbb 100644
--- a/application/tests/_ci_phpunit_test/functions.php
+++ b/application/tests/_ci_phpunit_test/functions.php
@@ -49,17 +49,29 @@ function reset_instance()
}
// Load core classes
- load_class('Benchmark', 'core');
- load_class('Hooks', 'core');
- load_class('Config', 'core');
-// load_class('Utf8', 'core');
- load_class('URI', 'core');
- load_class('Router', 'core');
- load_class('Output', 'core');
- load_class('Security', 'core');
- load_class('Input', 'core');
- load_class('Lang', 'core');
-
+ $BM =& load_class('Benchmark', 'core');
+ $EXT =& load_class('Hooks', 'core');
+ $CFG =& load_class('Config', 'core');
+// $URI =& load_class('Utf8', 'core');
+ $UNI =& load_class('URI', 'core');
+ $RTR =& load_class('Router', 'core');
+ $OUT =& load_class('Output', 'core');
+ $SEC =& load_class('Security', 'core');
+ $IN =& load_class('Input', 'core');
+ $LANG =& load_class('Lang', 'core');
+
+ // Set global variables
+ CIPHPUnitTestSuperGlobal::set_Global('BM', $BM);
+ CIPHPUnitTestSuperGlobal::set_Global('EXT', $EXT);
+ CIPHPUnitTestSuperGlobal::set_Global('CFG', $CFG);
+ CIPHPUnitTestSuperGlobal::set_Global('UNI', $UNI);
+// CIPHPUnitTestSuperGlobal::set_Global('URI', $URI);
+ CIPHPUnitTestSuperGlobal::set_Global('RTR', $RTR);
+ CIPHPUnitTestSuperGlobal::set_Global('OUT', $OUT);
+ CIPHPUnitTestSuperGlobal::set_Global('SEC', $SEC);
+ CIPHPUnitTestSuperGlobal::set_Global('IN', $IN);
+ CIPHPUnitTestSuperGlobal::set_Global('LANG', $LANG);
+
CIPHPUnitTest::loadLoader();
// Remove CodeIgniter instance
I tested, it can work will in Welcome_test/test_index(), it does not work in __construct or _sanitize_globals
here is my test fie and code:
application\ core\MY_Input.php
codes:
class MY_Input extends CI_Input{
public function __construct(){
parent::__construct();
global $CFG;
}
}
Sorry, fixed. Please try #76
yes, it works , thanks . :+1:
global $CI is null
@lingfengchencn Why do you so love global variables? :laughing:
Why don't you use get_instance()
?
--- a/application/tests/_ci_phpunit_test/replacing/core/CodeIgniter.php
+++ b/application/tests/_ci_phpunit_test/replacing/core/CodeIgniter.php
@@ -511,6 +511,7 @@ if ( ! is_php('5.4'))
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start'
$CI = new $class();
+ CIPHPUnitTestSuperGlobal::set_Global('CI', $CI);
/*
* ------------------------------------------------------
HAHAHA, They are using a framework based on CI,so ...
so do "functions.php“ ?
CIPHPUnitTestSuperGlobal::set_Global('CI', $CI);
$BM =& load_class('Benchmark', 'core');
CIPHPUnitTestSuperGlobal::set_Global('BM', $BM);
It seems this is not so simple. The above comment of mine was wrong.
I have to look into it.
@lingfengchencn Where do you use $CI
? Would you give me some sample code and test code?
If we set global $CI
, we get PDOException: You cannot serialize or unserialize PDO instances
error in every test method when we use PDO.
what? we are using pdo.... but I don't get PDOException;
APPPATH \helpers\app_helper.php
if( !function_exists('modifier_i18n') ){
function modifier_i18n($string){
global $CI;
return $CI->lang->line($string);
}
}
The framework named "mlb", I don't know where it come from; README.md
### mlb - README
mlb, my launcher of building, is a PHP project scaffold based on CodeIgniter framework.
I'm trying to delete bussness codes ...
I recommend you modify your application code:
$CI =& get_instance();
If I set global $CI
, PHPUnit always serialize and deserialize it.
See https://phpunit.de/manual/current/en/fixtures.html#fixtures.global-state:
Note The backup and restore operations for global variables and static class attributes use serialize() and unserialize(). Objects of some classes (e.g., PDO) cannot be serialized and the backup operation will break when such an object is stored e.g. in the $GLOBALS array.
If $CI
has PDO object, it will cause error. It won't be fixed.
In my opinion, ci-phpunit-test can't support global $CI
.
Hi kenjis: I used Base_Input to extens CI_Input,and I have a function:
when i used index.php
global $RTR
is{CI_Router}[9]
,but now use "Welcome_test" , it's null。 why?