3 if ( !defined( 'MEDIAWIKI' ) ) {
8 define( "NO_COMMAND_LINE", 1 );
9 define( "PARSER_TESTS", "$IP/maintenance/parserTests.txt" );
11 require_once( "$IP/maintenance/parserTests.inc" );
13 class PHPUnitTestRecorder extends TestRecorder {
15 function record( $test, $result ) {
17 $this->success += $result;
21 function reportPercentage( $success, $total ) {}
24 class MediaWikiParserTestSuite extends PHPUnit_Framework_TestSuite {
25 #implements PHPUnit_Framework_SelfDescribing {
26 static private $count;
27 static public $parser;
30 public static function suite() {
31 $suite = new PHPUnit_Framework_TestSuite();
33 self::$iter = new TestFileIterator( PARSER_TESTS );
35 foreach(self::$iter as $i => $test) {
36 $suite->addTest(new ParserUnitTest($i, $test['test']));
41 self::$parser = new PTShell;
42 self::$iter->setParser(self::$parser);
43 self::$parser->recorder->start();
44 self::$parser->setupDatabase();
45 self::$iter->rewind();
47 /* function setUp() { */
48 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
49 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
50 $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
51 $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
52 $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
53 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
55 $wgScript = '/index.php';
57 $wgArticlePath = '/wiki/$1';
58 $wgStyleSheetPath = '/skins';
59 $wgStylePath = '/skins';
60 $wgThumbnailScriptPath = false;
61 $wgLocalFileRepo = array(
62 'class' => 'LocalRepo',
65 'url' => 'http://example.com/images',
67 'transformVia404' => false,
69 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
70 $wgNamespaceAliases['Image'] = NS_FILE;
71 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
74 $wgEnableParserCache = false;
75 $wgDeferredUpdateList = array();
76 $wgMemc =& wfGetMainCache();
77 $messageMemc =& wfGetMessageCacheStorage();
78 $parserMemc =& wfGetParserCacheStorage();
80 $wgContLang = new StubContLang;
81 $wgUser = new StubUser;
82 $wgLang = new StubUserLang;
83 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
84 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
85 $wgRequest = new WebRequest;
87 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
88 array( $messageMemc, $wgUseDatabaseMessages,
89 $wgMsgCacheExpiry, wfWikiID() ) );
90 if( $wgStyleDirectory === false) $wgStyleDirectory = "$IP/skins";
95 public function tearDown() {
96 $this->teardownDatabase();
97 $this->recorder->report();
98 $this->recorder->end();
99 $this->teardownUploadDir($this->uploadDir);
102 public function count() {return self::$count;}
104 public function toString() {
105 return "MediaWiki Parser Tests";
111 private $keepUploads;
113 * Remove the dummy uploads directory
115 private function teardownUploadDir( $dir ) {
116 if ( $this->keepUploads ) {
120 // delete the files first, then the dirs.
123 "$dir/3/3a/Foobar.jpg",
124 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
125 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
126 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
127 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
139 "$dir/thumb/3/3a/Foobar.jpg",
153 * Delete the specified files, if they exist.
154 * @param array $files full paths to files to delete.
156 private static function deleteFiles( $files ) {
157 foreach( $files as $file ) {
158 if( file_exists( $file ) ) {
164 * Delete the specified directories, if they exist. Must be empty.
165 * @param array $dirs full paths to directories to delete.
167 private static function deleteDirs( $dirs ) {
168 foreach( $dirs as $dir ) {
169 if( is_dir( $dir ) ) {
176 * Create a dummy uploads directory which will contain a couple
177 * of files in order to pass existence tests.
178 * @return string The directory
180 private function setupUploadDir() {
182 if ( $this->keepUploads ) {
183 $dir = wfTempDir() . '/mwParser-images';
184 if ( is_dir( $dir ) ) {
188 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
191 wfDebug( "Creating upload directory $dir\n" );
192 if ( file_exists( $dir ) ) {
193 wfDebug( "Already exists!\n" );
196 wfMkdirParents( $dir . '/3/3a' );
197 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
199 wfMkdirParents( $dir . '/0/09' );
200 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
205 class ParserUnitTest extends PHPUnit_Framework_TestCase {
209 public function __construct($number, $test) {
210 $this->number = $number;
214 function count() {return 1;}
216 public function run(PHPUnit_Framework_TestResult $result = NULL) {
217 PHPUnit_Framework_Assert::resetCount();
218 if ($result === NULL) {
219 $result = new PHPUnit_Framework_TestResult;
222 $t = MediaWikiParserTestSuite::$iter->current();
223 $k = MediaWikiParserTestSuite::$iter->key();
225 if(!MediaWikiParserTestSuite::$iter->valid()) {
229 // The only way this should happen is if the parserTest.txt
230 // file were modified while the script is running.
231 if($k != $this->number) {
233 wfDie("I got confused!\n");
236 $result->startTest($this);
237 PHPUnit_Util_Timer::start();
241 $r = MediaWikiParserTestSuite::$parser->runTest(
242 $t['test'], $t['input'], $t['result'], $t['options'], $t['config']
244 PHPUnit_Framework_Assert::assertTrue(true, $t['test']);
246 catch (PHPUnit_Framework_AssertionFailedError $e) {
247 $result->addFailure($this, $e, PHPUnit_Util_Timer::stop());
249 catch (Exception $e) {
250 $result->addError($this, $e, PHPUnit_Util_Timer::stop());
252 PHPUnit_Framework_Assert::assertTrue(true, $t['test']);
254 $result->endTest($this, PHPUnit_Util_Timer::stop());
256 MediaWikiParserTestSuite::$parser->recorder->record($t['test'], $r);
257 MediaWikiParserTestSuite::$iter->next();
258 $this->addToAssertionCount(PHPUnit_Framework_Assert::getCount());
265 class PTShell extends ParserTest {
266 function showTesting( $desc ) {
269 function showRunFile( $path ) {
272 function showSuccess( $desc ) {
273 PHPUnit_Framework_Assert::assertTrue(true, $desc);
277 function showFailure( $desc, $expected, $got ) {
278 PHPUnit_Framework_Assert::assertEquals($expected, $got, $desc);