3
* base include file for SimpleTest
5
* @subpackage UnitTester
6
* @version $Id: dumper.php,v 1.28 2006/01/03 01:17:07 lastcraft Exp $
11
if (! defined('TYPE_MATTERS')) {
12
define('TYPE_MATTERS', true);
16
* Displays variables as text and does diffs.
18
* @subpackage UnitTester
23
* Renders a variable in a shorter form than print_r().
24
* @param mixed $value Variable to render as a string.
25
* @return string Human readable string form.
28
function describeValue($value) {
29
$type = $this->getType($value);
34
return "Boolean: " . ($value ? "true" : "false");
36
return "Array: " . count($value) . " items";
38
return "Object: of " . get_class($value);
40
return "String: " . $this->clipString($value, 200);
42
return "$type: $value";
48
* Gets the string representation of a type.
49
* @param mixed $value Variable to check against.
50
* @return string Type.
53
function getType($value) {
54
if (! isset($value)) {
56
} elseif (is_bool($value)) {
58
} elseif (is_string($value)) {
60
} elseif (is_integer($value)) {
62
} elseif (is_float($value)) {
64
} elseif (is_array($value)) {
66
} elseif (is_resource($value)) {
68
} elseif (is_object($value)) {
75
* Creates a human readable description of the
76
* difference between two variables. Uses a
78
* @param mixed $first First variable.
79
* @param mixed $second Value to compare with.
80
* @param boolean $identical If true then type anomolies count.
81
* @return string Description of difference.
84
function describeDifference($first, $second, $identical = false) {
86
if (! $this->_isTypeMatch($first, $second)) {
87
return "with type mismatch as [" . $this->describeValue($first) .
88
"] does not match [" . $this->describeValue($second) . "]";
91
$type = $this->getType($first);
92
if ($type == "Unknown") {
93
return "with unknown type";
95
$method = '_describe' . $type . 'Difference';
96
return $this->$method($first, $second, $identical);
100
* Tests to see if types match.
101
* @param mixed $first First variable.
102
* @param mixed $second Value to compare with.
103
* @return boolean True if matches.
106
function _isTypeMatch($first, $second) {
107
return ($this->getType($first) == $this->getType($second));
111
* Clips a string to a maximum length.
112
* @param string $value String to truncate.
113
* @param integer $size Minimum string size to show.
114
* @param integer $position Centre of string section.
115
* @return string Shortened version.
118
function clipString($value, $size, $position = 0) {
119
$length = strlen($value);
120
if ($length <= $size) {
123
$position = min($position, $length);
124
$start = ($size/2 > $position ? 0 : $position - $size/2);
125
if ($start + $size > $length) {
126
$start = $length - $size;
128
$value = substr($value, $start, $size);
129
return ($start > 0 ? "..." : "") . $value . ($start + $size < $length ? "..." : "");
133
* Creates a human readable description of the
134
* difference between two variables. The minimal
136
* @param null $first First value.
137
* @param mixed $second Value to compare with.
138
* @return string Human readable description.
141
function _describeGenericDifference($first, $second) {
142
return "as [" . $this->describeValue($first) .
143
"] does not match [" .
144
$this->describeValue($second) . "]";
148
* Creates a human readable description of the
149
* difference between a null and another variable.
150
* @param null $first First null.
151
* @param mixed $second Null to compare with.
152
* @param boolean $identical If true then type anomolies count.
153
* @return string Human readable description.
156
function _describeNullDifference($first, $second, $identical) {
157
return $this->_describeGenericDifference($first, $second);
161
* Creates a human readable description of the
162
* difference between a boolean and another variable.
163
* @param boolean $first First boolean.
164
* @param mixed $second Boolean to compare with.
165
* @param boolean $identical If true then type anomolies count.
166
* @return string Human readable description.
169
function _describeBooleanDifference($first, $second, $identical) {
170
return $this->_describeGenericDifference($first, $second);
174
* Creates a human readable description of the
175
* difference between a string and another variable.
176
* @param string $first First string.
177
* @param mixed $second String to compare with.
178
* @param boolean $identical If true then type anomolies count.
179
* @return string Human readable description.
182
function _describeStringDifference($first, $second, $identical) {
183
if (is_object($second) || is_array($second)) {
184
return $this->_describeGenericDifference($first, $second);
186
$position = $this->_stringDiffersAt($first, $second);
187
$message = "at character $position";
188
$message .= " with [" .
189
$this->clipString($first, 200, $position) . "] and [" .
190
$this->clipString($second, 200, $position) . "]";
195
* Creates a human readable description of the
196
* difference between an integer and another variable.
197
* @param integer $first First number.
198
* @param mixed $second Number to compare with.
199
* @param boolean $identical If true then type anomolies count.
200
* @return string Human readable description.
203
function _describeIntegerDifference($first, $second, $identical) {
204
if (is_object($second) || is_array($second)) {
205
return $this->_describeGenericDifference($first, $second);
207
return "because [" . $this->describeValue($first) .
209
$this->describeValue($second) . "] by " .
210
abs($first - $second);
214
* Creates a human readable description of the
215
* difference between two floating point numbers.
216
* @param float $first First float.
217
* @param mixed $second Float to compare with.
218
* @param boolean $identical If true then type anomolies count.
219
* @return string Human readable description.
222
function _describeFloatDifference($first, $second, $identical) {
223
if (is_object($second) || is_array($second)) {
224
return $this->_describeGenericDifference($first, $second);
226
return "because [" . $this->describeValue($first) .
228
$this->describeValue($second) . "] by " .
229
abs($first - $second);
233
* Creates a human readable description of the
234
* difference between two arrays.
235
* @param array $first First array.
236
* @param mixed $second Array to compare with.
237
* @param boolean $identical If true then type anomolies count.
238
* @return string Human readable description.
241
function _describeArrayDifference($first, $second, $identical) {
242
if (! is_array($second)) {
243
return $this->_describeGenericDifference($first, $second);
245
if (! $this->_isMatchingKeys($first, $second, $identical)) {
246
return "as key list [" .
247
implode(", ", array_keys($first)) . "] does not match key list [" .
248
implode(", ", array_keys($second)) . "]";
250
foreach (array_keys($first) as $key) {
251
if ($identical && ($first[$key] === $second[$key])) {
254
if (! $identical && ($first[$key] == $second[$key])) {
257
return "with member [$key] " . $this->describeDifference(
266
* Compares two arrays to see if their key lists match.
267
* For an identical match, the ordering and types of the keys
269
* @param array $first First array.
270
* @param array $second Array to compare with.
271
* @param boolean $identical If true then type anomolies count.
272
* @return boolean True if matching.
275
function _isMatchingKeys($first, $second, $identical) {
276
$first_keys = array_keys($first);
277
$second_keys = array_keys($second);
279
return ($first_keys === $second_keys);
283
return ($first_keys == $second_keys);
287
* Creates a human readable description of the
288
* difference between a resource and another variable.
289
* @param resource $first First resource.
290
* @param mixed $second Resource to compare with.
291
* @param boolean $identical If true then type anomolies count.
292
* @return string Human readable description.
295
function _describeResourceDifference($first, $second, $identical) {
296
return $this->_describeGenericDifference($first, $second);
300
* Creates a human readable description of the
301
* difference between two objects.
302
* @param object $first First object.
303
* @param mixed $second Object to compare with.
304
* @param boolean $identical If true then type anomolies count.
305
* @return string Human readable description.
308
function _describeObjectDifference($first, $second, $identical) {
309
if (! is_object($second)) {
310
return $this->_describeGenericDifference($first, $second);
312
return $this->_describeArrayDifference(
313
get_object_vars($first),
314
get_object_vars($second),
319
* Find the first character position that differs
320
* in two strings by binary chop.
321
* @param string $first First string.
322
* @param string $second String to compare with.
323
* @return integer Position of first differing
327
function _stringDiffersAt($first, $second) {
328
if (! $first || ! $second) {
331
if (strlen($first) < strlen($second)) {
332
list($first, $second) = array($second, $first);
335
$step = strlen($first);
337
$step = (integer)(($step + 1) / 2);
338
if (strncmp($first, $second, $position + $step) == 0) {
346
* Sends a formatted dump of a variable to a string.
347
* @param mixed $variable Variable to display.
348
* @return string Output from print_r().
352
function dump($variable) {
355
$formatted = ob_get_contents();
361
* Extracts the last assertion that was not within
362
* Simpletest itself. The name must start with "assert".
363
* @param array $stack List of stack frames.
367
function getFormattedAssertionLine($stack) {
368
foreach ($stack as $frame) {
369
if (isset($frame['file'])) {
370
if (strpos($frame['file'], SIMPLE_TEST) !== false) {
371
if (dirname($frame['file']) . '/' == SIMPLE_TEST) {
376
if (SimpleDumper::_stackFrameIsAnAssertion($frame)) {
377
return ' at [' . $frame['file'] . ' line ' . $frame['line'] . ']';
384
* Tries to determine if the method call is an assertion.
385
* @param array $frame PHP stack frame.
389
function _stackFrameIsAnAssertion($frame) {
390
if (($frame['function'] == 'fail') || ($frame['function'] == 'pass')) {
393
if (strncmp($frame['function'], 'assert', 6) == 0) {
396
if (strncmp($frame['function'], 'expect', 6) == 0) {
b'\\ No newline at end of file'