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

/lib/simpletest/dumper.php

https://github.com/komagata/plnet
PHP | 402 lines | 211 code | 20 blank | 171 comment | 44 complexity | fcedb6ab94c624014a8707d34efce191 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: dumper.php,v 1.28 2006/01/03 01:17:07 lastcraft Exp $
  7. */
  8. /**
  9. * does type matter
  10. */
  11. if (! defined('TYPE_MATTERS')) {
  12. define('TYPE_MATTERS', true);
  13. }
  14. /**
  15. * Displays variables as text and does diffs.
  16. * @package SimpleTest
  17. * @subpackage UnitTester
  18. */
  19. class SimpleDumper {
  20. /**
  21. * Renders a variable in a shorter form than print_r().
  22. * @param mixed $value Variable to render as a string.
  23. * @return string Human readable string form.
  24. * @access public
  25. */
  26. function describeValue($value) {
  27. $type = $this->getType($value);
  28. switch($type) {
  29. case "Null":
  30. return "NULL";
  31. case "Boolean":
  32. return "Boolean: " . ($value ? "true" : "false");
  33. case "Array":
  34. return "Array: " . count($value) . " items";
  35. case "Object":
  36. return "Object: of " . get_class($value);
  37. case "String":
  38. return "String: " . $this->clipString($value, 200);
  39. default:
  40. return "$type: $value";
  41. }
  42. return "Unknown";
  43. }
  44. /**
  45. * Gets the string representation of a type.
  46. * @param mixed $value Variable to check against.
  47. * @return string Type.
  48. * @access public
  49. */
  50. function getType($value) {
  51. if (! isset($value)) {
  52. return "Null";
  53. } elseif (is_bool($value)) {
  54. return "Boolean";
  55. } elseif (is_string($value)) {
  56. return "String";
  57. } elseif (is_integer($value)) {
  58. return "Integer";
  59. } elseif (is_float($value)) {
  60. return "Float";
  61. } elseif (is_array($value)) {
  62. return "Array";
  63. } elseif (is_resource($value)) {
  64. return "Resource";
  65. } elseif (is_object($value)) {
  66. return "Object";
  67. }
  68. return "Unknown";
  69. }
  70. /**
  71. * Creates a human readable description of the
  72. * difference between two variables. Uses a
  73. * dynamic call.
  74. * @param mixed $first First variable.
  75. * @param mixed $second Value to compare with.
  76. * @param boolean $identical If true then type anomolies count.
  77. * @return string Description of difference.
  78. * @access public
  79. */
  80. function describeDifference($first, $second, $identical = false) {
  81. if ($identical) {
  82. if (! $this->_isTypeMatch($first, $second)) {
  83. return "with type mismatch as [" . $this->describeValue($first) .
  84. "] does not match [" . $this->describeValue($second) . "]";
  85. }
  86. }
  87. $type = $this->getType($first);
  88. if ($type == "Unknown") {
  89. return "with unknown type";
  90. }
  91. $method = '_describe' . $type . 'Difference';
  92. return $this->$method($first, $second, $identical);
  93. }
  94. /**
  95. * Tests to see if types match.
  96. * @param mixed $first First variable.
  97. * @param mixed $second Value to compare with.
  98. * @return boolean True if matches.
  99. * @access private
  100. */
  101. function _isTypeMatch($first, $second) {
  102. return ($this->getType($first) == $this->getType($second));
  103. }
  104. /**
  105. * Clips a string to a maximum length.
  106. * @param string $value String to truncate.
  107. * @param integer $size Minimum string size to show.
  108. * @param integer $position Centre of string section.
  109. * @return string Shortened version.
  110. * @access public
  111. */
  112. function clipString($value, $size, $position = 0) {
  113. $length = strlen($value);
  114. if ($length <= $size) {
  115. return $value;
  116. }
  117. $position = min($position, $length);
  118. $start = ($size/2 > $position ? 0 : $position - $size/2);
  119. if ($start + $size > $length) {
  120. $start = $length - $size;
  121. }
  122. $value = substr($value, $start, $size);
  123. return ($start > 0 ? "..." : "") . $value . ($start + $size < $length ? "..." : "");
  124. }
  125. /**
  126. * Creates a human readable description of the
  127. * difference between two variables. The minimal
  128. * version.
  129. * @param null $first First value.
  130. * @param mixed $second Value to compare with.
  131. * @return string Human readable description.
  132. * @access private
  133. */
  134. function _describeGenericDifference($first, $second) {
  135. return "as [" . $this->describeValue($first) .
  136. "] does not match [" .
  137. $this->describeValue($second) . "]";
  138. }
  139. /**
  140. * Creates a human readable description of the
  141. * difference between a null and another variable.
  142. * @param null $first First null.
  143. * @param mixed $second Null to compare with.
  144. * @param boolean $identical If true then type anomolies count.
  145. * @return string Human readable description.
  146. * @access private
  147. */
  148. function _describeNullDifference($first, $second, $identical) {
  149. return $this->_describeGenericDifference($first, $second);
  150. }
  151. /**
  152. * Creates a human readable description of the
  153. * difference between a boolean and another variable.
  154. * @param boolean $first First boolean.
  155. * @param mixed $second Boolean to compare with.
  156. * @param boolean $identical If true then type anomolies count.
  157. * @return string Human readable description.
  158. * @access private
  159. */
  160. function _describeBooleanDifference($first, $second, $identical) {
  161. return $this->_describeGenericDifference($first, $second);
  162. }
  163. /**
  164. * Creates a human readable description of the
  165. * difference between a string and another variable.
  166. * @param string $first First string.
  167. * @param mixed $second String to compare with.
  168. * @param boolean $identical If true then type anomolies count.
  169. * @return string Human readable description.
  170. * @access private
  171. */
  172. function _describeStringDifference($first, $second, $identical) {
  173. if (is_object($second) || is_array($second)) {
  174. return $this->_describeGenericDifference($first, $second);
  175. }
  176. $position = $this->_stringDiffersAt($first, $second);
  177. $message = "at character $position";
  178. $message .= " with [" .
  179. $this->clipString($first, 200, $position) . "] and [" .
  180. $this->clipString($second, 200, $position) . "]";
  181. return $message;
  182. }
  183. /**
  184. * Creates a human readable description of the
  185. * difference between an integer and another variable.
  186. * @param integer $first First number.
  187. * @param mixed $second Number to compare with.
  188. * @param boolean $identical If true then type anomolies count.
  189. * @return string Human readable description.
  190. * @access private
  191. */
  192. function _describeIntegerDifference($first, $second, $identical) {
  193. if (is_object($second) || is_array($second)) {
  194. return $this->_describeGenericDifference($first, $second);
  195. }
  196. return "because [" . $this->describeValue($first) .
  197. "] differs from [" .
  198. $this->describeValue($second) . "] by " .
  199. abs($first - $second);
  200. }
  201. /**
  202. * Creates a human readable description of the
  203. * difference between two floating point numbers.
  204. * @param float $first First float.
  205. * @param mixed $second Float to compare with.
  206. * @param boolean $identical If true then type anomolies count.
  207. * @return string Human readable description.
  208. * @access private
  209. */
  210. function _describeFloatDifference($first, $second, $identical) {
  211. if (is_object($second) || is_array($second)) {
  212. return $this->_describeGenericDifference($first, $second);
  213. }
  214. return "because [" . $this->describeValue($first) .
  215. "] differs from [" .
  216. $this->describeValue($second) . "] by " .
  217. abs($first - $second);
  218. }
  219. /**
  220. * Creates a human readable description of the
  221. * difference between two arrays.
  222. * @param array $first First array.
  223. * @param mixed $second Array to compare with.
  224. * @param boolean $identical If true then type anomolies count.
  225. * @return string Human readable description.
  226. * @access private
  227. */
  228. function _describeArrayDifference($first, $second, $identical) {
  229. if (! is_array($second)) {
  230. return $this->_describeGenericDifference($first, $second);
  231. }
  232. if (! $this->_isMatchingKeys($first, $second, $identical)) {
  233. return "as key list [" .
  234. implode(", ", array_keys($first)) . "] does not match key list [" .
  235. implode(", ", array_keys($second)) . "]";
  236. }
  237. foreach (array_keys($first) as $key) {
  238. if ($identical && ($first[$key] === $second[$key])) {
  239. continue;
  240. }
  241. if (! $identical && ($first[$key] == $second[$key])) {
  242. continue;
  243. }
  244. return "with member [$key] " . $this->describeDifference(
  245. $first[$key],
  246. $second[$key],
  247. $identical);
  248. }
  249. return "";
  250. }
  251. /**
  252. * Compares two arrays to see if their key lists match.
  253. * For an identical match, the ordering and types of the keys
  254. * is significant.
  255. * @param array $first First array.
  256. * @param array $second Array to compare with.
  257. * @param boolean $identical If true then type anomolies count.
  258. * @return boolean True if matching.
  259. * @access private
  260. */
  261. function _isMatchingKeys($first, $second, $identical) {
  262. $first_keys = array_keys($first);
  263. $second_keys = array_keys($second);
  264. if ($identical) {
  265. return ($first_keys === $second_keys);
  266. }
  267. sort($first_keys);
  268. sort($second_keys);
  269. return ($first_keys == $second_keys);
  270. }
  271. /**
  272. * Creates a human readable description of the
  273. * difference between a resource and another variable.
  274. * @param resource $first First resource.
  275. * @param mixed $second Resource to compare with.
  276. * @param boolean $identical If true then type anomolies count.
  277. * @return string Human readable description.
  278. * @access private
  279. */
  280. function _describeResourceDifference($first, $second, $identical) {
  281. return $this->_describeGenericDifference($first, $second);
  282. }
  283. /**
  284. * Creates a human readable description of the
  285. * difference between two objects.
  286. * @param object $first First object.
  287. * @param mixed $second Object to compare with.
  288. * @param boolean $identical If true then type anomolies count.
  289. * @return string Human readable description.
  290. * @access private
  291. */
  292. function _describeObjectDifference($first, $second, $identical) {
  293. if (! is_object($second)) {
  294. return $this->_describeGenericDifference($first, $second);
  295. }
  296. return $this->_describeArrayDifference(
  297. get_object_vars($first),
  298. get_object_vars($second),
  299. $identical);
  300. }
  301. /**
  302. * Find the first character position that differs
  303. * in two strings by binary chop.
  304. * @param string $first First string.
  305. * @param string $second String to compare with.
  306. * @return integer Position of first differing
  307. * character.
  308. * @access private
  309. */
  310. function _stringDiffersAt($first, $second) {
  311. if (! $first || ! $second) {
  312. return 0;
  313. }
  314. if (strlen($first) < strlen($second)) {
  315. list($first, $second) = array($second, $first);
  316. }
  317. $position = 0;
  318. $step = strlen($first);
  319. while ($step > 1) {
  320. $step = (integer)(($step + 1) / 2);
  321. if (strncmp($first, $second, $position + $step) == 0) {
  322. $position += $step;
  323. }
  324. }
  325. return $position;
  326. }
  327. /**
  328. * Sends a formatted dump of a variable to a string.
  329. * @param mixed $variable Variable to display.
  330. * @return string Output from print_r().
  331. * @access public
  332. * @static
  333. */
  334. function dump($variable) {
  335. ob_start();
  336. print_r($variable);
  337. $formatted = ob_get_contents();
  338. ob_end_clean();
  339. return $formatted;
  340. }
  341. /**
  342. * Extracts the last assertion that was not within
  343. * Simpletest itself. The name must start with "assert".
  344. * @param array $stack List of stack frames.
  345. * @access public
  346. * @static
  347. */
  348. function getFormattedAssertionLine($stack) {
  349. foreach ($stack as $frame) {
  350. if (isset($frame['file'])) {
  351. if (strpos($frame['file'], SIMPLE_TEST) !== false) {
  352. if (dirname($frame['file']) . '/' == SIMPLE_TEST) {
  353. continue;
  354. }
  355. }
  356. }
  357. if (SimpleDumper::_stackFrameIsAnAssertion($frame)) {
  358. return ' at [' . $frame['file'] . ' line ' . $frame['line'] . ']';
  359. }
  360. }
  361. return '';
  362. }
  363. /**
  364. * Tries to determine if the method call is an assertion.
  365. * @param array $frame PHP stack frame.
  366. * @access private
  367. * @static
  368. */
  369. function _stackFrameIsAnAssertion($frame) {
  370. if (($frame['function'] == 'fail') || ($frame['function'] == 'pass')) {
  371. return true;
  372. }
  373. if (strncmp($frame['function'], 'assert', 6) == 0) {
  374. return true;
  375. }
  376. if (strncmp($frame['function'], 'expect', 6) == 0) {
  377. return true;
  378. }
  379. return false;
  380. }
  381. }
  382. ?>