-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphptest.php
More file actions
740 lines (636 loc) · 16.6 KB
/
phptest.php
File metadata and controls
740 lines (636 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
<?php
/******************************************************************************
Module : phpTest. cxxTest clone for PHP
File : phptest.php
Author : Alexei V. Vasilyev
-----------------------------------------------------------------------------
Description: cxxTest clone for PHP. see samples.
******************************************************************************/
//show information about running test
global $PHPTEST_VERBOSE;
$PHPTEST_VERBOSE=false;
define( 'PHPTEST_EXCEPTIONS', version_compare(PHP_VERSION, '5.0.0', '>='));
class PhpTest_AssertException extends Exception
{
}
class PhpTest_TestSuite
{
function setUp()
{
}
function tearDown()
{
}
function suiteStart()
{
}
function suiteEnd()
{
}
//new methods
function suiteSetUp()
{
$this->suiteStart();
}
function suiteTearDown()
{
$this->suiteEnd();
}
/**
* Assert that $x == $y
* @param mixed $x X argument
* @param mixed $y Y argument
* @param string $m message which will be display onm assertion fail.
* @return bool true on success.
*/
function assertEquals( $x, $y, $m=null)
{
TS_ASSERT_EQUALS($x, $y, $m);
}
/**
* Assert that $x != $y
* @param mixed $x X argument
* @param mixed $y Y argument
* @param string $m message which will be display onm assertion fail.
* @return bool true on success.
*/
function assertDiffers( $x, $y, $m=null)
{
TS_ASSERT_DIFFERS($x, $y, $m);
}
/**
* Assert that $x != $y
* @param mixed $x X argument
* @param mixed $y Y argument
* @param string $m message which will be display onm assertion fail.
* @return bool true on success.
*/
function assertNotEquals( $x, $y, $m=null)
{
$this->assertDiffers($x, $y, $m);
}
/**
* Assert that $x > $y
* @param mixed $x X argument
* @param mixed $y Y argument
* @param string $m message which will be display onm assertion fail.
* @return bool true on success.
*/
function assertGreateThen($x, $y, $m=null)
{
TS_ASSERT_GREATER($x, $y, $m);
}
/**
* Assert that $x >= $y
* @param mixed $x X argument
* @param mixed $y Y argument
* @param string $m message which will be display onm assertion fail.
* @return bool true on success.
*/
function assertGreateOrEquals( $x, $y, $m=null )
{
TS_ASSERT_GREATER_OR_EQUALS($x, $y, $m);
}
/**
* Assert that $x < $y
* @param mixed $x X argument
* @param mixed $y Y argument
* @param string $m message which will be display onm assertion fail.
* @return bool true on success.
*/
function assertLessThen( $x, $y, $m=null)
{
TS_ASSERT_LESS_THEN($x, $y, $m);
}
/**
* Assert that $x <= $y
* @param mixed $x X argument
* @param mixed $y Y argument
* @param string $m message which will be display onm assertion fail.
* @return bool true on success.
*/
function assertLessOrEquals( $x, $y, $m=null)
{
TS_ASSERT_LESSER_OR_EQUALS($x, $y, $m);
}
/**
* assert that $x contains $y
*
* @param string $x X argument
* @param string $y Y argument
* @param string $msg message which will be displayed on assertion fail.
*/
function assertContains( $x, $y, $msg=null ) {
TS_ASSERT_CONTAINS($x, $y, $msg);
}
};
define( "CLASS_PhpTest_TestSuite", get_class( new PhpTest_TestSuite() ));
class PhpTest_Output
{
var $nMessage = 0;
var $isVerboseMode = false;
function PhpTest_Output()
{
if ( array_key_exists( "argv", $_SERVER )) {
define( "CR", "\n" );
define( "PARA", "" );
define( "B1", "" );
define( "B2", "" );
}
else {
define( "PARA", "<pre>" );
define( "CR", "<br>" );
define( "B1", "<b>" );
define( "B2", "</b>" );
}
global $PHPTEST_VERBOSE;
$this->isVerboseMode = $PHPTEST_VERBOSE;
}
function printmsg( $msg, $endl = CR )
{
print( "$msg$endl" );
}
function begin( $total )
{
$this->printmsg( sprintf( PARA . "Running %d tests", $total), "" );
}
function end( $total, $nFailed )
{
if ( $nFailed == 0 ) {
if ( $this->isVerboseMode) {
$this->printmsg( CR. "Success rate: 100%", CR . CR);
}
else {
$this->printmsg( "OK");
}
return;
}
else {
$this->printmsg( CR. CR. "$nFailed of $total failed");
}
$this->printmsg( sprintf( "Success rate: %d%%", (($total - $nFailed ) / $total ) * 100 ) );
}
function printError( $file, $line, $suite, $test, $msg )
{
$this->trace( $file, $line, $suite, $test, $msg, "Assertion failed" );
}
function trace( $file, $line, $suite, $test, $msg, $alert="Trace" )
{
if ( $this->nMessage ==0 ) {
$this->printmsg( CR . "In $suite::$test");
}
$this->printmsg( "$file:$line: $alert: $msg");
$this->nMessage++;
}
function testBegin( $suite, $test )
{
$this->nMessage = 0;
if ( $this->isVerboseMode) {
$this->printmsg( CR . "$suite::$test: ", "" );
}
//dbg( "\n$suite::$test: " );
}
function testPassed()
{
if ( $this->isVerboseMode) {
$this->printmsg( "OK" , "");
}
else {
print( "." );
}
}
function testFailed()
{
print( "F" );
}
public function testIgnored($suite, $test)
{
if ( $this->isVerboseMode) {
$this->printmsg( CR . "$suite::$test: IGNORED", "" );
}
else {
$this->printmsg( "Ignored: $suite::$test" );
}
}
function gettype( $var )
{
if ( is_string( $var ) ) return "string";
if ( is_bool( $var ) ) return "bool";
if ( is_int( $var ) ) return "int";
if ( is_null( $var ) ) return "null";
return gettype( $var );
}
function getReportExpect( $op, $x, $y, $msg=null )
{
$xtype= $this->gettype( $x );
$ytype = $this->gettype( $y );
$x = var_export($x, true);
$y = var_export($y, true);
$display = "x $op y";
$display .= (( $msg ) ? "\nMessage : $msg" : "") . CR;
$display .= "Expect x ($xtype):<$x>" . CR;
$display .= " Was y ($ytype):<$y>";
return( $display );
}
function getReportExpectNot( $op, $x, $y, $msg=null )
{
$xtype= $this->gettype( $x );
$ytype = $this->gettype( $y );
$x = var_export($x, true);
$y = var_export($y, true);
$display = "x $op y";
$display .= (( $msg ) ? "\nMessage : $msg" : "") . CR;
$display .= "Expect not x($xtype):<$x>" . CR;
$display .= " Was y($ytype):<$y>";
return( $display );
}
function getReportXY( $op, $x, $y, $msg=null )
{
$x = var_export($x, true);
$y = var_export($y, true);
$display = "x $op y";
$display .= (( $msg ) ? "\nMessage : $msg" : "") . CR;
$display .= " x:<$x>" . CR;
$display .= " y:<$y>";
return( $display );
}
};
class phpTest
{
var $total = 0; //totalTests
var $testCount =0 ; //nFailedAsserts (for current test)
var $nFailed = 0; //nFailedTests (total)
var $output = null;
var $suites = array();
var $tests = array();
var $ignoredTests = array();
//current test
var $suiteName;
var $testName;
var $verboseOutput = false;
function PhpTest()
{
$this->output = new PhpTest_Output();
global $PHPTEST_VERBOSE;
$this->verboseOutput = $PHPTEST_VERBOSE;
}
function getTestNames( $args)
{
$result = array();
foreach( $args as $name ) {
if( strncmp( $name, "xtest", 5 ) == 0 ) {
$this->ignoredTests[] = $name;
}
if( strncmp( $name, "test", 4 ) != 0 ) continue;
array_push( $result, $name );
}
return( $result );
}
function getSuiteNames( $args )
{
$result = array();
foreach( $args as $name ) {
if( strncmp( $name, "test", 4 ) != 0 ) continue;
if ( get_parent_class( new $name ) != CLASS_PhpTest_TestSuite ) continue;
array_push( $result, $name );
}
return( $result );
}
function scanTests()
{
$this->suites = PhpTest::getSuiteNames( get_declared_classes() );
foreach( $this->suites as $suite ) {
$tests = PhpTest::getTestNames( get_class_methods( $suite ) );
foreach( $this->ignoredTests as $name ) {
$this->output->testIgnored($suite, $name );
}
$this->ignoredTests = array();
$this->tests[ $suite ]= $tests; //??? for future use
$this->total += count( $tests );
}
}
function doSuite( $suite )
{
//suite
$methods = get_class_methods( $suite );
$tests = PhpTest::getTestNames( $methods );
$obj = new $suite;
$obj->suiteSetUp();
foreach ( $tests as $test ) {
$this->enterTest( $test );
$obj->setUp($test);
//print( "<li>$suite::$test<br>");
if ( PHPTEST_EXCEPTIONS) {
try {
$obj->$test();
}
catch( PhpTest_AssertException $e){
}
}
else {
$obj->$test();
}
$obj->tearDown();
$this->leaveTest();
}
$obj->suiteTearDown();
unset( $obj );
}
function doWorld()
{
$this->scanTests();
$this->enterWorld();
//world
foreach( $this->suites as $suite ) {
$this->enterSuite( $suite );
$this->doSuite( $suite );
$this->leaveSuite();
}
$this->leaveWorld();
}
function enterWorld()
{
$this->output->begin( $this->total );
}
function leaveWorld()
{
$this->output->end( $this->total, $this->nFailed );
}
function enterSuite( $suiteName )
{
$this->suiteName = $suiteName;
}
function leaveSuite()
{
}
function enterTest( $testName )
{
$this->testName = $testName;
$this->testCount = 0;
$this->output->testBegin( $this->suiteName, $this->testName );
}
function leaveTest()
{
if ( $this->testCount == 0 ) {
$this->output->testPassed();
}
else {
$this->nFailed++;
$this->output->testFailed();
}
}
function getClass( $stackInfo )
{
if ( array_key_exists( "class", $stackInfo) ) return $stackInfo["class"];
return "__global__";
}
function getTrace( $trace )
{
while( $trace[0]['file'] == __FILE__ ){
array_shift( $trace );
};
$source1 = $trace[0];
$source2 = $trace[1];
$val = array(
"file" => $source1["file"],
"line" => $source1["line"],
"class" => phpTest::getClass($source2),
"function" => $source2["function"],
"from_file" => $source2["file"],
"from_line" => $source2["line"],
);
return( $val );
}
function assertionFailed( $msg )
{
$this->testCount++;
$trace = PhpTest::getTrace( debug_backtrace() );
$this->output->printError( $trace["file"], $trace["line"], $this->suiteName, $this->testName, $msg); //$trace["class"], $trace["function"]
//reuse validators support
$this->printTrace();
$this->output->printmsg( "" );
if ( PHPTEST_EXCEPTIONS) {
throw new PhpTest_AssertException("assertion failed");
}
return( true );
}
function printTrace()
{
$bt = debug_backtrace();
$this->output->printmsg( "Trace:" );
for( $i=0; $i < count( $bt ); $i++ ) {
$point = &$bt[$i];
$next = &$bt[$i+1];
if ( !array_key_exists( "class", $next ) ) $next["class"]=null;
if ( $point["file"] === __FILE__ ) continue;
$this->output->printmsg( sprintf( "%s:%d: %s::%s()", $point[ "file" ], $point[ "line" ], $next["class"], $next['function'] ) );
$pclass = get_parent_class( $next["class"] );
if ( $pclass === CLASS_PhpTest_TestSuite && strpos($next['function'], "test")===0 ) {
break;
}
}
}
function trace( $msg, $alert="Trace", $level=0)
{
$trace = PhpTest::getTrace( debug_backtrace() );
$this->output->trace( $trace["file"], $trace["line"], $trace["class"], $trace["function"], $msg, $alert);
}
function isTestFailed()
{
return( $this->testCount != 0 );
}
static function doScanFiles()
{
//print( "PhpTest autoload : " );
if ($handle = opendir('.')) {
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
$file = trim( $file );
if ( strncmp( $file, "test", 4 ) == 0 &&
substr( $file, strlen($file) - 4 ) == ".php" ) {
//print ("$file\n " );
require_once( $file );
}
}
closedir($handle);
}
else {
print "Error: can't open directory .";
}
}
/** Main function. static
*/
static function run( $signScanFiles = false )
{
if ( $signScanFiles ) {
PhpTest::doScanFiles();
}
global $_phpTestObject;
$_phpTestObject = new PhpTest;
$_phpTestObject->doWorld();
}
};
function TS_ASSERT( $arg, $msg=null )
{
global $_phpTestObject;
if ( $arg == FALSE ) return( $_phpTestObject->assertionFailed( "$arg" . (( $msg ) ? "\n\tMessage: " . $msg : "")) );
return( false );
}
function TS_ASSERT_EQUALS( $x, $y, $msg=null )
{
global $_phpTestObject;
if ( (is_array( $x ) || is_object( $x )) || ( is_array( $y ) || is_object( $y ) ) ) {
return TS_ASSERT_EQUALS_ARRAY( $x, $y, $msg );
}
if ( $x !== $y ) {
$display = $_phpTestObject->output->getReportExpect( "===", $x, $y, $msg );
return $_phpTestObject->assertionFailed( $display );
}
return( false );
}
function TS_ASSERT_DIFFERS( $x, $y, $msg="" )
{
global $_phpTestObject;
if ( (is_array( $x ) || is_object( $x )) || ( is_array( $y ) || is_object( $y ) ) ) {
return TS_ASSERT_DIFFERS_ARRAY( $x, $y, $msg );
}
if ( $x === $y ) {
$display = $_phpTestObject->output->getReportExpectNot( "!==", $x, $y, $msg );
return $_phpTestObject->assertionFailed( $display );//"'$x' !== '$y'" . ( $msg != "" ) ? " : " . $msg : "" );
}
return( false );
}
/**
* Assert that $x > $y
* @global phpTest $_phpTestObject
* @param mixed $x
* @param mixed $y
* @param string $msg
* @return bool true on success.
*/
function TS_ASSERT_GREATER( $x, $y, $msg=null )
{
global $_phpTestObject;
if ( !( $x > $y ) ) {
$display = $_phpTestObject->output->getReportExpect( ">", $x, $y, $msg );
return $_phpTestObject->assertionFailed( $display );
}
return( false );
}
function TS_ASSERT_GREATER_OR_EQUALS( $x, $y, $msg=null )
{
global $_phpTestObject;
if ( !( $x >= $y ) ) {
$display = $_phpTestObject->output->getReportExpect( ">=", $x, $y, $msg );
return $_phpTestObject->assertionFailed( $display );
}
return( false );
}
function TS_ASSERT_LESS_THEN( $x, $y, $msg=null )
{
global $_phpTestObject;
if ( !( $x < $y ) ) {
$display = $_phpTestObject->output->getReportExpect( "<", $x, $y, $msg );
return $_phpTestObject->assertionFailed( $display );
}
return( false );
}
function TS_ASSERT_LESSER_OR_EQUALS( $x, $y, $msg=null )
{
global $_phpTestObject;
if ( !( $x <= $y ) ) {
$display = $_phpTestObject->output->getReportExpect( "<=", $x, $y, $msg );
return $_phpTestObject->assertionFailed( $display );
}
return( false );
}
/**
* Assert that $x contains $y
*
* @param string $x
* @param string $y
* @param string $msg
* @return bool assert result
*/
function TS_ASSERT_CONTAINS( $x, $y, $msg="" )
{
global $_phpTestObject;
//print_r( func_get_arg() );
if ( FALSE === strpos( $x, $y ) ) {
$display = $_phpTestObject->output->getReportXY( "contains", $x, $y, $msg );
return $_phpTestObject->assertionFailed( $display );
}
return( false );
}
function TS_FAIL( $msg="" )
{
global $_phpTestObject;
return( $_phpTestObject->assertionFailed( "" . (( $msg ) ? "\n\tMessage: " . $msg : "")) );
}
function TS_ASSERT_NOT_CONTAINS( $x, $y, $msg="" )
{
global $_phpTestObject;
//print_r( func_get_arg() );
if ( FALSE !== strpos( $x, $y ) ) {
$display = $_phpTestObject->output->getReportXY( "not contains", $x, $y, $msg );
return $_phpTestObject->assertionFailed( $display );
}
return( false );
}
function TS_ISFAILED()
{
global $_phpTestObject;
return( $_phpTestObject->isTestFailed() );
}
function TS_TRACE( $msg )
{
global $_phpTestObject;
return( $_phpTestObject->trace( $msg ) );
}
/**
* Assert equals array
*
* @param array $x
* @param array $y
* @param string $msg
*/
function TS_ASSERT_EQUALS_ARRAY( $x, $y, $msg="")
{
ob_start();
print_r( $x );
$strx = ob_get_clean();
ob_start();
print_r( $y );
$stry = ob_get_clean();
if ( $strx != $stry && function_exists( 'xdiff_string_diff') ) {
$diff = "diff: " .xdiff_string_diff( $strx, $stry );
}
else {
$diff = "";
}
TS_ASSERT_EQUALS( $strx, $stry, $msg . $diff);
}
/**
* Assert equals array
*
* @param array $x
* @param array $y
* @param string $msg
*/
function TS_ASSERT_DIFFERS_ARRAY( $x, $y, $msg="")
{
ob_start();
print_r( $x );
$strx = ob_get_clean();
ob_start();
print_r( $y );
$stry = ob_get_clean();
if ( $strx != $stry && function_exists( 'xdiff_string_diff') ) {
$diff = "diff: " .xdiff_string_diff( $strx, $stry );
}
else {
$diff = "";
}
TS_ASSERT_DIFFERS( $strx, $stry, $msg . $diff);
}
?>