summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/parser/ParserHelpers.php
blob: 4a6ce7c47e56b1fa6a1854b7a5b06596732dca20 (plain)
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
<?php

class PHPUnitParserTest extends ParserTest {
	function showTesting( $desc ) {
		/* Do nothing since we don't want to show info during PHPUnit testing. */
	}

	public function showSuccess( $desc ) {
		PHPUnit_Framework_Assert::assertTrue( true, $desc );
		return true;
	}

	public function showFailure( $desc, $expected, $got ) {
		PHPUnit_Framework_Assert::assertEquals( $expected, $got, $desc );
		return false;
	}
	
	public function setupRecorder( $options ) {
		$this->recorder = new PHPUnitTestRecorder( $this );
	}
}

class ParserUnitTest extends MediaWikiTestCase {
	private $test = "";

	public function __construct( $suite, $test = null ) {
		parent::__construct();
		$this->test = $test;
		$this->suite = $suite;
	}

	function count() { return 1; }

	public function run( PHPUnit_Framework_TestResult $result = null ) {
		PHPUnit_Framework_Assert::resetCount();
		if ( $result === NULL ) {
			$result = new PHPUnit_Framework_TestResult;
		}

		$this->suite->publishTestArticles(); // Add articles needed by the tests.
		$backend = new ParserTestSuiteBackend;
		$result->startTest( $this );

		// Support the transition to PHPUnit 3.5 where PHPUnit_Util_Timer is replaced with PHP_Timer
		if ( class_exists( 'PHP_Timer' ) ) {
			PHP_Timer::start();
		} else {
			PHPUnit_Util_Timer::start();
		}

		$r = false;
		try {
			# Run the test.
			# On failure, the subclassed backend will throw an exception with
			# the details.
			$pt = new PHPUnitParserTest;
			$r =  $pt->runTest( $this->test['test'], $this->test['input'],
				$this->test['result'], $this->test['options'], $this->test['config']
			);
		}
		catch ( PHPUnit_Framework_AssertionFailedError $e ) {

			// PHPUnit_Util_Timer -> PHP_Timer support (see above)
			if ( class_exists( 'PHP_Timer' ) ) {
				$result->addFailure( $this, $e, PHP_Timer::stop() );
			} else {
				$result->addFailure( $this, $e, PHPUnit_Util_Timer::stop() );
			}
		}
		catch ( Exception $e ) {
			// PHPUnit_Util_Timer -> PHP_Timer support (see above)
			if ( class_exists( 'PHP_Timer' ) ) {
				$result->addFailure( $this, $e, PHP_Timer::stop() );
			} else {
				$result->addFailure( $this, $e, PHPUnit_Util_Timer::stop() );
			}
		}

		// PHPUnit_Util_Timer -> PHP_Timer support (see above)
		if ( class_exists( 'PHP_Timer' ) ) {
			$result->endTest( $this, PHP_Timer::stop() );
		} else {
			$result->endTest( $this, PHPUnit_Util_Timer::stop() );
		}

		$backend->recorder->record( $this->test['test'], $r );
		$this->addToAssertionCount( PHPUnit_Framework_Assert::getCount() );

		return $result;
	}

	public function toString() {
		return $this->test['test'];
	}

}

class ParserTestSuiteBackend extends PHPUnit_FrameWork_TestSuite {
	public $recorder;
	public $term;
	static $usePHPUnit = false;

	function __construct() {
		parent::__construct();
		$this->setupRecorder(null);
		self::$usePHPUnit = method_exists('PHPUnit_Framework_Assert', 'assertEquals');
	}

	function showTesting( $desc ) {
	}

	function showRunFile( $path ) {
	}

	function showTestResult( $desc, $result, $out ) {
		if ( $result === $out ) {
			return self::showSuccess( $desc, $result, $out );
		} else {
			return self::showFailure( $desc, $result, $out );
	}
	}

	public function setupRecorder( $options ) {
		$this->recorder = new PHPUnitTestRecorder( $this );
	}
}

class PHPUnitTestRecorder extends TestRecorder {
	function record( $test, $result ) {
		$this->total++;
		$this->success += $result;

	}

	function reportPercentage( $success, $total ) { }
}