2 require_once __DIR__
. '/NewParserTest.php';
5 * The UnitTest must be either a class that inherits from MediaWikiTestCase
6 * or a class that provides a public static suite() method which returns
7 * an PHPUnit_Framework_Test object
13 class MediaWikiParserTest
{
16 * @defgroup filtering_constants Filtering constants
18 * Limit inclusion of parser tests files coming from MediaWiki core
22 /** Include files shipped with MediaWiki core */
24 /** Include non core files as set in $wgParserTestFiles */
26 /** Include anything set via $wgParserTestFiles */
27 const WITH_ALL
= 3; # CORE_ONLY | NO_CORE
32 * Get a PHPUnit test suite of parser tests. Optionally filtered with
36 * Get a suite of parser tests shipped by MediaWiki core:
38 * MediaWikiParserTest::suite( MediaWikiParserTest::CORE_ONLY );
40 * Get a suite of various parser tests, like extensions:
42 * MediaWikiParserTest::suite( MediaWikiParserTest::NO_CORE );
44 * Get any test defined via $wgParserTestFiles:
46 * MediaWikiParserTest::suite( MediaWikiParserTest::WITH_ALL );
49 * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
50 * will be included. Default: MediaWikiParserTest::CORE_ONLY
52 * @return PHPUnit_Framework_TestSuite
54 public static function suite( $flags = self
::CORE_ONLY
) {
55 if ( is_string( $flags ) ) {
56 $flags = self
::CORE_ONLY
;
58 global $wgParserTestFiles, $IP;
60 $mwTestDir = $IP . '/tests/';
62 # Human friendly helpers
63 $wantsCore = ( $flags & self
::CORE_ONLY
);
64 $wantsRest = ( $flags & self
::NO_CORE
);
66 # Will hold the .txt parser test files we will include
67 $filesToTest = array();
69 # Filter out .txt files
70 foreach ( $wgParserTestFiles as $parserTestFile ) {
71 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
73 if ( $isCore && $wantsCore ) {
74 self
::debug( "included core parser tests: $parserTestFile" );
75 $filesToTest[] = $parserTestFile;
76 } elseif ( !$isCore && $wantsRest ) {
77 self
::debug( "included non core parser tests: $parserTestFile" );
78 $filesToTest[] = $parserTestFile;
80 self
::debug( "skipped parser tests: $parserTestFile" );
83 self
::debug( 'parser tests files: '
84 . implode( ' ', $filesToTest ) );
86 $suite = new PHPUnit_Framework_TestSuite
;
89 foreach ( $filesToTest as $fileName ) {
90 // Call the highest level directory the extension name.
91 // It may or may not actually be, but it should be close
92 // enough to cause there to be separate names for different
93 // things, which is good enough for our purposes.
94 $extensionName = basename( dirname( $fileName ) );
95 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
96 $escapedFileName = strtr( $fileName, array( "'" => "\\'", '\\' => '\\\\' ) );
97 $parserTestClassName = ucfirst( $testsName );
99 // Official spec for class names: http://php.net/manual/en/language.oop5.basic.php
100 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
101 $parserTestClassName = 'ParserTest_' .
102 preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
104 if ( isset( $testList[$parserTestClassName] ) ) {
105 // If a conflict happens, gives a very unclear fatal.
106 // So as a last ditch effort to prevent that eventuality, if there
107 // is a conflict, append a number.
109 $parserTestClassName .= $counter;
111 $testList[$parserTestClassName] = true;
112 $parserTestClassDefinition = <<<EOT
117 * @group ParserTests_$parserTestClassName
119 class $parserTestClassName extends NewParserTest {
120 protected \$file = '$escapedFileName';
124 eval( $parserTestClassDefinition );
125 self
::debug( "Adding test class $parserTestClassName" );
126 $suite->addTestSuite( $parserTestClassName );
132 * Write $msg under log group 'tests-parser'
133 * @param string $msg Message to log
135 protected static function debug( $msg ) {
136 return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );