4 define( "NO_COMMAND_LINE", 1 );
5 define( "PARSER_TESTS", "$IP/maintenance/parserTests.txt" );
7 require_once( "$IP/maintenance/parserTests.inc" );
9 class PHPUnitTestRecorder
extends TestRecorder
{
11 function record( $test, $result ) {
13 $this->success +
= $result;
17 function reportPercentage( $success, $total ) {}
20 class MediaWikiParserTestSuite
extends PHPUnit_Framework_TestSuite
{
21 #implements PHPUnit_Framework_SelfDescribing {
22 static private $count;
23 static public $parser;
26 public static function suite() {
27 $suite = new PHPUnit_Framework_TestSuite();
29 self
::$iter = new TestFileIterator( PARSER_TESTS
);
31 foreach(self
::$iter as $i => $test) {
32 $suite->addTest(new ParserUnitTest($i, $test['test']));
37 self
::$parser = new PTShell
;
38 self
::$iter->setParser(self
::$parser);
39 self
::$parser->recorder
->start();
40 self
::$parser->setupDatabase();
41 self
::$iter->rewind();
43 /* function setUp() { */
44 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
45 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
46 $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
47 $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
48 $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
49 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
51 $wgScript = '/index.php';
53 $wgArticlePath = '/wiki/$1';
54 $wgStyleSheetPath = '/skins';
55 $wgStylePath = '/skins';
56 $wgThumbnailScriptPath = false;
57 $wgLocalFileRepo = array(
58 'class' => 'LocalRepo',
61 'url' => 'http://example.com/images',
63 'transformVia404' => false,
65 $wgNamespaceProtection[NS_MEDIAWIKI
] = 'editinterface';
66 $wgNamespaceAliases['Image'] = NS_FILE
;
67 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK
;
70 $wgEnableParserCache = false;
71 $wgDeferredUpdateList = array();
72 $wgMemc =& wfGetMainCache();
73 $messageMemc =& wfGetMessageCacheStorage();
74 $parserMemc =& wfGetParserCacheStorage();
76 $wgContLang = new StubContLang
;
77 $wgUser = new StubUser
;
78 $wgLang = new StubUserLang
;
79 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
80 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
81 $wgRequest = new WebRequest
;
83 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
84 array( $messageMemc, $wgUseDatabaseMessages,
85 $wgMsgCacheExpiry, wfWikiID() ) );
86 if( $wgStyleDirectory === false) $wgStyleDirectory = "$IP/skins";
91 public function tearDown() {
92 $this->teardownDatabase();
93 $this->recorder
->report();
94 $this->recorder
->end();
95 $this->teardownUploadDir($this->uploadDir
);
98 public function count() {return self
::$count;}
100 public function toString() {
101 return "MediaWiki Parser Tests";
107 private $keepUploads;
109 * Remove the dummy uploads directory
111 private function teardownUploadDir( $dir ) {
112 if ( $this->keepUploads
) {
116 // delete the files first, then the dirs.
119 "$dir/3/3a/Foobar.jpg",
120 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
121 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
122 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
123 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
135 "$dir/thumb/3/3a/Foobar.jpg",
149 * Delete the specified files, if they exist.
150 * @param array $files full paths to files to delete.
152 private static function deleteFiles( $files ) {
153 foreach( $files as $file ) {
154 if( file_exists( $file ) ) {
160 * Delete the specified directories, if they exist. Must be empty.
161 * @param array $dirs full paths to directories to delete.
163 private static function deleteDirs( $dirs ) {
164 foreach( $dirs as $dir ) {
165 if( is_dir( $dir ) ) {
172 * Create a dummy uploads directory which will contain a couple
173 * of files in order to pass existence tests.
174 * @return string The directory
176 private function setupUploadDir() {
178 if ( $this->keepUploads
) {
179 $dir = wfTempDir() . '/mwParser-images';
180 if ( is_dir( $dir ) ) {
184 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
187 wfDebug( "Creating upload directory $dir\n" );
188 if ( file_exists( $dir ) ) {
189 wfDebug( "Already exists!\n" );
192 wfMkdirParents( $dir . '/3/3a' );
193 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
195 wfMkdirParents( $dir . '/0/09' );
196 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
201 class ParserUnitTest
extends PHPUnit_Framework_TestCase
{
205 public function __construct($number, $test) {
206 $this->number
= $number;
210 function count() {return 1;}
212 public function run(PHPUnit_Framework_TestResult
$result = NULL) {
213 PHPUnit_Framework_Assert
::resetCount();
214 if ($result === NULL) {
215 $result = new PHPUnit_Framework_TestResult
;
218 $t = MediaWikiParserTestSuite
::$iter->current();
219 $k = MediaWikiParserTestSuite
::$iter->key();
221 if(!MediaWikiParserTestSuite
::$iter->valid()) {
225 // The only way this should happen is if the parserTest.txt
226 // file were modified while the script is running.
227 if($k != $this->number
) {
229 wfDie("I got confused!\n");
232 $result->startTest($this);
233 PHPUnit_Util_Timer
::start();
237 $r = MediaWikiParserTestSuite
::$parser->runTest(
238 $t['test'], $t['input'], $t['result'], $t['options'], $t['config']
240 PHPUnit_Framework_Assert
::assertTrue(true, $t['test']);
242 catch (PHPUnit_Framework_AssertionFailedError
$e) {
243 $result->addFailure($this, $e, PHPUnit_Util_Timer
::stop());
245 catch (Exception
$e) {
246 $result->addError($this, $e, PHPUnit_Util_Timer
::stop());
248 PHPUnit_Framework_Assert
::assertTrue(true, $t['test']);
250 $result->endTest($this, PHPUnit_Util_Timer
::stop());
252 MediaWikiParserTestSuite
::$parser->recorder
->record($t['test'], $r);
253 MediaWikiParserTestSuite
::$iter->next();
254 $this->addToAssertionCount(PHPUnit_Framework_Assert
::getCount());
261 class PTShell
extends ParserTest
{
262 function showTesting( $desc ) {
265 function showRunFile( $path ) {
268 function showSuccess( $desc ) {
269 PHPUnit_Framework_Assert
::assertTrue(true, $desc);
273 function showFailure( $desc, $expected, $got ) {
274 PHPUnit_Framework_Assert
::assertEquals($expected, $got, $desc);