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 static private $count;
22 static public $parser;
25 public static function addTables( &$tables ) {
26 $tables[] = 'user_properties';
27 $tables[] = 'filearchive';
28 $tables[] = 'logging';
29 $tables[] = 'updatelog';
30 $tables[] = 'iwlinks';
31 $tables[] = 'searchindex';
35 public static function suite() {
36 $suite = new PHPUnit_Framework_TestSuite();
39 $wgHooks['ParserTestTables'][] = "MediaWikiParserTestSuite::addTables";
41 self
::$iter = new TestFileIterator( PARSER_TESTS
);
43 foreach ( self
::$iter as $i => $test ) {
44 $suite->addTest( new ParserUnitTest( $i, $test['test'] ) );
49 self
::$parser = new PTShell
;
50 self
::$iter->setParser( self
::$parser );
51 self
::$parser->recorder
->start();
52 self
::$parser->setupDatabase();
53 self
::$iter->rewind();
55 /* function setUp() { */
56 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
57 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
58 $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
59 $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
60 $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
61 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
63 $wgScript = '/index.php';
65 $wgArticlePath = '/wiki/$1';
66 $wgStyleSheetPath = '/skins';
67 $wgStylePath = '/skins';
68 $wgThumbnailScriptPath = false;
69 $wgLocalFileRepo = array(
70 'class' => 'LocalRepo',
72 'directory' => 'test-repo',
73 'url' => 'http://example.com/images',
75 'transformVia404' => false,
77 $wgNamespaceProtection[NS_MEDIAWIKI
] = 'editinterface';
78 $wgNamespaceAliases['Image'] = NS_FILE
;
79 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK
;
82 $wgEnableParserCache = false;
83 $wgDeferredUpdateList = array();
84 $wgMemc =& wfGetMainCache();
85 $messageMemc =& wfGetMessageCacheStorage();
86 $parserMemc =& wfGetParserCacheStorage();
88 $wgContLang = new StubContLang
;
89 $wgUser = new StubUser
;
90 $wgLang = new StubUserLang
;
91 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
92 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
93 $wgRequest = new WebRequest
;
95 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
96 array( $messageMemc, $wgUseDatabaseMessages,
97 $wgMsgCacheExpiry, wfWikiID() ) );
98 if ( $wgStyleDirectory === false ) $wgStyleDirectory = "$IP/skins";
103 public function tearDown() {
104 /* $this->teardownDatabase(); */
105 $this->recorder
->report();
106 $this->recorder
->end();
107 $this->teardownUploadDir( $this->uploadDir
);
110 public function count() { return self
::$count; }
112 public function toString() {
113 return "MediaWiki Parser Tests";
118 private $keepUploads;
120 * Remove the dummy uploads directory
122 private function teardownUploadDir( $dir ) {
123 if ( $this->keepUploads
) {
127 // delete the files first, then the dirs.
130 "$dir/3/3a/Foobar.jpg",
131 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
132 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
133 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
134 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
146 "$dir/thumb/3/3a/Foobar.jpg",
160 * Delete the specified files, if they exist.
161 * @param array $files full paths to files to delete.
163 private static function deleteFiles( $files ) {
164 foreach ( $files as $file ) {
165 if ( file_exists( $file ) ) {
171 * Delete the specified directories, if they exist. Must be empty.
172 * @param array $dirs full paths to directories to delete.
174 private static function deleteDirs( $dirs ) {
175 foreach ( $dirs as $dir ) {
176 if ( is_dir( $dir ) ) {
183 * Create a dummy uploads directory which will contain a couple
184 * of files in order to pass existence tests.
185 * @return string The directory
187 private function setupUploadDir() {
189 if ( $this->keepUploads
) {
190 $dir = wfTempDir() . '/mwParser-images';
191 if ( is_dir( $dir ) ) {
195 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
198 wfDebug( "Creating upload directory $dir\n" );
199 if ( file_exists( $dir ) ) {
200 wfDebug( "Already exists!\n" );
203 wfMkdirParents( $dir . '/3/3a' );
204 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
206 wfMkdirParents( $dir . '/0/09' );
207 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
215 class ParserUnitTest
extends PHPUnit_Framework_TestCase
{
219 public function testBogus() {
220 $this->markTestSkipped( "This is a stub" );
223 public function __construct( $number = null, $test = null ) {
224 $this->number
= $number;
228 function count() { return 1; }
230 public function run( PHPUnit_Framework_TestResult
$result = NULL ) {
231 PHPUnit_Framework_Assert
::resetCount();
232 if ( $result === NULL ) {
233 $result = new PHPUnit_Framework_TestResult
;
236 $t = MediaWikiParserTestSuite
::$iter->current();
237 $k = MediaWikiParserTestSuite
::$iter->key();
239 if ( !MediaWikiParserTestSuite
::$iter->valid() ) {
243 // The only way this should happen is if the parserTest.txt
244 // file were modified while the script is running.
245 if ( $k != $this->number
) {
247 wfDie( "I got confused!\n" );
250 $result->startTest( $this );
251 PHPUnit_Util_Timer
::start();
255 $r = MediaWikiParserTestSuite
::$parser->runTest(
256 $t['test'], $t['input'], $t['result'], $t['options'], $t['config']
258 PHPUnit_Framework_Assert
::assertTrue( true, $t['test'] );
260 catch ( PHPUnit_Framework_AssertionFailedError
$e ) {
261 $result->addFailure( $this, $e, PHPUnit_Util_Timer
::stop() );
263 catch ( Exception
$e ) {
264 $result->addError( $this, $e, PHPUnit_Util_Timer
::stop() );
266 PHPUnit_Framework_Assert
::assertTrue( true, $t['test'] );
268 $result->endTest( $this, PHPUnit_Util_Timer
::stop() );
270 MediaWikiParserTestSuite
::$parser->recorder
->record( $t['test'], $r );
271 MediaWikiParserTestSuite
::$iter->next();
272 $this->addToAssertionCount( PHPUnit_Framework_Assert
::getCount() );
279 class PTShell
extends ParserTest
{
280 function showTesting( $desc ) {
283 function showRunFile( $path ) {
286 function showSuccess( $desc ) {
287 PHPUnit_Framework_Assert
::assertTrue( true, $desc );
291 function showFailure( $desc, $expected, $got ) {
292 PHPUnit_Framework_Assert
::assertEquals( $expected, $got, $desc );