MediaWiki
REL1_24
|
00001 <?php 00002 require_once __DIR__ . '/NewParserTest.php'; 00003 00012 class MediaWikiParserTest { 00013 00022 const CORE_ONLY = 1; 00024 const NO_CORE = 2; 00026 const WITH_ALL = 3; # CORE_ONLY | NO_CORE 00027 00053 public static function suite( $flags = self::CORE_ONLY ) { 00054 if ( is_string( $flags ) ) { 00055 $flags = self::CORE_ONLY; 00056 } 00057 global $wgParserTestFiles, $IP; 00058 00059 $mwTestDir = $IP . '/tests/'; 00060 00061 # Human friendly helpers 00062 $wantsCore = ( $flags & self::CORE_ONLY ); 00063 $wantsRest = ( $flags & self::NO_CORE ); 00064 00065 # Will hold the .txt parser test files we will include 00066 $filesToTest = array(); 00067 00068 # Filter out .txt files 00069 foreach ( $wgParserTestFiles as $parserTestFile ) { 00070 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) ); 00071 00072 if ( $isCore && $wantsCore ) { 00073 self::debug( "included core parser tests: $parserTestFile" ); 00074 $filesToTest[] = $parserTestFile; 00075 } elseif ( !$isCore && $wantsRest ) { 00076 self::debug( "included non core parser tests: $parserTestFile" ); 00077 $filesToTest[] = $parserTestFile; 00078 } else { 00079 self::debug( "skipped parser tests: $parserTestFile" ); 00080 } 00081 } 00082 self::debug( 'parser tests files: ' 00083 . implode( ' ', $filesToTest ) ); 00084 00085 $suite = new PHPUnit_Framework_TestSuite; 00086 $testList = array(); 00087 $counter = 0; 00088 foreach ( $filesToTest as $fileName ) { 00089 // Call the highest level directory the extension name. 00090 // It may or may not actually be, but it should be close 00091 // enough to cause there to be separate names for different 00092 // things, which is good enough for our purposes. 00093 $extensionName = basename( dirname( $fileName ) ); 00094 $testsName = $extensionName . '⁄' . basename( $fileName, '.txt' ); 00095 $escapedFileName = strtr( $fileName, array( "'" => "\\'", '\\' => '\\\\' ) ); 00096 $parserTestClassName = ucfirst( $testsName ); 00097 // Official spec for class names: http://php.net/manual/en/language.oop5.basic.php 00098 // Prepend 'ParserTest_' to be paranoid about it not starting with a number 00099 $parserTestClassName = 'ParserTest_' . preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName ); 00100 if ( isset( $testList[$parserTestClassName] ) ) { 00101 // If a conflict happens, gives a very unclear fatal. 00102 // So as a last ditch effort to prevent that eventuality, if there 00103 // is a conflict, append a number. 00104 $counter++; 00105 $parserTestClassName .= $counter; 00106 } 00107 $testList[$parserTestClassName] = true; 00108 $parserTestClassDefinition = <<<EOT 00115 class $parserTestClassName extends NewParserTest { 00116 protected \$file = '$escapedFileName'; 00117 } 00118 EOT; 00119 00120 eval( $parserTestClassDefinition ); 00121 self::debug( "Adding test class $parserTestClassName" ); 00122 $suite->addTestSuite( $parserTestClassName ); 00123 } 00124 return $suite; 00125 } 00126 00131 protected static function debug( $msg ) { 00132 return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg ); 00133 } 00134 }