Skip to content

Instantly share code, notes, and snippets.

@JDGrimes
Created June 16, 2015 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JDGrimes/262567acb0032ba23994 to your computer and use it in GitHub Desktop.
Save JDGrimes/262567acb0032ba23994 to your computer and use it in GitHub Desktop.
backup globals whitelist for PHPUnit test case
/**
* 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