Merge "Add tests for WikiMap and WikiReference"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / MediaWikiParserTest.php
1 <?php
2 require_once __DIR__ . '/NewParserTest.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 Database
11 */
12 class MediaWikiParserTest {
13
14 /**
15 * @defgroup filtering_constants Filtering constants
16 *
17 * Limit inclusion of parser tests files coming from MediaWiki core
18 * @{
19 */
20
21 /** Include files shipped with MediaWiki core */
22 const CORE_ONLY = 1;
23 /** Include non core files as set in $wgParserTestFiles */
24 const NO_CORE = 2;
25 /** Include anything set via $wgParserTestFiles */
26 const WITH_ALL = 3; # CORE_ONLY | NO_CORE
27
28 /** @} */
29
30 /**
31 * Get a PHPUnit test suite of parser tests. Optionally filtered with
32 * $flags.
33 *
34 * @par Examples:
35 * Get a suite of parser tests shipped by MediaWiki core:
36 * @code
37 * MediaWikiParserTest::suite( MediaWikiParserTest::CORE_ONLY );
38 * @endcode
39 * Get a suite of various parser tests, like extensions:
40 * @code
41 * MediaWikiParserTest::suite( MediaWikiParserTest::NO_CORE );
42 * @endcode
43 * Get any test defined via $wgParserTestFiles:
44 * @code
45 * MediaWikiParserTest::suite( MediaWikiParserTest::WITH_ALL );
46 * @endcode
47 *
48 * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
49 * will be included. Default: MediaWikiParserTest::CORE_ONLY
50 *
51 * @return PHPUnit_Framework_TestSuite
52 */
53 public static function suite( $flags = self::CORE_ONLY ) {
54 if ( is_string( $flags ) ) {
55 $flags = self::CORE_ONLY;
56 }
57 global $wgParserTestFiles, $IP;
58
59 $mwTestDir = $IP . '/tests/';
60
61 # Human friendly helpers
62 $wantsCore = ( $flags & self::CORE_ONLY );
63 $wantsRest = ( $flags & self::NO_CORE );
64
65 # Will hold the .txt parser test files we will include
66 $filesToTest = array();
67
68 # Filter out .txt files
69 foreach ( $wgParserTestFiles as $parserTestFile ) {
70 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
71
72 if ( $isCore && $wantsCore ) {
73 self::debug( "included core parser tests: $parserTestFile" );
74 $filesToTest[] = $parserTestFile;
75 } elseif ( !$isCore && $wantsRest ) {
76 self::debug( "included non core parser tests: $parserTestFile" );
77 $filesToTest[] = $parserTestFile;
78 } else {
79 self::debug( "skipped parser tests: $parserTestFile" );
80 }
81 }
82 self::debug( 'parser tests files: '
83 . implode( ' ', $filesToTest ) );
84
85 $suite = new PHPUnit_Framework_TestSuite;
86 $testList = array();
87 $counter = 0;
88 foreach ( $filesToTest as $fileName ) {
89 // Call the highest level directory the extension name.
90 // It may or may not actually be, but it should be close
91 // enough to cause there to be separate names for different
92 // things, which is good enough for our purposes.
93 $extensionName = basename( dirname( $fileName ) );
94 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
95 $escapedFileName = strtr( $fileName, array( "'" => "\\'", '\\' => '\\\\' ) );
96 $parserTestClassName = ucfirst( $testsName );
97 // Official spec for class names: http://php.net/manual/en/language.oop5.basic.php
98 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
99 $parserTestClassName = 'ParserTest_' . preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
100 if ( isset( $testList[$parserTestClassName] ) ) {
101 // If a conflict happens, gives a very unclear fatal.
102 // So as a last ditch effort to prevent that eventuality, if there
103 // is a conflict, append a number.
104 $counter++;
105 $parserTestClassName .= $counter;
106 }
107 $testList[$parserTestClassName] = true;
108 $parserTestClassDefinition = <<<EOT
109 /**
110 * @group Database
111 * @group Parser
112 * @group ParserTests
113 * @group ParserTests_$parserTestClassName
114 */
115 class $parserTestClassName extends NewParserTest {
116 protected \$file = '$escapedFileName';
117 }
118 EOT;
119
120 eval( $parserTestClassDefinition );
121 self::debug( "Adding test class $parserTestClassName" );
122 $suite->addTestSuite( $parserTestClassName );
123 }
124 return $suite;
125 }
126
127 /**
128 * Write $msg under log group 'tests-parser'
129 * @param string $msg Message to log
130 */
131 protected static function debug( $msg ) {
132 return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );
133 }
134 }