Global Variables and PHPUnit

Sebastian Bergmann » 17 June 2008 » in Articles » 0 Comments

In PHP, global variables work like this:

  • A global variable $foo = 'bar'; is stored as $GLOBALS['foo'] = 'bar';.
  • $GLOBALS is a so-called superglobal.
  • Superglobals are built-in variables that are always available in all scopes.
  • In the scope of a function or method, you may access the global variable $foo by either directly accessing $GLOBALS['foo'] or by using global $foo; to create a local variable with a reference to the global variable.

It is hard to test code that uses singletons. The same is true for code that uses global variables.

Typically, the code you want to test is coupled strongly with a global variable and you cannot control its creation. An additional problem is the fact that one test's change to a global variable might break another test.

By default, PHPUnit runs your tests in a way where even changes to global and super-global variables (such as $GLOBALS) do not affect other tests.

The implementation of the backup and restore operations for the global and super-global variables uses serialize() and unserialize(). Objects of some classes that are provided by PHP itself, such as PDO, cannot be serialized and the backup operation will break when such an object is stored in the $GLOBALS array, for instance.

The backup and restore operations for the global and super-global variables can be disabled like this:

<?php
class MyTest extends PHPUnit_Framework_TestCase
{
    protected $backupGlobals = FALSE;
 
    // ...
}
?>

Please note that setting the $backupGlobals attribute inside the setUp() method, for instance, has no effect.

Starting with PHPUnit 3.3, the configuration of the $GLOBALS isolation is also supported on the TestSuite level. See ticket #497 for details.

Starting with PHPUnit 4.0, there will be support for running each test in a separate PHP process. Among other benefits, this will allow for testing singletons in an isolated environment.

Defined tags for this entry: , , ,

Trackback specific URI for this entry

0 Comments to "Global Variables and PHPUnit"

Display comments as (Linear | Threaded)
  1. No comments

7 Trackbacks to "Global Variables and PHPUnit"

  1. Sebastian Bergmann 02/09/2008 at 07:56
    I have rolled a release candidate of PHPUnit 3.3. Highlights of this release include:Improvements and Fixes for PHPUnit_Framework_AssertassertStringEqualsFile(), assertStringNotEqualsFile(), assertXmlStringEqualsXmlFile(), assertXmlStringNotEqualsXmlFile(
  2. Sebastian Bergmann 15/09/2008 at 16:00
    PHPUnit 3.3.This release is a major improvement to the popular open source testing solution for PHP applications. It includes new features and bug fixes. Highlights of this release include:Improvements and Fixes for PHPUnit_Framework_AssertassertStringEqu
  3. Sebastian Bergmann 15/09/2008 at 16:02
    Sebastian Bergmann and his contributors are proud to announce the immediate availability of PHPUnit 3.3.This release is a major improvement to the popular open source testing solution for PHP applications. It includes new features and bug fixes. Highlight
  4. Sebastian Bergmann 27/11/2008 at 10:40
    Some of PHPUnit features come with the cost of a performance penality. This posting explores the effect of the --no-syntax-check, $backupGlobals = FALSE;, and --coverage-html options. Syntax Check: disabled, $GLOBALS Backup: disabled, Code Coverage: disa
  5. Sebastian Bergmann 27/11/2008 at 12:50
    Some of PHPUnit features come with the cost of a performance penality. This posting explores the effect of the --no-syntax-check, $backupGlobals = FALSE;, and --coverage-html options. Syntax Check: disabled, $GLOBALS Backup: disabled, Code Coverage: disa
  6. Sebastian Bergmann 18/01/2009 at 14:36
    This is a follow-up to "The Cost of Test Isolation (and other PHPUnit Features)".Since the previous posting, I have added a backup/restore mechanism for static attributes of classes to PHPUnit. This is yet another feature of PHPUnit that makes the testing
  7. Sebastian Bergmann 11/02/2010 at 15:47
    I frequently quote Miško Hevery with "It is hard to test code that uses singletons." And then my audience asks me ... Why is it hard to test code that uses singletons? Lets have a look at the default implementation of the Singleton design pattern in P

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Submitted comments will be subject to moderation before being displayed.