Make MediaWikiParserTest work now in PHPUnit. There are still a few bugs, such as...
authorX! <soxred93@users.mediawiki.org>
Wed, 29 Dec 2010 02:23:51 +0000 (02:23 +0000)
committerX! <soxred93@users.mediawiki.org>
Wed, 29 Dec 2010 02:23:51 +0000 (02:23 +0000)
lack of fuzz testing and other parser test options, and the DB creation is still a little flaky.
A MediaWikiPHPUnitCommand class had to be created to allow for custom CLI parameters.

tests/phpunit/MediaWikiPHPUnitCommand.php [new file with mode: 0644]
tests/phpunit/bootstrap.php
tests/phpunit/includes/parser/MediaWikiParserTest.php
tests/phpunit/includes/parser/ParserHelpers.php
tests/phpunit/phpunit.php
tests/phpunit/suite.xml

diff --git a/tests/phpunit/MediaWikiPHPUnitCommand.php b/tests/phpunit/MediaWikiPHPUnitCommand.php
new file mode 100644 (file)
index 0000000..b46c880
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+class MediaWikiPHPUnitCommand extends PHPUnit_TextUI_Command {
+
+       public function __construct() {
+               $this->longOptions['verbose'] = 'verboseHandler';
+       }
+       
+       public static function main( $exit = true ) {
+        $command = new self;
+        $command->run($_SERVER['argv'], $exit);
+    }
+
+       protected function verboseHandler($value) {
+               global $additionalMWCLIArgs;
+               $additionalMWCLIArgs['verbose'] = true;
+       }
+
+}
index e3a525b..480954a 100644 (file)
@@ -43,7 +43,7 @@ $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
 /* Classes */
 
 abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
-       protected $suite;
+       public $suite;
        public $regex = '';
        public $runDisabled = false;
        
@@ -53,11 +53,13 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected $oldTablePrefix;
        protected $useTemporaryTables = true;
 
