Renames preparatory to parser tests refactor
[lhc/web/wiklou.git] / tests / phpunit / suites / ParserTestTopLevelSuite.php
1 <?php
2 require_once __DIR__ . '/../includes/parser/ParserIntegrationTest.php';
3
4 /**
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
8 *
9 * @group Parser
10 * @group ParserTests
11 * @group Database
12 */
13 class ParserTestTopLevelSuite {
14
15 /**
16 * @defgroup filtering_constants Filtering constants
17 *
18 * Limit inclusion of parser tests files coming from MediaWiki core
19 * @{
20 */
21
22 /** Include files shipped with MediaWiki core */
23 const CORE_ONLY = 1;
24 /** Include non core files as set in $wgParserTestFiles */
25 const NO_CORE = 2;
26 /** Include anything set via $wgParserTestFiles */
27 const WITH_ALL = 3; # CORE_ONLY | NO_CORE
28
29 /** @} */
30
31 /**
32 * Get a PHPUnit test suite of parser tests. Optionally filtered with
33 * $flags.
34 *
35 * @par Examples:
36 * Get a suite of parser tests shipped by MediaWiki core:
37 * @code
38 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::CORE_ONLY );
39 * @endcode
40 * Get a suite of various parser tests, like extensions:
41 * @code
42 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::NO_CORE );
43 * @endcode
44 * Get any test defined via $wgParserTestFiles:
45 * @code
46 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::WITH_ALL );
47 * @endcode
48 *
49 * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
50 * will be included. Default: ParserTestTopLevelSuite::CORE_ONLY
51 *
52 * @return PHPUnit_Framework_TestSuite
53 */
54 public static function suite( $flags = self::CORE_ONLY ) {
55 if ( is_string( $flags ) ) {
56 $flags = self::CORE_ONLY;
57 }
58 global $wgParserTestFiles, $IP;
59
60 $mwTestDir = $IP . '/tests/';
61
62 # Human friendly helpers
63 $wantsCore = ( $flags & self::CORE_ONLY );
64 $wantsRest = ( $flags & self::NO_CORE );
65
66 # Will hold the .txt parser test files we will include
67 $filesToTest = [];
68
69 # Filter out .txt files
70 foreach ( $wgParserTestFiles as $parserTestFile ) {
71 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
72
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;
79 } else {
80 self::debug( "skipped parser tests: $parserTestFile" );
81 }
82 }
83 self::debug( 'parser tests files: '
84 . implode( ' ', $filesToTest ) );
85
86 $suite = new PHPUnit_Framework_TestSuite;
87 $testList = [];
88 $counter = 0;
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, [ "'" => "\\'", '\\' => '\\\\' ] );
97 $parserTestClassName = ucfirst( $testsName );
98
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 );
103
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.
108 $counter++;
109 $parserTestClassName .= $counter;
110 }
111 $testList[$parserTestClassName] = true;
112 $parserTestClassDefinition = <<<EOT
113 /**
114 * @group Database
115 * @group Parser
116 * @group ParserTests
117 * @group ParserTests_$parserTestClassName
118 */
119 class $parserTestClassName extends ParserIntegrationTest {
120 protected \$file = '$escapedFileName';
121 }
122 EOT;
123
124 eval( $parserTestClassDefinition );
125 self::debug( "Adding test class $parserTestClassName" );
126 $suite->addTestSuite( $parserTestClassName );
127 }
128 return $suite;
129 }
130
131 /**
132 * Write $msg under log group 'tests-parser'
133 * @param string $msg Message to log
134 */
135 protected static function debug( $msg ) {
136 return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );
137 }
138 }