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';
32 public static function suite() {
33 $suite = new PHPUnit_Framework_TestSuite();
36 $wgHooks['ParserTestTables'][] = "MediaWikiParserTestSuite::addTables";
38 self
::$iter = new TestFileIterator( PARSER_TESTS
);
40 foreach(self
::$iter as $i => $test) {
41 $suite->addTest(new ParserUnitTest($i, $test['test']));
46 self
::$parser = new PTShell
;
47 self
::$iter->setParser(self
::$parser);
48 self
::$parser->recorder
->start();
49 self
::$parser->setupDatabase();
50 self
::$iter->rewind();
52 /* function setUp() { */
53 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
54 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
55 $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
56 $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
57 $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
58 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
60 $wgScript = '/index.php';
62 $wgArticlePath = '/wiki/$1';
63 $wgStyleSheetPath = '/skins';
64 $wgStylePath = '/skins';
65 $wgThumbnailScriptPath = false;
66 $wgLocalFileRepo = array(
67 'class' => 'LocalRepo',
69 'directory' => 'test-repo',
70 'url' => 'http://example.com/images',
72 'transformVia404' => false,
74 $wgNamespaceProtection[NS_MEDIAWIKI
] = 'editinterface';
75 $wgNamespaceAliases['Image'] = NS_FILE
;
76 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK
;
79 $wgEnableParserCache = false;
80 $wgDeferredUpdateList = array();
81 $wgMemc =& wfGetMainCache();
82 $messageMemc =& wfGetMessageCacheStorage();
83 $parserMemc =& wfGetParserCacheStorage();
85 $wgContLang = new StubContLang
;
86 $wgUser = new StubUser
;
87 $wgLang = new StubUserLang
;
88 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
89 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
90 $wgRequest = new WebRequest
;
92 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
93 array( $messageMemc, $wgUseDatabaseMessages,
94 $wgMsgCacheExpiry, wfWikiID() ) );
95 if( $wgStyleDirectory === false) $wgStyleDirectory = "$IP/skins";
100 /* public function tearDown() { */
101 /* $this->teardownDatabase(); */
102 /* $this->recorder->report(); */
103 /* $this->recorder->end(); */
104 /* $this->teardownUploadDir($this->uploadDir); */
107 public function count() {return self
::$count;}
109 public function toString() {
110 return "MediaWiki Parser Tests";
115 private $keepUploads;
117 * Remove the dummy uploads directory
119 private function teardownUploadDir( $dir ) {
120 if ( $this->keepUploads
) {
124 // delete the files first, then the dirs.
127 "$dir/3/3a/Foobar.jpg",
128 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
129 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
130 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
131 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
143 "$dir/thumb/3/3a/Foobar.jpg",
157 * Delete the specified files, if they exist.
158 * @param array $files full paths to files to delete.
160 private static function deleteFiles( $files ) {
161 foreach( $files as $file ) {
162 if( file_exists( $file ) ) {
168 * Delete the specified directories, if they exist. Must be empty.
169 * @param array $dirs full paths to directories to delete.
171 private static function deleteDirs( $dirs ) {
172 foreach( $dirs as $dir ) {
173 if( is_dir( $dir ) ) {
180 * Create a dummy uploads directory which will contain a couple
181 * of files in order to pass existence tests.
182 * @return string The directory
184 private function setupUploadDir() {
186 if ( $this->keepUploads
) {
187 $dir = wfTempDir() . '/mwParser-images';
188 if ( is_dir( $dir ) ) {
192 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
195 wfDebug( "Creating upload directory $dir\n" );
196 if ( file_exists( $dir ) ) {
197 wfDebug( "Already exists!\n" );
200 wfMkdirParents( $dir . '/3/3a' );
201 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
203 wfMkdirParents( $dir . '/0/09' );
204 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
212 class ParserUnitTest
extends PHPUnit_Framework_TestCase
{
216 public function testBogus() {
217 $this->markTestSkipped("This is a stub");
220 public function __construct($number = null, $test = null) {
221 $this->number
= $number;
225 function count() {return 1;}
227 public function run(PHPUnit_Framework_TestResult
$result = NULL) {
228 PHPUnit_Framework_Assert
::resetCount();
229 if ($result === NULL) {
230 $result = new PHPUnit_Framework_TestResult
;
233 $t = MediaWikiParserTestSuite
::$iter->current();
234 $k = MediaWikiParserTestSuite
::$iter->key();
236 if(!MediaWikiParserTestSuite
::$iter->valid()) {
240 // The only way this should happen is if the parserTest.txt
241 // file were modified while the script is running.
242 if($k != $this->number
) {
244 wfDie("I got confused!\n");
247 $result->startTest($this);
248 PHPUnit_Util_Timer
::start();
252 $r = MediaWikiParserTestSuite
::$parser->runTest(
253 $t['test'], $t['input'], $t['result'], $t['options'], $t['config']
255 PHPUnit_Framework_Assert
::assertTrue(true, $t['test']);
257 catch (PHPUnit_Framework_AssertionFailedError
$e) {
258 $result->addFailure($this, $e, PHPUnit_Util_Timer
::stop());
260 catch (Exception
$e) {
261 $result->addError($this, $e, PHPUnit_Util_Timer
::stop());
263 PHPUnit_Framework_Assert
::assertTrue(true, $t['test']);
265 $result->endTest($this, PHPUnit_Util_Timer
::stop());
267 MediaWikiParserTestSuite
::$parser->recorder
->record($t['test'], $r);
268 MediaWikiParserTestSuite
::$iter->next();
269 $this->addToAssertionCount(PHPUnit_Framework_Assert
::getCount());
276 class PTShell
extends ParserTest
{
277 function showTesting( $desc ) {
280 function showRunFile( $path ) {
283 function showSuccess( $desc ) {
284 PHPUnit_Framework_Assert
::assertTrue(true, $desc);
288 function showFailure( $desc, $expected, $got ) {
289 PHPUnit_Framework_Assert
::assertEquals($expected, $got, $desc);