4 * The UnitTest must be either a class that inherits from MediaWikiTestCase
5 * or a class that provides a public static suite() method which returns
6 * an PHPUnit_Framework_Test object
12 class ParserTestTopLevelSuite
extends PHPUnit_Framework_TestSuite
{
13 /** @var ParserTestRunner */
16 /** @var ScopedCallback */
17 private $ptTeardownScope;
20 * @defgroup filtering_constants Filtering constants
22 * Limit inclusion of parser tests files coming from MediaWiki core
26 /** Include files shipped with MediaWiki core */
28 /** Include non core files as set in $wgParserTestFiles */
30 /** Include anything set via $wgParserTestFiles */
31 const WITH_ALL
= 3; # CORE_ONLY | NO_CORE
36 * Get a PHPUnit test suite of parser tests. Optionally filtered with
40 * Get a suite of parser tests shipped by MediaWiki core:
42 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::CORE_ONLY );
44 * Get a suite of various parser tests, like extensions:
46 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::NO_CORE );
48 * Get any test defined via $wgParserTestFiles:
50 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::WITH_ALL );
53 * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
54 * will be included. Default: ParserTestTopLevelSuite::CORE_ONLY
56 * @return PHPUnit_Framework_TestSuite
58 public static function suite( $flags = self
::CORE_ONLY
) {
59 return new self( $flags );
62 function __construct( $flags ) {
63 parent
::__construct();
65 $this->ptRecorder
= new PhpunitTestRecorder
;
66 $this->ptRunner
= new ParserTestRunner( $this->ptRecorder
);
68 if ( is_string( $flags ) ) {
69 $flags = self
::CORE_ONLY
;
71 global $wgParserTestFiles, $IP;
73 $mwTestDir = $IP . '/tests/';
75 # Human friendly helpers
76 $wantsCore = ( $flags & self
::CORE_ONLY
);
77 $wantsRest = ( $flags & self
::NO_CORE
);
79 # Will hold the .txt parser test files we will include
82 # Filter out .txt files
83 foreach ( $wgParserTestFiles as $parserTestFile ) {
84 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
86 if ( $isCore && $wantsCore ) {
87 self
::debug( "included core parser tests: $parserTestFile" );
88 $filesToTest[] = $parserTestFile;
89 } elseif ( !$isCore && $wantsRest ) {
90 self
::debug( "included non core parser tests: $parserTestFile" );
91 $filesToTest[] = $parserTestFile;
93 self
::debug( "skipped parser tests: $parserTestFile" );
96 self
::debug( 'parser tests files: '
97 . implode( ' ', $filesToTest ) );
101 foreach ( $filesToTest as $fileName ) {
102 // Call the highest level directory the extension name.
103 // It may or may not actually be, but it should be close
104 // enough to cause there to be separate names for different
105 // things, which is good enough for our purposes.
106 $extensionName = basename( dirname( $fileName ) );
107 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
108 $parserTestClassName = ucfirst( $testsName );
110 // Official spec for class names: http://php.net/manual/en/language.oop5.basic.php
111 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
112 $parserTestClassName = 'ParserTest_' .
113 preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
115 if ( isset( $testList[$parserTestClassName] ) ) {
116 // If there is a conflict, append a number.
118 $parserTestClassName .= $counter;
120 $testList[$parserTestClassName] = true;
122 // Previously we actually created a class here, with eval(). We now
123 // just override the name.
125 self
::debug( "Adding test class $parserTestClassName" );
126 $this->addTest( new ParserTestFileSuite(
127 $this->ptRunner
, $parserTestClassName, $fileName ) );
131 public function setUp() {
132 wfDebug( __METHOD__
);
133 $db = wfGetDB( DB_MASTER
);
134 $type = $db->getType();
135 $prefix = $type === 'oracle' ?
136 MediaWikiTestCase
::ORA_DB_PREFIX
: MediaWikiTestCase
::DB_PREFIX
;
137 MediaWikiTestCase
::setupTestDB( $db, $prefix );
138 $teardown = $this->ptRunner
->setDatabase( $db );
139 $teardown = $this->ptRunner
->setupUploads( $teardown );
140 $this->ptTeardownScope
= $teardown;
143 public function tearDown() {
144 wfDebug( __METHOD__
);
145 if ( $this->ptTeardownScope
) {
146 ScopedCallback
::consume( $this->ptTeardownScope
);
151 * Write $msg under log group 'tests-parser'
152 * @param string $msg Message to log
154 protected static function debug( $msg ) {
155 return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );