5b494805bdecd893f8664c919f2458129962e5b8
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserHelpers.php
1 <?php
2
3 class PHPUnitParserTest extends ParserTest {
4 function showTesting( $desc ) {
5 global $additionalMWCLIArgs;
6 if( $additionalMWCLIArgs['verbose'] ) parent::showTesting( $desc );
7 /* Do nothing since we don't want to show info during PHPUnit testing. */
8 }
9
10 public function showSuccess( $desc ) {
11 global $additionalMWCLIArgs;
12 PHPUnit_Framework_Assert::assertTrue( true, $desc );
13 if( $additionalMWCLIArgs['verbose'] ) parent::showSuccess( $desc );
14 return true;
15 }
16
17 public function showFailure( $desc, $expected, $got ) {
18 global $additionalMWCLIArgs;
19 PHPUnit_Framework_Assert::assertEquals( $expected, $got, $desc );
20 if( $additionalMWCLIArgs['verbose'] ) parent::showFailure( $desc, $expected, $got );
21 return false;
22 }
23
24 public function setupRecorder( $options ) {
25 $this->recorder = new PHPUnitTestRecorder( $this );
26 }
27 }
28
29 class ParserUnitTest extends MediaWikiTestCase {
30 private $test = "";
31
32 public function __construct( $suite, $test = null ) {
33 parent::__construct();
34 $this->test = $test;
35 $this->suite = $suite;
36 }
37
38 function count() { return 1; }
39
40 public function run( PHPUnit_Framework_TestResult $result = null ) {
41 PHPUnit_Framework_Assert::resetCount();
42 if ( $result === NULL ) {
43 $result = new PHPUnit_Framework_TestResult;
44 }
45
46 $this->suite->publishTestArticles(); // Add articles needed by the tests.
47 $backend = new ParserTestSuiteBackend;
48 $result->startTest( $this );
49
50 // Support the transition to PHPUnit 3.5 where PHPUnit_Util_Timer is replaced with PHP_Timer
51 if ( class_exists( 'PHP_Timer' ) ) {
52 PHP_Timer::start();
53 } else {
54 PHPUnit_Util_Timer::start();
55 }
56
57 $r = false;
58 try {
59 # Run the test.
60 # On failure, the subclassed backend will throw an exception with
61 # the details.
62 $pt = new PHPUnitParserTest;
63 $r = $pt->runTest( $this->test['test'], $this->test['input'],
64 $this->test['result'], $this->test['options'], $this->test['config']
65 );
66 }
67 catch ( PHPUnit_Framework_AssertionFailedError $e ) {
68
69 // PHPUnit_Util_Timer -> PHP_Timer support (see above)
70 if ( class_exists( 'PHP_Timer' ) ) {
71 $result->addFailure( $this, $e, PHP_Timer::stop() );
72 } else {
73 $result->addFailure( $this, $e, PHPUnit_Util_Timer::stop() );
74 }
75 }
76 catch ( Exception $e ) {
77 // PHPUnit_Util_Timer -> PHP_Timer support (see above)
78 if ( class_exists( 'PHP_Timer' ) ) {
79 $result->addFailure( $this, $e, PHP_Timer::stop() );
80 } else {
81 $result->addFailure( $this, $e, PHPUnit_Util_Timer::stop() );
82 }
83 }
84
85 // PHPUnit_Util_Timer -> PHP_Timer support (see above)
86 if ( class_exists( 'PHP_Timer' ) ) {
87 $result->endTest( $this, PHP_Timer::stop() );
88 } else {
89 $result->endTest( $this, PHPUnit_Util_Timer::stop() );
90 }
91
92 $backend->recorder->record( $this->test['test'], $r );
93 $this->addToAssertionCount( PHPUnit_Framework_Assert::getCount() );
94
95 return $result;
96 }
97
98 public function toString() {
99 return $this->test['test'];
100 }
101
102 }
103
104 class ParserTestSuiteBackend extends PHPUnit_FrameWork_TestSuite {
105 public $recorder;
106 public $term;
107 static $usePHPUnit = false;
108
109 function __construct() {
110 parent::__construct();
111 $this->setupRecorder(null);
112 self::$usePHPUnit = method_exists('PHPUnit_Framework_Assert', 'assertEquals');
113 }
114
115 function showTesting( $desc ) {
116 }
117
118 function showRunFile( $path ) {
119 }
120
121 function showTestResult( $desc, $result, $out ) {
122 if ( $result === $out ) {
123 return self::showSuccess( $desc, $result, $out );
124 } else {
125 return self::showFailure( $desc, $result, $out );
126 }
127 }
128
129 public function setupRecorder( $options ) {
130 $this->recorder = new PHPUnitTestRecorder( $this );
131 }
132 }
133
134 class PHPUnitTestRecorder extends TestRecorder {
135 function record( $test, $result ) {
136 $this->total++;
137 $this->success += $result;
138
139 }
140
141 function reportPercentage( $success, $total ) { }
142 }