Git Product home page Git Product logo

Comments (14)

kenjis avatar kenjis commented on May 24, 2024

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.

lingfengchencn avatar lingfengchencn commented on May 24, 2024

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.

kenjis avatar kenjis commented on May 24, 2024

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.

lingfengchencn avatar lingfengchencn commented on May 24, 2024

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.

kenjis avatar kenjis commented on May 24, 2024

Sorry, fixed. Please try #76

from ci-phpunit-test.

lingfengchencn avatar lingfengchencn commented on May 24, 2024

yes, it works ,
thanks . ๐Ÿ‘

from ci-phpunit-test.

lingfengchencn avatar lingfengchencn commented on May 24, 2024

global $CI is null

from ci-phpunit-test.

kenjis avatar kenjis commented on May 24, 2024

@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.

lingfengchencn avatar lingfengchencn commented on May 24, 2024

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.

kenjis avatar kenjis commented on May 24, 2024

It seems this is not so simple.
The above comment of mine was wrong.

I have to look into it.

from ci-phpunit-test.

kenjis avatar kenjis commented on May 24, 2024

@lingfengchencn Where do you use $CI? Would you give me some sample code and test code?

from ci-phpunit-test.

kenjis avatar kenjis commented on May 24, 2024

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.

lingfengchencn avatar lingfengchencn commented on May 24, 2024

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.

kenjis avatar kenjis commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.