Permalink
Newer
Older
100644 415 lines (389 sloc) 12.8 KB
1
<?php
2
@include_once 'Text/Diff.php';
3
@include_once 'Text/Diff/Renderer.php';
4
@include_once 'Text/Diff/Renderer/unified.php';
5
require_once 'PEAR/ErrorStack.php';
6
require_once 'PEAR.php';
7
class PEAR_PHPTest
8
{
9
var $_diffonly;
10
var $_errors;
11
function PEAR_PHPTest($diffonly = false, $noStackCatch = false)
12
{
13
$this->_diffonly = $diffonly;
14
$this->_errors = array();
Mar 6, 2012
15
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'pearerrorCallback'));
Mar 6, 2012
17
PEAR_ErrorStack::setDefaultCallback(array($this, 'pearerrorstackCallback'));
18
}
19
}
20
21
function pearerrorCallback($err)
22
{
23
PEAR_ErrorStack::staticPush('PEAR_Error', -1, 'error', array('obj' => $err),
24
$err->getMessage());
25
}
26
27
function pearerrorstackCallback($err)
28
{
29
$this->_errors[] = $err;
30
}
31
32
function assertPEARError($err, $message)
33
{
34
if (is_a($err, 'PEAR_Error')) {
35
return true;
36
}
37
$this->_failTest(debug_backtrace(), $message);
38
echo "Not a PEAR_Error\n";
39
return false;
40
}
41
42
function assertNoErrors($message, $trace = null)
43
{
44
if (count($this->_errors) == 0) {
45
return true;
46
}
47
if ($trace === null) {
48
$trace = debug_backtrace();
49
}
50
$this->_failTest($trace, $message);
51
foreach ($this->_errors as $err) {
52
if ($err['package'] == 'PEAR_Error') {
53
echo "Unexpected PEAR_Error:\n";
54
echo 'message "' . $err['message'] . "\"\n";
55
} else {
56
echo "Unexpected PEAR_ErrorStack error:\n";
57
echo 'package "' . $err['package'] . "\"\n";
58
echo 'message "' . $err['message'] . "\"\n";
59
}
60
}
61
$this->_errors = array();
62
return false;
63
}
64
65
function assertErrors($errors, $message, $trace = null)
66
{
67
if (!count($this->_errors)) {
68
if ($trace === null) {
69
$trace = debug_backtrace();
70
}
71
$this->_failTest($trace, $message);
72
echo "No errors caught, but errors were expected\n";
73
return false;
74
}
75
if (!isset($errors[0])) {
76
$errors = array($errors);
77
}
78
$failed = false;
79
foreach ($errors as $err) {
80
$found = false;
81
foreach ($this->_errors as $i => $caughterror) {
82
if ($caughterror['package'] == $err['package']) {
83
if ($caughterror['message'] == $err['message']) {
84
$found = true;
85
break;
86
}
87
}
88
}
89
if ($found) {
90
unset($this->_errors[$i]);
91
continue;
92
}
93
if (!$failed) {
94
if ($trace === null) {
95
$trace = debug_backtrace();
96
}
97
$failed = true;
98
$this->_failTest($trace, $message);
99
}
100
echo "Unthrown error:\n";
101
if ($err['package'] == 'PEAR_Error') {
102
echo "PEAR_Error:\n";
103
} else {
104
echo "error package: \"$err[package]\"\n";
105
}
106
echo "message: \"$err[message]\"\n";
107
}
108
if (count($this->_errors)) {
109
if (!$failed) {
110
if ($trace === null) {
111
$trace = debug_backtrace();
112
}
113
$failed = true;
114
$this->_failTest($trace, $message);
115
}
116
foreach ($this->_errors as $err) {
117
echo "Unexpected error:\n";
118
if ($err['package'] == 'PEAR_Error') {
119
echo "PEAR_Error:\n";
120
} else {
121
echo "error package: \"$err[package]\"\n";
122
}
123
echo "message: \"$err[message]\"\n";
124
}
125
}
126
$this->_errors = array();
127
return !$failed;
128
}
129
130
function assertTrue($test, $message)
131
{
132
$this->assertNoErrors($message, debug_backtrace());
133
if ($test === true) {
134
return true;
135
}
136
$this->_failTest(debug_backtrace(), $message);
137
echo "Unexpected non-true value: \n";
138
var_export($test);
139
echo "\n'$message'\n";
140
return false;
141
}
142
143
function assertIsa($control, $test, $message)
144
{
145
$this->assertNoErrors($message, debug_backtrace());
146
if (is_a($test, $control)) {
147
return true;
148
}
149
$this->_failTest(debug_backtrace(), $message);
150
echo "Unexpected non-$control object: \n";
151
var_export($test);
152
echo "\n'$message'\n";
153
return false;
154
}
155
156
function assertNull($test, $message)
157
{
158
$this->assertNoErrors($message, debug_backtrace());
159
if ($test === null) {
160
return true;
161
}
162
$this->_failTest(debug_backtrace(), $message);
163
echo "Unexpected non-null value: \n";
164
var_export($test);
165
echo "\n'$message'\n";
166
return false;
167
}
168
169
function assertNotNull($test, $message)
170
{
171
$this->assertNoErrors($message, debug_backtrace());
172
if ($test !== null) {
173
return true;
174
}
175
$this->_failTest(debug_backtrace(), $message);
176
echo "Unexpected null: \n";
177
var_export($test);
178
echo "\n'$message'\n";
179
return false;
180
}
181
182
function assertSame($test, $test1, $message)
183
{
184
$this->assertNoErrors($message, debug_backtrace());
185
if ($test === $test1) {
186
return true;
187
}
188
$this->_failTest(debug_backtrace(), $message);
189
echo "Unexpectedly two vars are not the same thing: \n";
190
echo "\n'$message'\n";
191
return false;
192
}
193
194
function assertNotSame($test, $test1, $message)
195
{
196
$this->assertNoErrors($message, debug_backtrace());
197
if ($test !== $test1) {
198
return true;
199
}
200
$this->_failTest(debug_backtrace(), $message);
201
echo "Unexpectedly two vars are the same thing: \n";
202
echo "\n'$message'\n";
203
return false;
204
}
205
206
function assertFalse($test, $message)
207
{
208
$this->assertNoErrors($message, debug_backtrace());
209
if ($test === false) {
210
return true;
211
}
212
$this->_failTest(debug_backtrace(), $message);
213
echo "Unexpected non-false value: \n";
214
var_export($test);
215
echo "\n'$message'\n";
216
return false;
217
}
218
219
function assertNotTrue($test, $message)
220
{
221
$this->assertNoErrors($message, debug_backtrace());
222
if (!$test) {
223
return true;
224
}
225
$this->_failTest(debug_backtrace(), $message);
226
echo "Unexpected loose true value: \n";
227
var_export($test);
228
echo "\n'$message'\n";
229
return false;
230
}
231
232
function assertNotFalse($test, $message)
233
{
234
$this->assertNoErrors($message, debug_backtrace());
235
if ($test) {
236
return true;
237
}
238
$this->_failTest(debug_backtrace(), $message);
239
echo "Unexpected loose false value: \n";
240
var_export($test);
241
echo "\n'$message'\n";
242
return false;
243
}
244
245
function assertEquals($control, $test, $message)
246
{
247
$this->assertNoErrors($message, debug_backtrace());
248
if (str_replace(array("\r", "\n"), array('', ''),
249
var_export($control, true)) != str_replace(array("\r", "\n"), array('', ''),
250
var_export($test, true))) {
251
$this->_failTest(debug_backtrace(), $message);
252
if (class_exists('Text_Diff')) {
253
echo "Diff of expecting/received:\n";
Mar 6, 2012
254
$diff = new Text_Diff(
255
explode("\n", var_export($control, true)),
256
explode("\n", var_export($test, true)));
257
258
// Output the diff in unified format.
Mar 6, 2012
259
$renderer = new Text_Diff_Renderer_unified();
260
echo $renderer->render($diff);
261
if ($this->_diffonly) {
262
return false;
263
}
264
}
265
echo "Expecting:\n";
266
var_export($control);
267
echo "\nReceived:\n";
268
var_export($test);
269
echo "\n";
270
return false;
271
}
272
return true;
273
}
274
275
function assertFileExists($fname, $message)
276
{
277
$this->assertNoErrors($message, debug_backtrace());
278
if (!@file_exists($fname)) {
279
$this->_failTest(debug_backtrace(), $message);
280
echo "File '$fname' does not exist, and should\n";
281
return false;
282
}
283
return true;
284
}
285
286
function assertFileNotExists($fname, $message)
287
{
288
$this->assertNoErrors($message, debug_backtrace());
289
if (@file_exists($fname)) {
290
$this->_failTest(debug_backtrace(), $message);
291
echo "File '$fname' exists, and should not\n";
292
return false;
293
}
294
return true;
295
}
296
Mar 6, 2012
297
function assertRegEquals($dump, $reg, $message)
298
{
299
$actualdump = var_export(trim($this->dumpReg($reg)), true);
300
$testdump = var_export(trim($dump), true);
301
return $this->assertEquals($testdump, $actualdump, $message);
302
}
303
304
function assertPackageInfoEquals($control, $test, $message)
305
{
306
$this->assertNoErrors($message, debug_backtrace());
307
if (isset($control[0])) {
308
if (!isset($test[0]) || (count($control) != count($test))) {
309
echo "Invalid packageInfo\n";
310
$ret = $this->assertEquals($control, $test, $message);
311
}
312
$ret = true;
313
foreach ($control as $i => $packageinfo) {
314
$ret = $ret &&
315
$this->assertPackageInfoEquals($packageinfo, $test[$i], $message . $i);
316
}
317
return $ret;
318
}
319
if (isset($control['_lastmodified'])) {
320
if (!isset($test['_lastmodified'])) {
321
echo "_lastmodified is not set in packageInfo() output\n";
322
$this->_failTest(debug_backtrace(), $message);
323
return false;
324
}
325
}
326
$savecontrol = $control;
327
$savetest = $test;
328
unset($control['_lastmodified']);
329
unset($test['_lastmodified']);
330
if (var_export($control, true) != var_export($test, true)) {
331
$this->_failTest(debug_backtrace(), $message);
332
if (class_exists('Text_Diff')) {
333
echo "Diff of expecting/received:\n";
Mar 6, 2012
334
$diff = new Text_Diff(
335
explode("\n", var_export($control, true)),
336
explode("\n", var_export($test, true)));
337
338
// Output the diff in unified format.
Mar 6, 2012
339
$renderer = new Text_Diff_Renderer_unified();
340
echo $renderer->render($diff);
341
if ($this->_diffonly) {
342
return false;
343
}
344
}
345
echo "Expecting:\n";
346
var_export($savecontrol);
347
echo "\nReceived:\n";
348
var_export($savetest);
349
return false;
350
}
351
return true;
352
}
353
354
function _sortRegEntries($a, $b)
355
{
356
return strnatcasecmp($a['name'], $b['name']);
357
}
358
Mar 6, 2012
359
function dumpReg($reg)
360
{
361
ob_start();
362
print "dumping registry...\n";
363
$infos = $reg->packageInfo(null, null, null);
364
ksort($infos);
365
foreach ($infos as $channel => $info) {
366
echo "channel $channel:\n";
367
usort($info, array($this, '_sortRegEntries'));
368
foreach ($info as $pkg) {
369
print $pkg["name"] . ":";
370
unset($pkg["name"]);
371
foreach ($pkg as $k => $v) {
372
if ($k == '_lastmodified') {
373
print " _lastmodified is set";
374
continue;
375
}
376
if (is_array($v) && $k == 'filelist') {
377
print " $k=array(";
378
$i = 0;
379
foreach ($v as $k2 => $v2) {
380
if ($i++ > 0) print ",";
381
print "{$k2}[";
382
$j = 0;
383
foreach ($v2 as $k3 => $v3) {
384
if ($j++ > 0) print ",";
385
print "$k3=$v3";
386
}
387
print "]";
388
}
389
print ")";
390
} else {
391
print " $k=\"$v\"";
392
}
393
}
394
print "\n";
395
}
396
}
397
print "dump done\n";
398
$ret = ob_get_contents();
399
ob_end_clean();
400
return $ret;
401
}
402
403
function _failTest($trace, $message)
404
{
405
echo 'Test Failure: "' . $message . "\"\n in " . $trace[0]['file'] . ' line ' .
406
$trace[0]['line'] . "\n";
407
}
408
409
function showAll()
410
{
411
$this->_diffonly = false;
412
}
413
}
Mar 6, 2012
414
?>