PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/vendor/phpunit/phpunit/PHPUnit/Util/GlobalState.php

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