PHPUnit now recognizes extension parser tests
authorAntoine Musso <hashar@free.fr>
Fri, 10 May 2013 19:20:12 +0000 (21:20 +0200)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 17 Jun 2013 15:21:31 +0000 (15:21 +0000)
Parser tests are registeredd by appending one or more .txt files to the
$wgParserTestFiles global setting.  Since this is shared with MediaWiki
core, I have made MediaWikiParserTest a factory of PHPUnit testsuite
which would filter in/out extensions tests.

The `extensions` test suite now has a second test suite builder which is
simply a wrapper around MediaWikiParserTest factory.

Play cases:

  $ php phpunit.php --group Parser --tap

Runs any parser tests including the ones coming from extensions.

With an extension having parser tests such as Cite:

  $ php phpunit.php --testsuite extensions --tap
  // Extensions tests are run including parser tests.

bug: 42506
Change-Id: Icc3e9d30706b32149aa9dd18552e4241ec4af67e

tests/TestsAutoLoader.php
tests/phpunit/includes/parser/MediaWikiParserTest.php
tests/phpunit/suite.xml
tests/phpunit/suites/ExtensionsParserTestSuite.php [new file with mode: 0644]

index 1e9c2af..a5c18e0 100644 (file)
@@ -73,6 +73,7 @@ $wgAutoloadClasses += array(
 
        # tests/phpunit/includes/parser
        'NewParserTest' => "$testDir/phpunit/includes/parser/NewParserTest.php",
+       'MediaWikiParserTest' => "$testDir/phpunit/includes/parser/MediaWikiParserTest.php",
 
        # tests/phpunit/includes/libs
        'GenericArrayObjectTest' => "$testDir/phpunit/includes/libs/GenericArrayObjectTest.php",
index 22737b3..87dc41c 100644 (file)
@@ -11,12 +11,79 @@ require_once __DIR__ . '/NewParserTest.php';
  */
 class MediaWikiParserTest {
 
-       public static function suite() {
-               global $wgParserTestFiles;
+       /**
+        * @defgroup filtering_constants Filtering constants
+        *
+        * Limit inclusion of parser tests files coming from MediaWiki core
+        * @{
+        */
 
-               $suite = new PHPUnit_Framework_TestSuite;
+       /** Include files shipped with MediaWiki core */
+       const CORE_ONLY = 1;
+       /** Include non core files as set in $wgParserTestFiles */
+       const NO_CORE = 2;
+       /** Include anything set via $wgParserTestFiles */
+       const WITH_ALL  = 3;  # CORE_ONLY | NO_CORE
+
+       /** @} */
+
+       /**
+        * Get a PHPUnit test suite of parser tests. Optionally filtered with
+        * $flags.
+        *
+        * @par Examples:
+        * Get a suite of parser tests shipped by MediaWiki core:
+        * @code
+        * MediaWikiParserTest::suite( MediaWikiParserTest::CORE_ONLY );
+        * @endcode
+        * Get a suite of various parser tests, like extensions:
+        * @code
+        * MediaWikiParserTest::suite( MediaWikiParserTest::NO_CORE );
+        * @endcode
+        * Get any test defined via $wgParserTestFiles:
+        * @code
+        * MediaWikiParserTest::suite( MediaWikiParserTest::WITH_ALL );
+        * @endcode
+        *
+        * @param $flags bitwise flag to filter out the $wgParserTestFiles that
+        * will be included.  Default: MediaWikiParserTest::CORE_ONLY
+        *
+        * @return PHPUnit_Framework_TestSuite
+        */
+       public static function suite( $flags = self::CORE_ONLY ) {
+               if( is_string( $flags ) ) {
+                       $flags = self::CORE_ONLY;
+               }
+               global $wgParserTestFiles, $IP;
+
+               $mwTestDir = $IP.'/tests/';
+
+               # Human friendly helpers
+               $wantsCore = ($flags & self::CORE_ONLY);
+               $wantsRest = ($flags & self::NO_CORE);
+
+               # Will hold the .txt parser test files we will include
+               $filesToTest = array();
+
+               # Filter out .txt files
+               foreach( $wgParserTestFiles as $parserTestFile ) {
+                       $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
 
-               foreach ( $wgParserTestFiles as $fileName ) {
+                       if( $isCore && $wantsCore ) {
+                               self::debug( "included core parser tests: $parserTestFile" );
+                               $filesToTest[] = $parserTestFile;
+                       } elseif( !$isCore && $wantsRest ) {
+                               self::debug( "included non core parser tests: $parserTestFile" );
+                               $filesToTest[] = $parserTestFile;
+                       } else {
+                               self::debug( "skipped parser tests: $parserTestFile" );
+                       }
+               }
+               self::debug( 'parser tests files: '
+                       . implode(' ', $filesToTest) );
+
+               $suite = new PHPUnit_Framework_TestSuite;
+               foreach ( $filesToTest as $fileName ) {
                        $testsName = basename( $fileName, '.txt' );
                        $escapedFileName = strtr( $fileName, array( "'" => "\\'", '\\' => '\\\\' ) );
                        /* This used to be ucfirst( basename( dirname( $filename ) ) )
@@ -37,11 +104,17 @@ class $parserTestClassName extends NewParserTest {
 EOT;
 
                        eval( $parserTestClassDefinition );
-
-                       $parserTester = new $parserTestClassName( $testsName );
-                       $suite->addTestSuite( new ReflectionClass ( $parserTester ) );
+                       self::debug( "Adding test class $parserTestClassName" );
+                       $suite->addTestSuite( $parserTestClassName );
                }
-
                return $suite;
        }
+
+       /**
+        * Write $msg under log group 'tests-parser'
+        * @param string $msg Message to log
+        */
+       protected static function debug( $msg ) {
+               return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );
+       }
 }
index 844c853..e1043b1 100644 (file)
@@ -36,6 +36,7 @@
                </testsuite>
                <testsuite name="extensions">
                        <file>suites/ExtensionsTestSuite.php</file>
+                       <file>suites/ExtensionsParserTestSuite.php</file>
                </testsuite>
        </testsuites>
        <groups>
diff --git a/tests/phpunit/suites/ExtensionsParserTestSuite.php b/tests/phpunit/suites/ExtensionsParserTestSuite.php
new file mode 100644 (file)
index 0000000..3d68b24
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+class ExtensionsParserTestSuite extends PHPUnit_Framework_TestSuite {
+
+       public static function suite() {
+               return MediaWikiParserTest::suite( MediaWikiParserTest::NO_CORE );
+       }
+
+}