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';
34 public static function suite() {
35 $suite = new PHPUnit_Framework_TestSuite();
38 $wgHooks['ParserTestTables'][] = "MediaWikiParserTestSuite::addTables";
40 self
::$iter = new TestFileIterator( PARSER_TESTS
);
42 foreach(self
::$iter as $i => $test) {
43 $suite->addTest(new ParserUnitTest($i, $test['test']));
48 self
::$parser = new PTShell
;
49 self
::$iter->setParser(self
::$parser);
50 self
::$parser->recorder
->start();
51 self
::$parser->setupDatabase();
52 self
::$iter->rewind();
54 /* function setUp() { */
55 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
56 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
57 $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
58 $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
59 $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
60 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
62 $wgScript = '/index.php';
64 $wgArticlePath = '/wiki/$1';
65 $wgStyleSheetPath = '/skins';
66 $wgStylePath = '/skins';
67 $wgThumbnailScriptPath = false;
68 $wgLocalFileRepo = array(
69 'class' => 'LocalRepo',
71 'directory' => 'test-repo',
72 'url' => 'http://example.com/images',
74 'transformVia404' => false,
76 $wgNamespaceProtection[NS_MEDIAWIKI
] = 'editinterface';
77 $wgNamespaceAliases['Image'] = NS_FILE
;
78 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK
;
81 $wgEnableParserCache = false;
82 $wgDeferredUpdateList = array();
83 $wgMemc =& wfGetMainCache();
84 $messageMemc =& wfGetMessageCacheStorage();
85 $parserMemc =& wfGetParserCacheStorage();
87 $wgContLang = new StubContLang
;
88 $wgUser = new StubUser
;
89 $wgLang = new StubUserLang
;
90 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
91 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
92 $wgRequest = new WebRequest
;
94 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
95 array( $messageMemc, $wgUseDatabaseMessages,
96 $wgMsgCacheExpiry, wfWikiID() ) );
97 if( $wgStyleDirectory === false) $wgStyleDirectory = "$IP/skins";
102 public function tearDown() {
103 /* $this->teardownDatabase(); */
104 $this->recorder
->report();
105 $this->recorder
->end();
106 $this->teardownUploadDir($this->uploadDir
);
109 public function count() {return self
::$count;}
111 public function toString() {
112 return "MediaWiki Parser Tests";
117 private $keepUploads;
119 * Remove the dummy uploads directory
121 private function teardownUploadDir( $dir ) {
122 if ( $this->keepUploads
) {
126 // delete the files first, then the dirs.
129 "$dir/3/3a/Foobar.jpg",
130 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
131 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
132 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
133 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
145 "$dir/thumb/3/3a/Foobar.jpg",
159 * Delete the specified files, if they exist.
160 * @param array $files full paths to files to delete.
162 private static function deleteFiles( $files ) {
163 foreach( $files as $file ) {
164 if( file_exists( $file ) ) {
170 * Delete the specified directories, if they exist. Must be empty.
171 * @param array $dirs full paths to directories to delete.
173 private static function deleteDirs( $dirs ) {
174 foreach( $dirs as $dir ) {
175 if( is_dir( $dir ) ) {
182 * Create a dummy uploads directory which will contain a couple
183 * of files in order to pass existence tests.
184 * @return string The directory
186 private function setupUploadDir() {
188 if ( $this->keepUploads
) {
189 $dir = wfTempDir() . '/mwParser-images';
190 if ( is_dir( $dir ) ) {
194 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
197 wfDebug( "Creating upload directory $dir\n" );
198 if ( file_exists( $dir ) ) {
199 wfDebug( "Already exists!\n" );
202 wfMkdirParents( $dir . '/3/3a' );
203 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
205 wfMkdirParents( $dir . '/0/09' );
206 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
214 class ParserUnitTest
extends PHPUnit_Framework_TestCase
{
218 public function testBogus() {
219 $this->markTestSkipped("This is a stub");
222 public function __construct($number = null, $test = null) {
223 $this->number
= $number;
227 function count() {return 1;}
229 public function run(PHPUnit_Framework_TestResult
$result = NULL) {
230 PHPUnit_Framework_Assert
::resetCount();
231 if ($result === NULL) {
232 $result = new PHPUnit_Framework_TestResult
;
235 $t = MediaWikiParserTestSuite
::$iter->current();
236 $k = MediaWikiParserTestSuite
::$iter->key();
238 if(!MediaWikiParserTestSuite
::$iter->valid()) {
242 // The only way this should happen is if the parserTest.txt
243 // file were modified while the script is running.
244 if($k != $this->number
) {
246 wfDie("I got confused!\n");
249 $result->startTest($this);
250 PHPUnit_Util_Timer
::start();
254 $r = MediaWikiParserTestSuite
::$parser->runTest(
255 $t['test'], $t['input'], $t['result'], $t['options'], $t['config']
257 PHPUnit_Framework_Assert
::assertTrue(true, $t['test']);
259 catch (PHPUnit_Framework_AssertionFailedError
$e) {
260 $result->addFailure($this, $e, PHPUnit_Util_Timer
::stop());
262 catch (Exception
$e) {
263 $result->addError($this, $e, PHPUnit_Util_Timer
::stop());
265 PHPUnit_Framework_Assert
::assertTrue(true, $t['test']);
267 $result->endTest($this, PHPUnit_Util_Timer
::stop());
269 MediaWikiParserTestSuite
::$parser->recorder
->record($t['test'], $r);
270 MediaWikiParserTestSuite
::$iter->next();
271 $this->addToAssertionCount(PHPUnit_Framework_Assert
::getCount());
278 class PTShell
extends ParserTest
{
279 function showTesting( $desc ) {
282 function showRunFile( $path ) {
285 function showSuccess( $desc ) {
286 PHPUnit_Framework_Assert
::assertTrue(true, $desc);
290 function showFailure( $desc, $expected, $got ) {
291 PHPUnit_Framework_Assert
::assertEquals($expected, $got, $desc);