Created
June 16, 2015 19:42
-
-
Save JDGrimes/262567acb0032ba23994 to your computer and use it in GitHub Desktop.
backup globals whitelist for PHPUnit test case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A list of global variables to back up. | |
* | |
* @since 2.0.0 | |
* | |
* @var string|string[] | |
*/ | |
protected $backup_globals; | |
/** | |
* Backed-up globals. | |
* | |
* @since 2.0.0 | |
* | |
* @var array | |
*/ | |
protected $globals_backup = array(); | |
public function setUp() { | |
parent::setUp(); | |
if ( ! empty( $this->backup_globals ) ) { | |
foreach ( (array) $this->backup_globals as $global ) { | |
if ( isset( $GLOBALS[ $global ] ) ) { | |
$this->globals_backup[ $global ] = $GLOBALS[ $global ]; | |
} | |
} | |
} | |
} | |
public function tearDown() { | |
if ( ! empty( $this->backup_globals ) ) { | |
foreach ( (array) $this->backup_globals as $global ) { | |
if ( isset( $this->globals_backup[ $global ] ) ) { | |
$GLOBALS[ $global ] = $this->globals_backup[ $global ]; | |
} else { | |
unset( $GLOBALS[ $global ] ); | |
} | |
} | |
} | |
parent::tearDown(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment