Comments (14)
Because ci-phpunit-test does not set global variable $RTR
.
How about changing your code like this?
$RTR =& load_class('Router');
[Edited]
from ci-phpunit-test.
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
from ci-phpunit-test.
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
from ci-phpunit-test.
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;
}
}
from ci-phpunit-test.
Sorry, fixed. Please try #76
from ci-phpunit-test.
yes, it works ,
thanks . ๐
from ci-phpunit-test.
global $CI is null
from ci-phpunit-test.
@lingfengchencn Why do you so love global variables? ๐
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);
/*
* ------------------------------------------------------
from ci-phpunit-test.
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);
from ci-phpunit-test.
It seems this is not so simple.
The above comment of mine was wrong.
I have to look into it.
from ci-phpunit-test.
@lingfengchencn Where do you use $CI
? Would you give me some sample code and test code?
from ci-phpunit-test.
If we set global $CI
, we get PDOException: You cannot serialize or unserialize PDO instances
error in every test method when we use PDO.
from ci-phpunit-test.
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 ...
from ci-phpunit-test.
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
.
from ci-phpunit-test.
Related Issues (20)
- 3.0.2 release date? HOT 2
- Coverage report is acting weird HOT 2
- Cannot create mock whose methods throw exceptions HOT 2
- How to extend the test runner for post operations? HOT 4
- 3.x new version with Updates for PHP 8.1 HOT 3
- How to stop PHP code execution when exit() in unit test? HOT 5
- Argument 1 passed to PHPUnit\Runner\TestSuiteSorter::reorderTestsInSuite() must implement interface PHPUnit\Framework\Test, instance of PHPUnit\Framework\TestSuite given HOT 3
- PHPUnit 9.5.24 + CodeIgniter 3.1.13 + PHP 8.1 HOT 2
- Session problem HOT 8
- Mocking DB object can't use getDouble? HOT 1
- How to clear RAM memory after run testcase in testsuite? HOT 3
- How to mock DB in CI? HOT 1
- PHP Fatal error: Uncaught Error: Class "TestCase" not found HOT 2
- Replacement of helper function redirect() does not work HOT 3
- cant set session $this->session->userdata HOT 1
- Upgrading to CodeIgniter4 is strongly recommended
- How to test external API with json response? HOT 1
- 1.0.2 fails with phpunit <7 when installed with "--from-composer" HOT 2
- issue during code coverage when referencing $this->uri in a view HOT 5
- Add support for php 8.2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ci-phpunit-test.