5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Sebastian Bergmann <sebastian@phpunit.de>
40 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42 * @link http://www.phpunit.de/
43 * @since File available since Release 3.4.0
51 * @author Sebastian Bergmann <sebastian@phpunit.de>
52 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
53 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
54 * @version Release: 3.5.13
55 * @link http://www.phpunit.de/
56 * @since Class available since Release 3.4.0
58 class PHPUnit_Util_GlobalState
63 protected static $globals = array();
68 protected static $staticAttributes = array();
73 protected static $superGlobalArrays = array(
86 protected static $superGlobalArraysLong = array(
95 public static function backupGlobals(array $blacklist)
97 self::$globals = array();
98 $superGlobalArrays = self::getSuperGlobalArrays();
100 foreach ($superGlobalArrays as $superGlobalArray) {
101 if (!in_array($superGlobalArray, $blacklist)) {
102 self::backupSuperGlobalArray($superGlobalArray);
106 foreach (array_keys($GLOBALS) as $key) {
107 if ($key != 'GLOBALS' &&
108 !in_array($key, $superGlobalArrays) &&
109 !in_array($key, $blacklist)) {
110 self::$globals['GLOBALS'][$key] = serialize($GLOBALS[$key]);
115 public static function restoreGlobals(array $blacklist)
117 if (ini_get('register_long_arrays') == '1') {
118 $superGlobalArrays = array_merge(
119 self::$superGlobalArrays, self::$superGlobalArraysLong
122 $superGlobalArrays = self::$superGlobalArrays;
125 foreach ($superGlobalArrays as $superGlobalArray) {
126 if (!in_array($superGlobalArray, $blacklist)) {
127 self::restoreSuperGlobalArray($superGlobalArray);
131 foreach (array_keys($GLOBALS) as $key) {
132 if ($key != 'GLOBALS' &&
133 !in_array($key, $superGlobalArrays) &&
134 !in_array($key, $blacklist)) {
135 if (isset(self::$globals['GLOBALS'][$key])) {
136 $GLOBALS[$key] = unserialize(
137 self::$globals['GLOBALS'][$key]
140 unset($GLOBALS[$key]);
145 self::$globals = array();
148 protected static function backupSuperGlobalArray($superGlobalArray)
150 self::$globals[$superGlobalArray] = array();
152 if (isset($GLOBALS[$superGlobalArray]) &&
153 is_array($GLOBALS[$superGlobalArray])) {
154 foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
155 self::$globals[$superGlobalArray][$key] = serialize($value);
160 protected static function restoreSuperGlobalArray($superGlobalArray)
162 if (isset($GLOBALS[$superGlobalArray]) &&
163 is_array($GLOBALS[$superGlobalArray]) &&
164 isset(self::$globals[$superGlobalArray])) {
167 $GLOBALS[$superGlobalArray], self::$globals[$superGlobalArray]
171 foreach ($keys as $key) {
172 if (isset(self::$globals[$superGlobalArray][$key])) {
173 $GLOBALS[$superGlobalArray][$key] = unserialize(
174 self::$globals[$superGlobalArray][$key]
177 unset($GLOBALS[$superGlobalArray][$key]);
182 self::$globals[$superGlobalArray] = array();
185 public static function getIncludedFilesAsString()
187 $blacklist = PHP_CodeCoverage::getInstance()->filter()->getBlacklist();
188 $blacklist = array_flip($blacklist['PHPUNIT']);
189 $files = get_included_files();
192 for ($i = count($files) - 1; $i > 0; $i--) {
193 if (!isset($blacklist[$files[$i]]) && is_file($files[$i])) {
194 $result = 'require_once \'' . $files[$i] . "';\n" . $result;
201 public static function getConstantsAsString()
203 $constants = get_defined_constants(TRUE);
206 if (isset($constants['user'])) {
207 foreach ($constants['user'] as $name => $value) {
209 'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
212 self::exportVariable($value)
220 public static function getGlobalsAsString()
223 $superGlobalArrays = self::getSuperGlobalArrays();
225 foreach ($superGlobalArrays as $superGlobalArray) {
226 if (isset($GLOBALS[$superGlobalArray]) &&
227 is_array($GLOBALS[$superGlobalArray])) {
228 foreach (array_keys($GLOBALS[$superGlobalArray]) as $key) {
230 '$GLOBALS[\'%s\'][\'%s\'] = %s;' . "\n",
233 self::exportVariable($GLOBALS[$superGlobalArray][$key])
239 $blacklist = $superGlobalArrays;
240 $blacklist[] = 'GLOBALS';
241 $blacklist[] = '_PEAR_Config_instance';
243 foreach (array_keys($GLOBALS) as $key) {
244 if (!in_array($key, $blacklist)) {
246 '$GLOBALS[\'%s\'] = %s;' . "\n",
248 self::exportVariable($GLOBALS[$key])
256 protected static function getSuperGlobalArrays()
258 if (ini_get('register_long_arrays') == '1') {
260 self::$superGlobalArrays, self::$superGlobalArraysLong
263 return self::$superGlobalArrays;
267 public static function backupStaticAttributes(array $blacklist)
269 self::$staticAttributes = array();
270 $declaredClasses = get_declared_classes();
271 $declaredClassesNum = count($declaredClasses);
273 for ($i = $declaredClassesNum - 1; $i >= 0; $i--) {
274 if (strpos($declaredClasses[$i], 'PHPUnit') !== 0 &&
275 strpos($declaredClasses[$i], 'File_Iterator') !== 0 &&
276 strpos($declaredClasses[$i], 'PHP_CodeCoverage') !== 0 &&
277 strpos($declaredClasses[$i], 'PHP_Timer') !== 0 &&
278 strpos($declaredClasses[$i], 'PHP_TokenStream') !== 0 &&
279 strpos($declaredClasses[$i], 'sfYaml') !== 0 &&
280 strpos($declaredClasses[$i], 'Text_Template') !== 0 &&
281 !$declaredClasses[$i] instanceof PHPUnit_Framework_Test) {
282 $class = new ReflectionClass($declaredClasses[$i]);
284 if (!$class->isUserDefined()) {
290 foreach ($class->getProperties() as $attribute) {
291 if ($attribute->isStatic()) {
292 $name = $attribute->getName();
294 if (!isset($blacklist[$declaredClasses[$i]]) ||
295 !in_array($name, $blacklist[$declaredClasses[$i]])) {
296 $attribute->setAccessible(TRUE);
297 $backup[$name] = serialize($attribute->getValue());
302 if (!empty($backup)) {
303 self::$staticAttributes[$declaredClasses[$i]] = $backup;
309 public static function restoreStaticAttributes()
311 foreach (self::$staticAttributes as $className => $staticAttributes) {
312 foreach ($staticAttributes as $name => $value) {
313 $reflector = new ReflectionProperty($className, $name);
314 $reflector->setAccessible(TRUE);
315 $reflector->setValue(unserialize($value));
319 self::$staticAttributes = array();
322 protected static function exportVariable($variable)
324 if (is_scalar($variable) || is_null($variable) ||
325 (is_array($variable) && self::arrayOnlyContainsScalars($variable))) {
326 return var_export($variable, TRUE);
329 return 'unserialize(\'' .
330 str_replace("'", "\'", serialize($variable)) .
334 protected static function arrayOnlyContainsScalars(array $array)
338 foreach ($array as $element) {
339 if (is_array($element)) {
340 $result = self::arrayOnlyContainsScalars($element);
343 else if (!is_scalar($element) && !is_null($element)) {
347 if ($result === FALSE) {