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';
33 public static function suite() {
34 $suite = new PHPUnit_Framework_TestSuite();
37 $wgHooks['ParserTestTables'][] = "MediaWikiParserTestSuite::addTables";
39 self
::$iter = new TestFileIterator( PARSER_TESTS
);
41 foreach(self
::$iter as $i => $test) {
42 $suite->addTest(new ParserUnitTest($i, $test['test']));
47 self
::$parser = new PTShell
;
48 self
::$iter->setParser(self
::$parser);
49 self
::$parser->recorder
->start();
50 self
::$parser->setupDatabase();
51 self
::$iter->rewind();
53 /* function setUp() { */
54 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
55 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
56 $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
57 $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
58 $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
59 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
61 $wgScript = '/index.php';
63 $wgArticlePath = '/wiki/$1';
64 $wgStyleSheetPath = '/skins';
65 $wgStylePath = '/skins';
66 $wgThumbnailScriptPath = false;
67 $wgLocalFileRepo = array(
68 'class' => 'LocalRepo',
70 'directory' => 'test-repo',
71 'url' => 'http://example.com/images',
73 'transformVia404' => false,
75 $wgNamespaceProtection[NS_MEDIAWIKI
] = 'editinterface';
76 $wgNamespaceAliases['Image'] = NS_FILE
;
77 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK
;
80 $wgEnableParserCache = false;
81 $wgDeferredUpdateList = array();
82 $wgMemc =& wfGetMainCache();
83 $messageMemc =& wfGetMessageCacheStorage();
84 $parserMemc =& wfGetParserCacheStorage();
86 $wgContLang = new StubContLang
;
87 $wgUser = new StubUser
;
88 $wgLang = new StubUserLang
;
89 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
90 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
91 $wgRequest = new WebRequest
;
93 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
94 array( $messageMemc, $wgUseDatabaseMessages,
95 $wgMsgCacheExpiry, wfWikiID() ) );
96 if( $wgStyleDirectory === false) $wgStyleDirectory = "$IP/skins";
101 public function tearDown() {
102 /* $this->teardownDatabase(); */
103 $this->recorder
->report();
104 $this->recorder
->end();
105 $this->teardownUploadDir($this->uploadDir
);
108 public function count() {return self
::$count;}
110 public function toString() {
111 return "MediaWiki Parser Tests";
116 private $keepUploads;
118 * Remove the dummy uploads directory
120 private function teardownUploadDir( $dir ) {
121 if ( $this->keepUploads
) {
125 // delete the files first, then the dirs.
128 "$dir/3/3a/Foobar.jpg",
129 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
130 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
131 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
132 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
144 "$dir/thumb/3/3a/Foobar.jpg",
158 * Delete the specified files, if they exist.
159 * @param array $files full paths to files to delete.
161 private static function deleteFiles( $files ) {
162 foreach( $files as $file ) {
163 if( file_exists( $file ) ) {
169 * Delete the specified directories, if they exist. Must be empty.
170 * @param array $dirs full paths to directories to delete.
172 private static function deleteDirs( $dirs ) {
173 foreach( $dirs as $dir ) {
174 if( is_dir( $dir ) ) {
181 * Create a dummy uploads directory which will contain a couple
182 * of files in order to pass existence tests.
183 * @return string The directory
185 private function setupUploadDir() {
187 if ( $this->keepUploads
) {
188 $dir = wfTempDir() . '/mwParser-images';
189 if ( is_dir( $dir ) ) {
193 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
196 wfDebug( "Creating upload directory $dir\n" );
197 if ( file_exists( $dir ) ) {
198 wfDebug( "Already exists!\n" );
201 wfMkdirParents( $dir . '/3/3a' );
202 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
204 wfMkdirParents( $dir . '/0/09' );
205 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
213 class ParserUnitTest
extends PHPUnit_Framework_TestCase
{
217 public function testBogus() {
218 $this->markTestSkipped("This is a stub");
221 public function __construct($number = null, $test = null) {
222 $this->number
= $number;
226 function count() {return 1;}
228 public function run(PHPUnit_Framework_TestResult
$result = NULL) {
229 PHPUnit_Framework_Assert
::resetCount();
230 if ($result === NULL) {
231 $result = new PHPUnit_Framework_TestResult
;
234 $t = MediaWikiParserTestSuite
::$iter->current();
235 $k = MediaWikiParserTestSuite
::$iter->key();
237 if(!MediaWikiParserTestSuite
::$iter->valid()) {
241 // The only way this should happen is if the parserTest.txt
242 // file were modified while the script is running.
243 if($k != $this->number
) {
245 wfDie("I got confused!\n");
248 $result->startTest($this);
249 PHPUnit_Util_Timer
::start();
253 $r = MediaWikiParserTestSuite
::$parser->runTest(
254 $t['test'], $t['input'], $t['result'], $t['options'], $t['config']
256 PHPUnit_Framework_Assert
::assertTrue(true, $t['test']);
258 catch (PHPUnit_Framework_AssertionFailedError
$e) {
259 $result->addFailure($this, $e, PHPUnit_Util_Timer
::stop());
261 catch (Exception
$e) {
262 $result->addError($this, $e, PHPUnit_Util_Timer
::stop());
264 PHPUnit_Framework_Assert
::assertTrue(true, $t['test']);
266 $result->endTest($this, PHPUnit_Util_Timer
::stop());
268 MediaWikiParserTestSuite
::$parser->recorder
->record($t['test'], $r);
269 MediaWikiParserTestSuite
::$iter->next();
270 $this->addToAssertionCount(PHPUnit_Framework_Assert
::getCount());
277 class PTShell
extends ParserTest
{
278 function showTesting( $desc ) {
281 function showRunFile( $path ) {
284 function showSuccess( $desc ) {
285 PHPUnit_Framework_Assert
::assertTrue(true, $desc);
289 function showFailure( $desc, $expected, $got ) {
290 PHPUnit_Framework_Assert
::assertEquals($expected, $got, $desc);