PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/NetteTest/NetteTestHelpers.php

https://github.com/DocX/nette
PHP | 282 lines | 161 code | 58 blank | 63 comment | 27 complexity | 145658fab8476a6b40ecb7d84bc6a49b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Test
  17. */
  18. /**
  19. * Test helpers.
  20. *
  21. * @author David Grudl
  22. * @package Nette\Test
  23. */
  24. final class NetteTestHelpers
  25. {
  26. /** @var int */
  27. static public $maxDepth = 5;
  28. /** @var array */
  29. private static $sections;
  30. /**
  31. * Configures PHP and environment.
  32. * @return void
  33. */
  34. public static function startup()
  35. {
  36. error_reporting(E_ALL | E_STRICT);
  37. ini_set('display_errors', TRUE);
  38. ini_set('html_errors', FALSE);
  39. ini_set('log_errors', FALSE);
  40. $_SERVER = array_intersect_key($_SERVER, array_flip(array('PHP_SELF', 'SCRIPT_NAME', 'SERVER_ADDR', 'SERVER_SOFTWARE', 'HTTP_HOST', 'DOCUMENT_ROOT', 'OS')));
  41. $_SERVER['REQUEST_TIME'] = 1234567890;
  42. $_ENV = array();
  43. if (PHP_SAPI !== 'cli') {
  44. header('Content-Type: text/plain; charset=utf-8');
  45. }
  46. if (extension_loaded('xdebug')) {
  47. xdebug_disable();
  48. xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
  49. register_shutdown_function(array(__CLASS__, 'prepareSaveCoverage'));
  50. }
  51. set_exception_handler(array(__CLASS__, 'exceptionHandler'));
  52. }
  53. /**
  54. * Purges directory.
  55. * @param string
  56. * @return void
  57. */
  58. public static function purge($dir)
  59. {
  60. @mkdir($dir); // intentionally @
  61. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST) as $entry) {
  62. if ($entry->getBasename() === '.gitignore') {
  63. // ignore
  64. } elseif ($entry->isDir()) {
  65. rmdir($entry);
  66. } else {
  67. unlink($entry);
  68. }
  69. }
  70. }
  71. /**
  72. * Returns current test section.
  73. * @param string
  74. * @param string
  75. * @return mixed
  76. */
  77. public static function getSection($file, $section)
  78. {
  79. if (!isset(self::$sections[$file])) {
  80. self::$sections[$file] = NetteTestCase::parseSections($file);
  81. }
  82. $lowerSection = strtolower($section);
  83. if (!isset(self::$sections[$file][$lowerSection])) {
  84. throw new Exception("Missing section '$section' in file '$file'.");
  85. }
  86. if (in_array($section, array('GET', 'POST', 'SERVER'), TRUE)) {
  87. return NetteTestCase::parseLines(self::$sections[$file][$lowerSection], '=');
  88. } else {
  89. return self::$sections[$file][$lowerSection];
  90. }
  91. }
  92. /**
  93. * Dumps information about a variable in readable format.
  94. * @param mixed variable to dump
  95. * @return void
  96. * @internal
  97. */
  98. public static function dump(& $var, $level = 0)
  99. {
  100. static $tableUtf, $tableBin, $re = '#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u';
  101. if ($tableUtf === NULL) {
  102. foreach (range("\x00", "\xFF") as $ch) {
  103. if (ord($ch) < 32 && strpos("\r\n\t", $ch) === FALSE) $tableUtf[$ch] = $tableBin[$ch] = '\\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
  104. elseif (ord($ch) < 127) $tableUtf[$ch] = $tableBin[$ch] = $ch;
  105. else { $tableUtf[$ch] = $ch; $tableBin[$ch] = '\\x' . dechex(ord($ch)); }
  106. }
  107. $tableUtf['\\x'] = $tableBin['\\x'] = '\\\\x';
  108. }
  109. if (is_bool($var)) {
  110. echo "bool(" . ($var ? 'TRUE' : 'FALSE') . ")\n";
  111. } elseif ($var === NULL) {
  112. echo "NULL\n";
  113. } elseif (is_int($var)) {
  114. echo "int($var)\n";
  115. } elseif (is_float($var)) {
  116. echo "float($var)\n";
  117. } elseif (is_string($var)) {
  118. $s = strtr($var, preg_match($re, $var) || preg_last_error() ? $tableBin : $tableUtf);
  119. echo "string(" . strlen($var) . ") \"$s\"\n";
  120. } elseif (is_array($var)) {
  121. echo "array(" . count($var) . ") ";
  122. $space = str_repeat("\t", $level);
  123. static $marker;
  124. if ($marker === NULL) $marker = uniqid("\x00", TRUE);
  125. if (empty($var)) {
  126. } elseif (isset($var[$marker])) {
  127. echo "{\n$space\t*RECURSION*\n$space}";
  128. } elseif ($level < self::$maxDepth) {
  129. echo "{\n";
  130. $var[$marker] = 0;
  131. foreach ($var as $k => &$v) {
  132. if ($k === $marker) continue;
  133. $k = is_int($k) ? $k : '"' . strtr($k, preg_match($re, $k) || preg_last_error() ? $tableBin : $tableUtf) . '"';
  134. echo "$space\t$k => ";
  135. self::dump($v, $level + 1);
  136. }
  137. unset($var[$marker]);
  138. echo "$space}";
  139. } else {
  140. echo "{\n$space\t...\n$space}";
  141. }
  142. echo "\n";
  143. } elseif ($var instanceof Exception) {
  144. echo 'Exception ', get_class($var), ': ', ($var->getCode() ? '#' . $var->getCode() . ' ' : '') . $var->getMessage(), "\n";
  145. } elseif (is_object($var)) {
  146. $arr = (array) $var;
  147. echo "object(" . get_class($var) . ") (" . count($arr) . ") ";
  148. $space = str_repeat("\t", $level);
  149. static $list = array();
  150. if (empty($arr)) {
  151. echo "{}";
  152. } elseif (in_array($var, $list, TRUE)) {
  153. echo "{\n$space\t*RECURSION*\n$space}";
  154. } elseif ($level < self::$maxDepth) {
  155. echo "{\n";
  156. $list[] = $var;
  157. foreach ($arr as $k => &$v) {
  158. $m = '';
  159. if ($k[0] === "\x00") {
  160. $m = $k[1] === '*' ? ' protected' : ' private';
  161. $k = substr($k, strrpos($k, "\x00") + 1);
  162. }
  163. $k = strtr($k, preg_match($re, $k) || preg_last_error() ? $tableBin : $tableUtf);
  164. echo "$space\t\"$k\"$m => ";
  165. echo self::dump($v, $level + 1);
  166. }
  167. array_pop($list);
  168. echo "$space}";
  169. } else {
  170. echo "{\n$space\t...\n$space}";
  171. }
  172. echo "\n";
  173. } elseif (is_resource($var)) {
  174. echo "resource of type(" . get_resource_type($var) . ")\n";
  175. } else {
  176. echo "unknown type\n";
  177. }
  178. }
  179. /**
  180. * Custom exception handler.
  181. * @param \Exception
  182. * @return void
  183. */
  184. public static function exceptionHandler(Exception $exception)
  185. {
  186. self::dump($exception, 0);
  187. }
  188. /**
  189. * Coverage saving helper.
  190. * @return void
  191. */
  192. public static function prepareSaveCoverage()
  193. {
  194. register_shutdown_function(array(__CLASS__, 'saveCoverage'));
  195. }
  196. /**
  197. * Saves information about code coverage.
  198. * @return void
  199. */
  200. public static function saveCoverage()
  201. {
  202. $file = dirname(__FILE__) . '/coverage.tmp';
  203. $coverage = @unserialize(file_get_contents($file));
  204. $root = realpath(dirname(__FILE__) . '/../../Nette') . DIRECTORY_SEPARATOR;
  205. foreach (xdebug_get_code_coverage() as $filename => $lines) {
  206. if (strncmp($root, $filename, strlen($root))) continue;
  207. foreach ($lines as $num => $val) {
  208. if (empty($coverage[$filename][$num]) || $val > 0) {
  209. $coverage[$filename][$num] = $val; // -1 => untested; -2 => dead code
  210. }
  211. }
  212. }
  213. file_put_contents($file, serialize($coverage));
  214. }
  215. /**
  216. * Skips this test.
  217. * @return void
  218. */
  219. public static function skip()
  220. {
  221. header('X-Nette-Test-Skip: 1');
  222. exit;
  223. }
  224. }