20

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.

5
  • Hehe, global variables is what you should get rid off when you start unit-testing because of isolation and state Commented Jan 31, 2012 at 1:14
  • Btw, you probably wanted to say global $secret_key = "a secret Key"; not static, didn't you? Commented Jan 31, 2012 at 1:17
  • The static keyword should just be removed. global only makes sense inside a function/method. Commented Jan 31, 2012 at 2:21
  • 1
    @zerkms Yes, global vars should never be used, but unfortunatelly phpunit also deletes locally initiated variables (protected within particular unit test class). This is wrong and should be left upon developer to decide which variables will clear up via setUp() & tearDown(). For example when I test web services, I only need one login, that I want to reuse for many tests - but phpunit always deleted initiated instance and forces me to authenticate many times. That's wrong. Commented Apr 15, 2014 at 11:32
  • @lubosdz: that's isolation Commented Apr 15, 2014 at 11:34

3 Answers 3

17

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This fix my problem. I'm using PHP version 7.3.0 with PHPUnit 7.3.5. Thanks a lot!
6

You should ask phpunit not to backup globals

protected $backupGlobals = FALSE; 

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

2 Comments

Or make sure they are set during the bootstrap process by bootstrap.php or some module it loads before PHPUnit starts executing actual test methods.
@rp90: the above code contains strange static and you haven't explained what that static is about
0

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); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.