3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\ScopedCallback
;
7 * The UnitTest must be either a class that inherits from MediaWikiTestCase
8 * or a class that provides a public static suite() method which returns
9 * an PHPUnit_Framework_Test object
15 class ParserTestTopLevelSuite
extends PHPUnit_Framework_TestSuite
{
16 /** @var ParserTestRunner */
19 /** @var ScopedCallback */
20 private $ptTeardownScope;
22 private $oldTablePrefix = '';
25 * @defgroup filtering_constants Filtering constants
27 * Limit inclusion of parser tests files coming from MediaWiki core
31 /** Include files shipped with MediaWiki core */
33 /** Include non core files as set in $wgParserTestFiles */
35 /** Include anything set via $wgParserTestFiles */
36 const WITH_ALL
= self
::CORE_ONLY | self
::NO_CORE
;
41 * Get a PHPUnit test suite of parser tests. Optionally filtered with
45 * Get a suite of parser tests shipped by MediaWiki core:
47 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::CORE_ONLY );
49 * Get a suite of various parser tests, like extensions:
51 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::NO_CORE );
53 * Get any test defined via $wgParserTestFiles:
55 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::WITH_ALL );
58 * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
59 * will be included. Default: ParserTestTopLevelSuite::CORE_ONLY
61 * @return PHPUnit_Framework_TestSuite
63 public static function suite( $flags = self
::CORE_ONLY
) {
64 return new self( $flags );
67 function __construct( $flags ) {
68 parent
::__construct();
70 $this->ptRecorder
= new PhpunitTestRecorder
;
71 $this->ptRunner
= new ParserTestRunner( $this->ptRecorder
);
73 if ( is_string( $flags ) ) {
74 $flags = self
::CORE_ONLY
;
78 $mwTestDir = $IP . '/tests/';
80 # Human friendly helpers
81 $wantsCore = ( $flags & self
::CORE_ONLY
);
82 $wantsRest = ( $flags & self
::NO_CORE
);
84 # Will hold the .txt parser test files we will include
87 # Filter out .txt files
88 $files = ParserTestRunner
::getParserTestFiles();
89 foreach ( $files as $extName => $parserTestFile ) {
90 $isCore = ( strpos( $parserTestFile, $mwTestDir ) === 0 );
92 if ( $isCore && $wantsCore ) {
93 self
::debug( "included core parser tests: $parserTestFile" );
94 $filesToTest[$extName] = $parserTestFile;
95 } elseif ( !$isCore && $wantsRest ) {
96 self
::debug( "included non core parser tests: $parserTestFile" );
97 $filesToTest[$extName] = $parserTestFile;
99 self
::debug( "skipped parser tests: $parserTestFile" );
102 self
::debug( 'parser tests files: '
103 . implode( ' ', $filesToTest ) );
107 foreach ( $filesToTest as $extensionName => $fileName ) {
108 if ( is_int( $extensionName ) ) {
109 // If there's no extension name because this is coming
110 // from the legacy global, then assume the next level directory
111 // is the extension name (e.g. extensions/FooBar/parserTests.txt).
112 $extensionName = basename( dirname( $fileName ) );
114 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
115 $parserTestClassName = ucfirst( $testsName );
117 // Official spec for class names: https://www.php.net/manual/en/language.oop5.basic.php
118 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
119 $parserTestClassName = 'ParserTest_' .
120 preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
122 if ( isset( $testList[$parserTestClassName] ) ) {
123 // If there is a conflict, append a number.
125 $parserTestClassName .= $counter;
127 $testList[$parserTestClassName] = true;
129 // Previously we actually created a class here, with eval(). We now
130 // just override the name.
132 self
::debug( "Adding test class $parserTestClassName" );
133 $this->addTest( new ParserTestFileSuite(
134 $this->ptRunner
, $parserTestClassName, $fileName ) );
138 public function setUp() {
139 wfDebug( __METHOD__
);
141 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
142 $db = $lb->getConnection( DB_MASTER
);
143 $type = $db->getType();
144 $prefix = $type === 'oracle' ?
145 MediaWikiTestCase
::ORA_DB_PREFIX
: MediaWikiTestCase
::DB_PREFIX
;
146 $this->oldTablePrefix
= $db->tablePrefix();
147 MediaWikiTestCase
::setupTestDB( $db, $prefix );
148 CloneDatabase
::changePrefix( $prefix );
150 $this->ptRunner
->setDatabase( $db );
152 MediaWikiTestCase
::resetNonServiceCaches();
154 MediaWikiTestCase
::installMockMwServices();
155 $teardown = new ScopedCallback( function () {
156 MediaWikiTestCase
::restoreMwServices();
159 $teardown = $this->ptRunner
->setupUploads( $teardown );
160 $this->ptTeardownScope
= $teardown;
163 public function tearDown() {
164 wfDebug( __METHOD__
);
165 if ( $this->ptTeardownScope
) {
166 ScopedCallback
::consume( $this->ptTeardownScope
);
168 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
172 * Write $msg under log group 'tests-parser'
173 * @param string $msg Message to log
175 protected static function debug( $msg ) {
176 wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );