Skip to content

Commit

Permalink
- Merge [3379].
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jul 7, 2008
1 parent 7c42419 commit e52321d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 21 deletions.
71 changes: 59 additions & 12 deletions PHPUnit/Framework/TestCase.php
Expand Up @@ -125,14 +125,13 @@ abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert imple
protected $backupGlobals = TRUE;

/**
* Enable or disable creating the $GLOBALS reference that is required
* for the "global" keyword to work correctly.
* Enable or disable the cleanup of the $GLOBALS array.
* Overwrite this attribute in a child class of TestCase.
* Setting this attribute in setUp() has no effect!
*
* @var boolean
*/
protected $createGlobalsReference = FALSE;
protected $cleanupGlobals = FALSE;

/**
* @var array
Expand Down Expand Up @@ -367,7 +366,18 @@ public function runBare()
{
// Backup the $GLOBALS array.
if ($this->backupGlobals === TRUE) {
$globalsBackup = serialize($GLOBALS);
$globalsBackup = array();

foreach ($GLOBALS as $k => $v) {
if ($k != 'GLOBALS') {
$globalsBackup[$k] = serialize($v);
}
}
}

// Cleanup the $GLOBALS array.
else if ($this->cleanupGlobals === TRUE) {
$this->cleanupGlobals();
}

// Set up the fixture.
Expand Down Expand Up @@ -415,13 +425,18 @@ public function runBare()

// Restore the $GLOBALS array.
if ($this->backupGlobals === TRUE) {
$GLOBALS = unserialize($globalsBackup);
$this->cleanupGlobals();

if ($this->createGlobalsReference) {
$GLOBALS['GLOBALS'] = &$GLOBALS;
foreach ($globalsBackup as $k => $v) {
$GLOBALS[$k] = unserialize($v);
}
}

// Cleanup the $GLOBALS array.
else if ($this->cleanupGlobals === TRUE) {
$this->cleanupGlobals();
}

// Clean up INI settings.
foreach ($this->iniSettings as $varName => $oldValue) {
ini_set($varName, $oldValue);
Expand Down Expand Up @@ -519,13 +534,22 @@ public function setName($name)
* Calling this method in setUp() has no effect!
*
* @param boolean $backupGlobals
* @param boolean $createGlobalsReference
* @since Method available since Release 3.3.0
*/
public function setGlobalsBackup($backupGlobals = TRUE, $createGlobalsReference = FALSE)
public function setBackupGlobals($backupGlobals = TRUE)
{
$this->backupGlobals = $backupGlobals;
}

/**
* Calling this method in setUp() has no effect!
*
* @param boolean $cleanupGlobals
* @since Method available since Release 3.3.0
*/
public function setCleanupGlobals($cleanupGlobals = FALSE)
{
$this->backupGlobals = $backupGlobals;
$this->createGlobalsReference = $createGlobalsReference;
$this->cleanupGlobals = $cleanupGlobals;
}

/**
Expand Down Expand Up @@ -919,11 +943,34 @@ protected function assertPostConditions()
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
*/
protected function tearDown()
{
}

/**
* @since Method available since Release 3.3.0
*/
protected function cleanupGlobals()
{
$GLOBALS = array(
'_ENV' => array(),
'HTTP_ENV_VARS' => array(),
'argv' => array(),
'argc' => array(),
'_POST' => array(),
'HTTP_POST_VARS' => array(),
'_GET' => array(),
'HTTP_GET_VARS' => array(),
'_COOKIE' => array(),
'HTTP_COOKIE_VARS' => array(),
'_SERVER' => array(),
'HTTP_SERVER_VARS' => array(),
'_FILES' => array(),
'HTTP_POST_FILES' => array(),
'_REQUEST' => array()
);
}
}

}
Expand Down
27 changes: 18 additions & 9 deletions PHPUnit/Framework/TestSuite.php
Expand Up @@ -103,12 +103,11 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra
protected $backupGlobals = TRUE;

/**
* Enable or disable creating the $GLOBALS reference that is required
* for the "global" keyword to work correctly.
* Enable or disable the cleanup of the $GLOBALS array.
*
* @var boolean
*/
protected $createGlobalsReference = FALSE;
protected $cleanupGlobals = FALSE;

/**
* Fixture that is shared between the tests of this test suite.
Expand Down Expand Up @@ -646,7 +645,8 @@ public function run(PHPUnit_Framework_TestResult $result = NULL, $filter = FALSE
}

if ($test instanceof PHPUnit_Framework_TestSuite) {
$test->setGlobalsBackup($this->backupGlobals, $this->createGlobalsReference);
$test->setBackupGlobals($this->backupGlobals);
$test->setCleanupGlobals($this->cleanupGlobals);
$test->setSharedFixture($this->sharedFixture);
$test->run($result, $filter, $groups, $excludeGroups);
} else {
Expand All @@ -668,6 +668,8 @@ public function run(PHPUnit_Framework_TestResult $result = NULL, $filter = FALSE

if ($runTest) {
if ($test instanceof PHPUnit_Framework_TestCase) {
$test->setBackupGlobals($this->backupGlobals);
$test->setCleanupGlobals($this->cleanupGlobals);
$test->setSharedFixture($this->sharedFixture);
}

Expand Down Expand Up @@ -802,7 +804,7 @@ public static function isTestMethod(ReflectionMethod $method)
// @scenario on TestCase::testMethod()
// @test on TestCase::testMethod()
return strpos($method->getDocComment(), '@test') !== FALSE ||
strpos($method->getDocComment(), '@scenario') !== FALSE;
strpos($method->getDocComment(), '@scenario') !== FALSE;
}

/**
Expand All @@ -816,13 +818,20 @@ protected static function warning($message)

/**
* @param boolean $backupGlobals
* @param boolean $createGlobalsReference
* @since Method available since Release 3.3.0
*/
public function setGlobalsBackup($backupGlobals = TRUE, $createGlobalsReference = FALSE)
public function setBackupGlobals($backupGlobals = TRUE)
{
$this->backupGlobals = $backupGlobals;
$this->createGlobalsReference = $createGlobalsReference;
$this->backupGlobals = $backupGlobals;
}

/**
* @param boolean $cleanupGlobals
* @since Method available since Release 3.3.0
*/
public function setCleanupGlobals($cleanupGlobals = FALSE)
{
$this->cleanupGlobals = $cleanupGlobals;
}

/**
Expand Down

0 comments on commit e52321d

Please sign in to comment.