2 use Wikimedia\ScopedCallback
;
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
13 class ParserTestTopLevelSuite
extends PHPUnit_Framework_TestSuite
{
14 /** @var ParserTestRunner */
17 /** @var ScopedCallback */
18 private $ptTeardownScope;
20 private $oldTablePrefix = '';
23 * @defgroup filtering_constants Filtering constants
25 * Limit inclusion of parser tests files coming from MediaWiki core
29 /** Include files shipped with MediaWiki core */
31 /** Include non core files as set in $wgParserTestFiles */
33 /** Include anything set via $wgParserTestFiles */
34 const WITH_ALL
= self
::CORE_ONLY | self
::NO_CORE
;
39 * Get a PHPUnit test suite of parser tests. Optionally filtered with
43 * Get a suite of parser tests shipped by MediaWiki core:
45 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::CORE_ONLY );
47 * Get a suite of various parser tests, like extensions:
49 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::NO_CORE );
51 * Get any test defined via $wgParserTestFiles:
53 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::WITH_ALL );
56 * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
57 * will be included. Default: ParserTestTopLevelSuite::CORE_ONLY
59 * @return PHPUnit_Framework_TestSuite
61 public static function suite( $flags = self
::CORE_ONLY
) {
62 return new self( $flags );
65 function __construct( $flags ) {
66 parent
::__construct();
68 $this->ptRecorder
= new PhpunitTestRecorder
;
69 $this->ptRunner
= new ParserTestRunner( $this->ptRecorder
);
71 if ( is_string( $flags ) ) {
72 $flags = self
::CORE_ONLY
;
76 $mwTestDir = $IP . '/tests/';
78 # Human friendly helpers
79 $wantsCore = ( $flags & self
::CORE_ONLY
);
80 $wantsRest = ( $flags & self
::NO_CORE
);
82 # Will hold the .txt parser test files we will include
85 # Filter out .txt files
86 $files = ParserTestRunner
::getParserTestFiles();
87 foreach ( $files as $extName => $parserTestFile ) {
88 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
90 if ( $isCore && $wantsCore ) {
91 self
::debug( "included core parser tests: $parserTestFile" );
92 $filesToTest[$extName] = $parserTestFile;
93 } elseif ( !$isCore && $wantsRest ) {
94 self
::debug( "included non core parser tests: $parserTestFile" );
95 $filesToTest[$extName] = $parserTestFile;
97 self
::debug( "skipped parser tests: $parserTestFile" );
100 self
::debug( 'parser tests files: '
101 . implode( ' ', $filesToTest ) );
105 foreach ( $filesToTest as $extensionName => $fileName ) {
106 if ( is_int( $extensionName ) ) {
107 // If there's no extension name because this is coming
108 // from the legacy global, then assume the next level directory
109 // is the extension name (e.g. extensions/FooBar/parserTests.txt).
110 $extensionName = basename( dirname( $fileName ) );
112 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
113 $parserTestClassName = ucfirst( $testsName );
115 // Official spec for class names: https://secure.php.net/manual/en/language.oop5.basic.php
116 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
117 $parserTestClassName = 'ParserTest_' .
118 preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
120 if ( isset( $testList[$parserTestClassName] ) ) {
121 // If there is a conflict, append a number.
123 $parserTestClassName .= $counter;
125 $testList[$parserTestClassName] = true;
127 // Previously we actually created a class here, with eval(). We now
128 // just override the name.
130 self
::debug( "Adding test class $parserTestClassName" );
131 $this->addTest( new ParserTestFileSuite(
132 $this->ptRunner
, $parserTestClassName, $fileName ) );
136 public function setUp() {
137 wfDebug( __METHOD__
);
138 $db = wfGetDB( DB_MASTER
);
139 $type = $db->getType();
140 $prefix = $type === 'oracle' ?
141 MediaWikiTestCase
::ORA_DB_PREFIX
: MediaWikiTestCase
::DB_PREFIX
;
142 $this->oldTablePrefix
= $db->tablePrefix();
143 MediaWikiTestCase
::setupTestDB( $db, $prefix );
144 CloneDatabase
::changePrefix( $prefix );
145 $teardown = $this->ptRunner
->setDatabase( $db );
146 $teardown = $this->ptRunner
->setupUploads( $teardown );
147 $this->ptTeardownScope
= $teardown;
150 public function tearDown() {
151 wfDebug( __METHOD__
);
152 if ( $this->ptTeardownScope
) {
153 ScopedCallback
::consume( $this->ptTeardownScope
);
155 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
159 * Write $msg under log group 'tests-parser'
160 * @param string $msg Message to log
162 protected static function debug( $msg ) {
163 wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );