global variables are null when using PHPUnit

橙三吉。 提交于 2019-12-17 16:35:43

问题


I am putting PHPUnit testing into an existing project. Global constants variables are used extensively. In my unit test functions are failing because the global variables are null. Here is an example of a failing test

static $secret_key = "a secret Key";
class secret_key_Test extends PHPUnit_Framework_TestCase
{
    function test_secret_key()
    {
        global $secret_key; 
        $this->assertEquals($secret_key, "a secret Key");   
    }
}

>> Failed asserting that 'a secret Key' matches expected null

Any help would be greatly appreciated

Update: I have tried removing static and adding

protected $backupGlobals = FALSE;

To the class declaration without success.


回答1:


This answer doesn't work. I asked a virtually identical question here and wound up with an answer that makes more sense; you can't overwrite the protected property $backupGlobals in the test Class that PHPUnit will see. If you're running on the command line, it seems that you can get Globals to work by creating an xml configuration file and setting up backupGlobals to false there.

EDIT: You need to declare $secret_key both global and assign a value to it in the global space when using PHPUnit. PHP defaults to placing globally initialized variables into the global namespace, but PHPUnit changes this default when backing up globals!

The following changes need to happen:

global $secret_key; // Declaring variable global in global namespace
$secret_key = "a secret Key"; // Assigning value to global variable

Your code should work now.




回答2:


You should ask phpunit not to backup globals

protected $backupGlobals = FALSE;

like it is said in the original article from S. Bergmann: http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html




回答3:


You will have to setup your global variable while bootstraping your testing. Here is a sample code of how I wrote test

    /**
     * Class to allow us set product on the fly
     */
    class Product
    {
        public function __call($method, $args)
        {
            if (isset($this->$method)) {
                $func = $this->$method;
                return call_user_func_array($func, $args);
            }
        }
    }

    /**
     * UssdShortcode Tester
     */
    class ShortCodeTester extends WP_UnitTestCase {

        protected  $product;

        public function setUp()
        {   
            $this->product            = new Product;
            $this->product->get_id    = function(){ return 50; };

            $GLOBALS['product']       = $this->product;
        }

        /**
         * A single example test.
         */
        function test_it_can_display_ussd_shortcode() {

            $displayer = new UssdShortCodeDisplayer;
            $expected  = view('show-product-short-code',['product_id' => $this->product->get_id() ]);
            $results   = $displayer->display($this->product->get_id());

            // $this->assertRegexp('/'.$expected.'/', $results);
            $this->assertEquals($expected,$results);
        }
    }


来源:https://stackoverflow.com/questions/9073144/global-variables-are-null-when-using-phpunit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!