-       function __construct( PHPUnit_Framework_TestSuite $suite = null ) {
-               if ( null !== $suite ) {
-                       $this->suite = $suite;
-               }
-               parent::__construct();
+       function  __construct( $name = null, array $data = array(), $dataName = '' ) {
+        if ($name !== null) {
+            $this->setName($name);
+        }
+
+        $this->data = $data;
+        $this->dataName = $dataName;
                
                if( $this->needsDB() && !is_object( $this->dbClone ) ) {
                        $this->initDB();
@@ -80,6 +82,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                
                //Make sysop user
                $user = User::newFromName( 'UTSysop' );
+               
                if ( $user->idForName() == 0 ) {
                        $user->addToDatabase();
                        $user->setPassword( 'UTSysopPassword' );
@@ -140,7 +143,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                if ( $dbType == 'oracle' ) {
                        # Insert 0 user to prevent FK violations
-
+                       
                        # Anonymous user
                        $this->db->insert( 'user', array(
                                'user_id'         => 0,
@@ -149,7 +152,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                
        }
        
-       private function destroyDB() {
+       protected function destroyDB() {
                if ( !self::$databaseSetupDone ) {
                        return;
                }
index d1e45c8..fcc10a8 100644 (file)
@@ -3,71 +3,62 @@
 require_once( dirname( __FILE__ ) . '/ParserHelpers.php' );
 require_once( dirname(dirname(dirname( __FILE__ ))) . '/bootstrap.php' );
 
+/**
+ * @group Parser
+ * @group Destructive
+ * @group Database
+ */
 class MediaWikiParserTest extends MediaWikiTestCase {
        public $count;          // Number of tests in the suite.
-       public $backend;        // ParserTestSuiteBackend instance
        public $articles = array();     // Array of test articles defined by the tests
-
-       public function __construct() {
-               $suite = new PHPUnit_Framework_TestSuite('Parser Tests');
-               parent::__construct($suite);
-               $this->backend = new ParserTestSuiteBackend;
-               $this->setName( 'Parser tests' );
+       protected $pt;
+       
+       function setUp() {
+               global $wgContLang;
+               $wgContLang = Language::factory( 'en' );
+               
+               $this->pt = new PHPUnitParserTest;
+               $this->pt->setupDatabase();
+               
        }
-
-       public static function suite() {
-               global $IP;
-
-               $tester = new self;
-
-               $iter = new TestFileIterator( "$IP/tests/parser/parserTests.txt", $tester );
-               $tester->count = 0;
-
-               foreach ( $iter as $test ) {
-                       $tester->suite->addTest( new ParserUnitTest( $tester, $test ), array( 'Parser', 'Destructive', 'Database', 'Broken' ) );
-                       $tester->count++;
+       
+       function tearDown() {
+               if( is_object( $this->pt ) && $this->pt instanceof PHPUnitParserTest ) {
+                       $this->pt->teardownDatabase();
+                       $this->pt = null;
                }
-
-               return $tester->suite;
-       }
-
-       public function count() {
-               return $this->count;
-       }
-
-       public function toString() {
-               return "MediaWiki Parser Tests";
        }
 
-       public function getBackend() {
-               return $this->backend;
-       }
-
-       public function getIterator() {
-               return $this->iterator;
-       }
-
-       public function publishTestArticles() {
-               if ( empty( $this->articles ) ) {
-                       return;
-               }
-
-               foreach ( $this->articles as $name => $text ) {
-                       $title = Title::newFromText( $name );
-
-                       if ( $title->getArticleID( Title::GAID_FOR_UPDATE ) == 0 ) {
-                               ParserTest::addArticle( $name, $text );
+       
+       public function testParserTests() {
+               //global $IP;
+               //$wgParserTestFiles = array( "$IP/tests/parser/testparserTests.txt" );
+               
+               global $wgParserTestFiles;
+               
+               foreach( $wgParserTestFiles as $file ) {
+                       
+                       $iter = new TestFileIterator( $file, $this->pt );
+                       
+                       try {
+                               foreach( $iter as $test ) {
+                                       $r = $this->pt->runTest( $test['test'], $test['input'],
+                                               $test['result'], $test['options'], $test['config']
+                                       );
+                                       
+                                       $this->assertTrue( $r, 'Parser test ' . $test['test'] );
+                                       
+                               }
+                       } 
+                       catch( DBQueryError $e ) {
+                               $this->assertTrue( false, 'Parser test ' . $test['test'] . ' (error: "' . $e->getMessage() . '")' );
+                               //This is annoying... it always stops on error and doesn't go to the next one.
+                               continue;
                        }
+                       
                }
-               $this->articles = array();
+               
        }
 
-       public function addArticle( $name, $text, $line ) {
-               $this->articles[$name] = $text;
-       }
-
-       public function showRunFile( $path ) {
-               /* Nothing shown when run from phpunit */
-       }
 }
 
index f490a9c..ad0bfd1 100644 (file)
@@ -2,18 +2,29 @@
 
 class PHPUnitParserTest extends ParserTest {
        function showTesting( $desc ) {
+               global $additionalMWCLIArgs;
+               if( $additionalMWCLIArgs['verbose'] ) parent::showTesting( $desc );
+               //var_dump($options);
                /* Do nothing since we don't want to show info during PHPUnit testing. */
        }
 
        public function showSuccess( $desc ) {
-               PHPUnit_Framework_Assert::assertTrue( true, $desc );
+               global $additionalMWCLIArgs;
+               
+               if( $additionalMWCLIArgs['verbose'] ) parent::showSuccess( $desc );
                return true;
        }
 
        public function showFailure( $desc, $expected, $got ) {
-               PHPUnit_Framework_Assert::assertEquals( $expected, $got, $desc );
+               global $additionalMWCLIArgs;
+               
+               if( $additionalMWCLIArgs['verbose'] ) parent::showFailure( $desc, $expected, $got );
                return false;
        }
+       
+       public function setupRecorder( $options ) {
+               $this->recorder = new PHPUnitTestRecorder( $this );
+       }
 }
 
 class ParserUnitTest extends MediaWikiTestCase {
index ddba247..2b3069d 100755 (executable)
@@ -14,6 +14,8 @@ $IP = dirname( dirname( dirname( __FILE__ ) ) );
 // Set a flag which can be used to detect when other scripts have been entered through this entry point or not
 define( 'MW_PHPUNIT_TEST', true );
 
+$options = array( 'quiet' );
+
 // Start up MediaWiki in command-line mode
 require_once( "$IP/maintenance/commandLine.inc" );
 
@@ -28,4 +30,11 @@ if( version_compare( PHPUnit_Runner_Version::id(), '3.5.0', '>=' ) ) {
        # Keep the old pre PHPUnit 3.5.0 behaviour for compatibility
        require_once( 'PHPUnit/TextUI/Command.php' );
 }
-PHPUnit_TextUI_Command::main();
+
+$additionalMWCLIArgs = array(
+       'verbose' => false,
+);
+
+require_once( "$IP/tests/phpunit/MediaWikiPHPUnitCommand.php" );
+MediaWikiPHPUnitCommand::main();
+
index f384d84..7efbe86 100644 (file)
@@ -6,7 +6,7 @@
          convertErrorsToExceptions="true"
          convertNoticesToExceptions="true"
          convertWarningsToExceptions="true"
-         stopOnFailure="true"
+         stopOnFailure="false"
          strict="true"
                 verbose="true">
        <testsuites>