]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Util/GlobalState.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Util / GlobalState.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
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
18  *     distribution.
19  *
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.
23  *
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.
36  *
37  * @package    PHPUnit
38  * @subpackage Util
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
44  */
45
46 /**
47  *
48  *
49  * @package    PHPUnit
50  * @subpackage Util
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
57  */
58 class PHPUnit_Util_GlobalState
59 {
60     /**
61      * @var array
62      */
63     protected static $globals = array();
64
65     /**
66      * @var array
67      */
68     protected static $staticAttributes = array();
69
70     /**
71      * @var array
72      */
73     protected static $superGlobalArrays = array(
74       '_ENV',
75       '_POST',
76       '_GET',
77       '_COOKIE',
78       '_SERVER',
79       '_FILES',
80       '_REQUEST'
81     );
82
83     /**
84      * @var array
85      */
86     protected static $superGlobalArraysLong = array(
87       'HTTP_ENV_VARS',
88       'HTTP_POST_VARS',
89       'HTTP_GET_VARS',
90       'HTTP_COOKIE_VARS',
91       'HTTP_SERVER_VARS',
92       'HTTP_POST_FILES'
93     );
94
95     public static function backupGlobals(array $blacklist)
96     {
97         self::$globals     = array();
98         $superGlobalArrays = self::getSuperGlobalArrays();
99
100         foreach ($superGlobalArrays as $superGlobalArray) {
101             if (!in_array($superGlobalArray, $blacklist)) {
102                 self::backupSuperGlobalArray($superGlobalArray);
103             }
104         }
105
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]);
111             }
112         }
113     }
114
115     public static function restoreGlobals(array $blacklist)
116     {
117         if (ini_get('register_long_arrays') == '1') {
118             $superGlobalArrays = array_merge(
119               self::$superGlobalArrays, self::$superGlobalArraysLong
120             );
121         } else {
122             $superGlobalArrays = self::$superGlobalArrays;
123         }
124
125         foreach ($superGlobalArrays as $superGlobalArray) {
126             if (!in_array($superGlobalArray, $blacklist)) {
127                 self::restoreSuperGlobalArray($superGlobalArray);
128             }
129         }
130
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]
138                     );
139                 } else {
140                     unset($GLOBALS[$key]);
141                 }
142             }
143         }
144
145         self::$globals = array();
146     }
147
148     protected static function backupSuperGlobalArray($superGlobalArray)
149     {
150         self::$globals[$superGlobalArray] = array();
151
152         if (isset($GLOBALS[$superGlobalArray]) &&
153             is_array($GLOBALS[$superGlobalArray])) {
154             foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
155                 self::$globals[$superGlobalArray][$key] = serialize($value);
156             }
157         }
158     }
159
160     protected static function restoreSuperGlobalArray($superGlobalArray)
161     {
162         if (isset($GLOBALS[$superGlobalArray]) &&
163             is_array($GLOBALS[$superGlobalArray]) &&
164             isset(self::$globals[$superGlobalArray])) {
165             $keys = array_keys(
166               array_merge(
167                 $GLOBALS[$superGlobalArray], self::$globals[$superGlobalArray]
168               )
169             );
170
171             foreach ($keys as $key) {
172                 if (isset(self::$globals[$superGlobalArray][$key])) {
173                     $GLOBALS[$superGlobalArray][$key] = unserialize(
174                       self::$globals[$superGlobalArray][$key]
175                     );
176                 } else {
177                     unset($GLOBALS[$superGlobalArray][$key]);
178                 }
179             }
180         }
181
182         self::$globals[$superGlobalArray] = array();
183     }
184
185     public static function getIncludedFilesAsString()
186     {
187         $blacklist = PHP_CodeCoverage::getInstance()->filter()->getBlacklist();
188         $blacklist = array_flip($blacklist['PHPUNIT']);
189         $files     = get_included_files();
190         $result    = '';
191
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;
195             }
196         }
197
198         return $result;
199     }
200
201     public static function getConstantsAsString()
202     {
203         $constants = get_defined_constants(TRUE);
204         $result    = '';
205
206         if (isset($constants['user'])) {
207             foreach ($constants['user'] as $name => $value) {
208                 $result .= sprintf(
209                   'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
210                   $name,
211                   $name,
212                   self::exportVariable($value)
213                 );
214             }
215         }
216
217         return $result;
218     }
219
220     public static function getGlobalsAsString()
221     {
222         $result            = '';
223         $superGlobalArrays = self::getSuperGlobalArrays();
224
225         foreach ($superGlobalArrays as $superGlobalArray) {
226             if (isset($GLOBALS[$superGlobalArray]) &&
227                 is_array($GLOBALS[$superGlobalArray])) {
228                 foreach (array_keys($GLOBALS[$superGlobalArray]) as $key) {
229                     $result .= sprintf(
230                       '$GLOBALS[\'%s\'][\'%s\'] = %s;' . "\n",
231                       $superGlobalArray,
232                       $key,
233                       self::exportVariable($GLOBALS[$superGlobalArray][$key])
234                     );
235                 }
236             }
237         }
238
239         $blacklist   = $superGlobalArrays;
240         $blacklist[] = 'GLOBALS';
241         $blacklist[] = '_PEAR_Config_instance';
242
243         foreach (array_keys($GLOBALS) as $key) {
244             if (!in_array($key, $blacklist)) {
245                 $result .= sprintf(
246                   '$GLOBALS[\'%s\'] = %s;' . "\n",
247                   $key,
248                   self::exportVariable($GLOBALS[$key])
249                 );
250             }
251         }
252
253         return $result;
254     }
255
256     protected static function getSuperGlobalArrays()
257     {
258         if (ini_get('register_long_arrays') == '1') {
259             return array_merge(
260               self::$superGlobalArrays, self::$superGlobalArraysLong
261             );
262         } else {
263             return self::$superGlobalArrays;
264         }
265     }
266
267     public static function backupStaticAttributes(array $blacklist)
268     {
269         self::$staticAttributes = array();
270         $declaredClasses        = get_declared_classes();
271         $declaredClassesNum     = count($declaredClasses);
272
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]);
283
284                 if (!$class->isUserDefined()) {
285                     break;
286                 }
287
288                 $backup = array();
289
290                 foreach ($class->getProperties() as $attribute) {
291                     if ($attribute->isStatic()) {
292                         $name = $attribute->getName();
293
294                         if (!isset($blacklist[$declaredClasses[$i]]) ||
295                            !in_array($name, $blacklist[$declaredClasses[$i]])) {
296                             $attribute->setAccessible(TRUE);
297                             $backup[$name] = serialize($attribute->getValue());
298                         }
299                     }
300                 }
301
302                 if (!empty($backup)) {
303                     self::$staticAttributes[$declaredClasses[$i]] = $backup;
304                 }
305             }
306         }
307     }
308
309     public static function restoreStaticAttributes()
310     {
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));
316             }
317         }
318
319         self::$staticAttributes = array();
320     }
321
322     protected static function exportVariable($variable)
323     {
324         if (is_scalar($variable) || is_null($variable) ||
325            (is_array($variable) && self::arrayOnlyContainsScalars($variable))) {
326             return var_export($variable, TRUE);
327         }
328
329         return 'unserialize(\'' .
330                 str_replace("'", "\'", serialize($variable)) .
331                 '\')';
332     }
333
334     protected static function arrayOnlyContainsScalars(array $array)
335     {
336         $result = TRUE;
337
338         foreach ($array as $element) {
339             if (is_array($element)) {
340                 $result = self::arrayOnlyContainsScalars($element);
341             }
342
343             else if (!is_scalar($element) && !is_null($element)) {
344                 $result = FALSE;
345             }
346
347             if ($result === FALSE) {
348                 break;
349             }
350         }
351
352         return $result;
353     }
354 }