| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PHPUnit
|
|
|
4 |
*
|
|
|
5 |
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.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 |
* @category Testing
|
|
|
38 |
* @package PHPUnit
|
|
|
39 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
40 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.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 |
require_once 'PHPUnit/Util/Filter.php';
|
|
|
47 |
|
|
|
48 |
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
*
|
|
|
52 |
*
|
|
|
53 |
* @category Testing
|
|
|
54 |
* @package PHPUnit
|
|
|
55 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
56 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
57 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
58 |
* @version Release: 3.4.15
|
|
|
59 |
* @link http://www.phpunit.de/
|
|
|
60 |
* @since Class available since Release 3.4.0
|
|
|
61 |
*/
|
|
|
62 |
class PHPUnit_Util_GlobalState
|
|
|
63 |
{
|
|
|
64 |
/**
|
|
|
65 |
* @var array
|
|
|
66 |
*/
|
|
|
67 |
protected static $globals = array();
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* @var array
|
|
|
71 |
*/
|
|
|
72 |
protected static $staticAttributes = array();
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* @var array
|
|
|
76 |
*/
|
|
|
77 |
protected static $superGlobalArrays = array(
|
|
|
78 |
'_ENV',
|
|
|
79 |
'_POST',
|
|
|
80 |
'_GET',
|
|
|
81 |
'_COOKIE',
|
|
|
82 |
'_SERVER',
|
|
|
83 |
'_FILES',
|
|
|
84 |
'_REQUEST'
|
|
|
85 |
);
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* @var array
|
|
|
89 |
*/
|
|
|
90 |
protected static $superGlobalArraysLong = array(
|
|
|
91 |
'HTTP_ENV_VARS',
|
|
|
92 |
'HTTP_POST_VARS',
|
|
|
93 |
'HTTP_GET_VARS',
|
|
|
94 |
'HTTP_COOKIE_VARS',
|
|
|
95 |
'HTTP_SERVER_VARS',
|
|
|
96 |
'HTTP_POST_FILES'
|
|
|
97 |
);
|
|
|
98 |
|
|
|
99 |
public static function backupGlobals(array $blacklist)
|
|
|
100 |
{
|
|
|
101 |
self::$globals = array();
|
|
|
102 |
$superGlobalArrays = self::getSuperGlobalArrays();
|
|
|
103 |
|
|
|
104 |
foreach ($superGlobalArrays as $superGlobalArray) {
|
|
|
105 |
if (!in_array($superGlobalArray, $blacklist)) {
|
|
|
106 |
self::backupSuperGlobalArray($superGlobalArray);
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
foreach (array_keys($GLOBALS) as $key) {
|
|
|
111 |
if ($key != 'GLOBALS' &&
|
|
|
112 |
!in_array($key, $superGlobalArrays) &&
|
|
|
113 |
!in_array($key, $blacklist)) {
|
|
|
114 |
self::$globals['GLOBALS'][$key] = serialize($GLOBALS[$key]);
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public static function restoreGlobals(array $blacklist)
|
|
|
120 |
{
|
|
|
121 |
if (ini_get('register_long_arrays') == '1') {
|
|
|
122 |
$superGlobalArrays = array_merge(
|
|
|
123 |
self::$superGlobalArrays, self::$superGlobalArraysLong
|
|
|
124 |
);
|
|
|
125 |
} else {
|
|
|
126 |
$superGlobalArrays = self::$superGlobalArrays;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
foreach ($superGlobalArrays as $superGlobalArray) {
|
|
|
130 |
if (!in_array($superGlobalArray, $blacklist)) {
|
|
|
131 |
self::restoreSuperGlobalArray($superGlobalArray);
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
foreach (array_keys($GLOBALS) as $key) {
|
|
|
136 |
if ($key != 'GLOBALS' &&
|
|
|
137 |
!in_array($key, $superGlobalArrays) &&
|
|
|
138 |
!in_array($key, $blacklist)) {
|
|
|
139 |
if (isset(self::$globals['GLOBALS'][$key])) {
|
|
|
140 |
$GLOBALS[$key] = unserialize(
|
|
|
141 |
self::$globals['GLOBALS'][$key]
|
|
|
142 |
);
|
|
|
143 |
} else {
|
|
|
144 |
unset($GLOBALS[$key]);
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
self::$globals = array();
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
protected static function backupSuperGlobalArray($superGlobalArray)
|
|
|
153 |
{
|
|
|
154 |
self::$globals[$superGlobalArray] = array();
|
|
|
155 |
|
|
|
156 |
if (isset($GLOBALS[$superGlobalArray]) &&
|
|
|
157 |
is_array($GLOBALS[$superGlobalArray])) {
|
|
|
158 |
foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
|
|
|
159 |
self::$globals[$superGlobalArray][$key] = serialize($value);
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
protected static function restoreSuperGlobalArray($superGlobalArray)
|
|
|
165 |
{
|
|
|
166 |
if (isset($GLOBALS[$superGlobalArray]) &&
|
|
|
167 |
is_array($GLOBALS[$superGlobalArray]) &&
|
|
|
168 |
isset(self::$globals[$superGlobalArray])) {
|
|
|
169 |
$keys = array_keys(
|
|
|
170 |
array_merge(
|
|
|
171 |
$GLOBALS[$superGlobalArray], self::$globals[$superGlobalArray]
|
|
|
172 |
)
|
|
|
173 |
);
|
|
|
174 |
|
|
|
175 |
foreach ($keys as $key) {
|
|
|
176 |
if (isset(self::$globals[$superGlobalArray][$key])) {
|
|
|
177 |
$GLOBALS[$superGlobalArray][$key] = unserialize(
|
|
|
178 |
self::$globals[$superGlobalArray][$key]
|
|
|
179 |
);
|
|
|
180 |
} else {
|
|
|
181 |
unset($GLOBALS[$superGlobalArray][$key]);
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
self::$globals[$superGlobalArray] = array();
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
public static function getIncludedFilesAsString()
|
|
|
190 |
{
|
|
|
191 |
$blacklist = PHPUnit_Util_Filter::getBlacklistedFiles();
|
|
|
192 |
$blacklist = array_flip($blacklist['PHPUNIT']);
|
|
|
193 |
$files = get_included_files();
|
|
|
194 |
$result = '';
|
|
|
195 |
|
|
|
196 |
for ($i = count($files) - 1; $i > 0; $i--) {
|
|
|
197 |
if (!isset($blacklist[$files[$i]]) && is_file($files[$i])) {
|
|
|
198 |
$result = 'require_once \'' . $files[$i] . "';\n" . $result;
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
return $result;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
public static function getConstantsAsString()
|
|
|
206 |
{
|
|
|
207 |
$constants = get_defined_constants(TRUE);
|
|
|
208 |
$result = '';
|
|
|
209 |
|
|
|
210 |
if (isset($constants['user'])) {
|
|
|
211 |
foreach ($constants['user'] as $name => $value) {
|
|
|
212 |
$result .= sprintf(
|
|
|
213 |
'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
|
|
|
214 |
$name,
|
|
|
215 |
$name,
|
|
|
216 |
self::exportVariable($value)
|
|
|
217 |
);
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
return $result;
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
public static function getGlobalsAsString()
|
|
|
225 |
{
|
|
|
226 |
$result = '';
|
|
|
227 |
$superGlobalArrays = self::getSuperGlobalArrays();
|
|
|
228 |
|
|
|
229 |
foreach ($superGlobalArrays as $superGlobalArray) {
|
|
|
230 |
if (isset($GLOBALS[$superGlobalArray]) &&
|
|
|
231 |
is_array($GLOBALS[$superGlobalArray])) {
|
|
|
232 |
foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
|
|
|
233 |
$result .= sprintf(
|
|
|
234 |
'$GLOBALS[\'%s\'][\'%s\'] = %s;' . "\n",
|
|
|
235 |
$superGlobalArray,
|
|
|
236 |
$key,
|
|
|
237 |
self::exportVariable($GLOBALS[$superGlobalArray][$key])
|
|
|
238 |
);
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
$blacklist = $superGlobalArrays;
|
|
|
244 |
$blacklist[] = 'GLOBALS';
|
|
|
245 |
$blacklist[] = '_PEAR_Config_instance';
|
|
|
246 |
|
|
|
247 |
foreach (array_keys($GLOBALS) as $key) {
|
|
|
248 |
if (!in_array($key, $blacklist)) {
|
|
|
249 |
$result .= sprintf(
|
|
|
250 |
'$GLOBALS[\'%s\'] = %s;' . "\n",
|
|
|
251 |
$key,
|
|
|
252 |
self::exportVariable($GLOBALS[$key])
|
|
|
253 |
);
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
return $result;
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
protected static function getSuperGlobalArrays()
|
|
|
261 |
{
|
|
|
262 |
if (ini_get('register_long_arrays') == '1') {
|
|
|
263 |
return array_merge(
|
|
|
264 |
self::$superGlobalArrays, self::$superGlobalArraysLong
|
|
|
265 |
);
|
|
|
266 |
} else {
|
|
|
267 |
return self::$superGlobalArrays;
|
|
|
268 |
}
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
public static function backupStaticAttributes(array $blacklist)
|
|
|
272 |
{
|
|
|
273 |
self::$staticAttributes = array();
|
|
|
274 |
$declaredClasses = get_declared_classes();
|
|
|
275 |
$declaredClassesNum = count($declaredClasses);
|
|
|
276 |
|
|
|
277 |
for ($i = $declaredClassesNum - 1; $i >= 0; $i--) {
|
|
|
278 |
if (strpos($declaredClasses[$i], 'PHPUnit') !== 0 &&
|
|
|
279 |
!$declaredClasses[$i] instanceof PHPUnit_Framework_Test) {
|
|
|
280 |
$class = new ReflectionClass($declaredClasses[$i]);
|
|
|
281 |
|
|
|
282 |
if (!$class->isUserDefined()) {
|
|
|
283 |
break;
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
$backup = array();
|
|
|
287 |
|
|
|
288 |
foreach ($class->getProperties() as $attribute) {
|
|
|
289 |
if ($attribute->isStatic()) {
|
|
|
290 |
$name = $attribute->getName();
|
|
|
291 |
|
|
|
292 |
if (!isset($blacklist[$declaredClasses[$i]]) ||
|
|
|
293 |
!in_array($name, $blacklist[$declaredClasses[$i]])) {
|
|
|
294 |
$attribute->setAccessible(TRUE);
|
|
|
295 |
$backup[$name] = serialize($attribute->getValue());
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
if (!empty($backup)) {
|
|
|
301 |
self::$staticAttributes[$declaredClasses[$i]] = $backup;
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
}
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
public static function restoreStaticAttributes()
|
|
|
308 |
{
|
|
|
309 |
foreach (self::$staticAttributes as $className => $staticAttributes) {
|
|
|
310 |
foreach ($staticAttributes as $name => $value) {
|
|
|
311 |
$reflector = new ReflectionProperty($className, $name);
|
|
|
312 |
$reflector->setAccessible(TRUE);
|
|
|
313 |
$reflector->setValue(unserialize($value));
|
|
|
314 |
}
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
self::$staticAttributes = array();
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
protected static function exportVariable($variable)
|
|
|
321 |
{
|
|
|
322 |
if (is_scalar($variable) || is_null($variable) ||
|
|
|
323 |
(is_array($variable) && self::arrayOnlyContainsScalars($variable))) {
|
|
|
324 |
return var_export($variable, TRUE);
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
return 'unserialize(\'' . serialize($variable) . '\')';
|
|
|
328 |
}
|
|
|
329 |
|
|
|
330 |
protected static function arrayOnlyContainsScalars(array $array)
|
|
|
331 |
{
|
|
|
332 |
$result = TRUE;
|
|
|
333 |
|
|
|
334 |
foreach ($array as $element) {
|
|
|
335 |
if (is_array($element)) {
|
|
|
336 |
$result = self::arrayOnlyContainsScalars($element);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
else if (!is_scalar($element) && !is_null($element)) {
|
|
|
340 |
$result = FALSE;
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
if ($result === FALSE) {
|
|
|
344 |
break;
|
|
|
345 |
}
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
return $result;
|
|
|
349 |
}
|
|
|
350 |
}
|
|
|
351 |
?>
|