test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / parser / MediaWikiParserTest.php
blob87dc41c1046d0a35cf28c7f41eb833a1679be02f
1 <?php
2 require_once __DIR__ . '/NewParserTest.php';
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
9 * @group Parser
10 * @group Database
12 class MediaWikiParserTest {
14 /**
15 * @defgroup filtering_constants Filtering constants
17 * Limit inclusion of parser tests files coming from MediaWiki core
18 * @{
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
28 /** @} */
30 /**
31 * Get a PHPUnit test suite of parser tests. Optionally filtered with
32 * $flags.
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
48 * @param $flags bitwise flag to filter out the $wgParserTestFiles that
49 * will be included. Default: MediaWikiParserTest::CORE_ONLY
51 * @return PHPUnit_Framework_TestSuite
53 public static function suite( $flags = self::CORE_ONLY ) {
54 if( is_string( $flags ) ) {
55 $flags = self::CORE_ONLY;
57 global $wgParserTestFiles, $IP;
59 $mwTestDir = $IP.'/tests/';
61 # Human friendly helpers
62 $wantsCore = ($flags & self::CORE_ONLY);
63 $wantsRest = ($flags & self::NO_CORE);
65 # Will hold the .txt parser test files we will include
66 $filesToTest = array();
68 # Filter out .txt files
69 foreach( $wgParserTestFiles as $parserTestFile ) {
70 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
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" );
82 self::debug( 'parser tests files: '
83 . implode(' ', $filesToTest) );
85 $suite = new PHPUnit_Framework_TestSuite;
86 foreach ( $filesToTest as $fileName ) {
87 $testsName = basename( $fileName, '.txt' );
88 $escapedFileName = strtr( $fileName, array( "'" => "\\'", '\\' => '\\\\' ) );
89 /* This used to be ucfirst( basename( dirname( $filename ) ) )
90 * and then was ucfirst( basename( $filename, '.txt' )
91 * but that didn't work with names like foo.tests.txt
93 $parserTestClassName = str_replace( '.', '_', ucfirst( $testsName ) );
94 $parserTestClassDefinition = <<<EOT
95 /**
96 * @group Database
97 * @group Parser
98 * @group ParserTests
99 * @group ParserTests_$parserTestClassName
101 class $parserTestClassName extends NewParserTest {
102 protected \$file = '$escapedFileName';
104 EOT;
106 eval( $parserTestClassDefinition );
107 self::debug( "Adding test class $parserTestClassName" );
108 $suite->addTestSuite( $parserTestClassName );
110 return $suite;
114 * Write $msg under log group 'tests-parser'
115 * @param string $msg Message to log
117 protected static function debug( $msg ) {
118 return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );