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;
21 * @defgroup filtering_constants Filtering constants
23 * Limit inclusion of parser tests files coming from MediaWiki core
27 /** Include files shipped with MediaWiki core */
29 /** Include non core files as set in $wgParserTestFiles */
31 /** Include anything set via $wgParserTestFiles */
32 const WITH_ALL
= 3; # CORE_ONLY | NO_CORE
37 * Get a PHPUnit test suite of parser tests. Optionally filtered with
41 * Get a suite of parser tests shipped by MediaWiki core:
43 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::CORE_ONLY );
45 * Get a suite of various parser tests, like extensions:
47 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::NO_CORE );
49 * Get any test defined via $wgParserTestFiles:
51 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::WITH_ALL );
54 * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
55 * will be included. Default: ParserTestTopLevelSuite::CORE_ONLY
57 * @return PHPUnit_Framework_TestSuite
59 public static function suite( $flags = self
::CORE_ONLY
) {
60 return new self( $flags );
63 function __construct( $flags ) {
64 parent
::__construct();
66 $this->ptRecorder
= new PhpunitTestRecorder
;
67 $this->ptRunner
= new ParserTestRunner( $this->ptRecorder
);
69 if ( is_string( $flags ) ) {
70 $flags = self
::CORE_ONLY
;
72 global $wgParserTestFiles, $IP;
74 $mwTestDir = $IP . '/tests/';
76 # Human friendly helpers
77 $wantsCore = ( $flags & self
::CORE_ONLY
);
78 $wantsRest = ( $flags & self
::NO_CORE
);
80 # Will hold the .txt parser test files we will include
83 # Filter out .txt files
84 foreach ( $wgParserTestFiles as $parserTestFile ) {
85 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
87 if ( $isCore && $wantsCore ) {
88 self
::debug( "included core parser tests: $parserTestFile" );
89 $filesToTest[] = $parserTestFile;
90 } elseif ( !$isCore && $wantsRest ) {
91 self
::debug( "included non core parser tests: $parserTestFile" );
92 $filesToTest[] = $parserTestFile;
94 self
::debug( "skipped parser tests: $parserTestFile" );
97 self
::debug( 'parser tests files: '
98 . implode( ' ', $filesToTest ) );
102 foreach ( $filesToTest as $fileName ) {
103 // Call the highest level directory the extension name.
104 // It may or may not actually be, but it should be close
105 // enough to cause there to be separate names for different
106 // things, which is good enough for our purposes.
107 $extensionName = basename( dirname( $fileName ) );
108 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
109 $parserTestClassName = ucfirst( $testsName );
111 // Official spec for class names: https://secure.php.net/manual/en/language.oop5.basic.php
112 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
113 $parserTestClassName = 'ParserTest_' .
114 preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
116 if ( isset( $testList[$parserTestClassName] ) ) {
117 // If there is a conflict, append a number.
119 $parserTestClassName .= $counter;
121 $testList[$parserTestClassName] = true;
123 // Previously we actually created a class here, with eval(). We now
124 // just override the name.
126 self
::debug( "Adding test class $parserTestClassName" );
127 $this->addTest( new ParserTestFileSuite(
128 $this->ptRunner
, $parserTestClassName, $fileName ) );
132 public function setUp() {
133 wfDebug( __METHOD__
);
134 $db = wfGetDB( DB_MASTER
);
135 $type = $db->getType();
136 $prefix = $type === 'oracle' ?
137 MediaWikiTestCase
::ORA_DB_PREFIX
: MediaWikiTestCase
::DB_PREFIX
;
138 MediaWikiTestCase
::setupTestDB( $db, $prefix );
139 $teardown = $this->ptRunner
->setDatabase( $db );
140 $teardown = $this->ptRunner
->setupUploads( $teardown );
141 $this->ptTeardownScope
= $teardown;
144 public function tearDown() {
145 wfDebug( __METHOD__
);
146 if ( $this->ptTeardownScope
) {
147 ScopedCallback
::consume( $this->ptTeardownScope
);
152 * Write $msg under log group 'tests-parser'
153 * @param string $msg Message to log
155 protected static function debug( $msg ) {
156 return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );