77461c5af26bbd7a8ec7caa3b2ac9ed01c7bc3bf
[lhc/web/wiklou.git] / tests / phpunit / suites / ParserTestTopLevelSuite.php
1 <?php
2
3 use Wikimedia\ScopedCallback;
4
5 /**
6 * The UnitTest must be either a class that inherits from MediaWikiTestCase
7 * or a class that provides a public static suite() method which returns
8 * an PHPUnit_Framework_Test object
9 *
10 * @group Parser
11 * @group ParserTests
12 * @group Database
13 */
14 class ParserTestTopLevelSuite extends PHPUnit_Framework_TestSuite {
15 /** @var ParserTestRunner */
16 private $ptRunner;
17
18 /** @var ScopedCallback */
19 private $ptTeardownScope;
20
21 private $oldTablePrefix = '';
22
23 /**
24 * @defgroup filtering_constants Filtering constants
25 *
26 * Limit inclusion of parser tests files coming from MediaWiki core
27 * @{
28 */
29
30 /** Include files shipped with MediaWiki core */
31 const CORE_ONLY = 1;
32 /** Include non core files as set in $wgParserTestFiles */
33 const NO_CORE = 2;
34 /** Include anything set via $wgParserTestFiles */
35 const WITH_ALL = self::CORE_ONLY | self::NO_CORE;
36
37 /** @} */
38
39 /**
40 * Get a PHPUnit test suite of parser tests. Optionally filtered with
41 * $flags.
42 *
43 * @par Examples:
44 * Get a suite of parser tests shipped by MediaWiki core:
45 * @code
46 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::CORE_ONLY );
47 * @endcode
48 * Get a suite of various parser tests, like extensions:
49 * @code
50 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::NO_CORE );
51 * @endcode
52 * Get any test defined via $wgParserTestFiles:
53 * @code
54 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::WITH_ALL );
55 * @endcode
56 *
57 * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
58 * will be included. Default: ParserTestTopLevelSuite::CORE_ONLY
59 *
60 * @return PHPUnit_Framework_TestSuite
61 */
62 public static function suite( $flags = self::CORE_ONLY ) {
63 return new self( $flags );
64 }
65
66 function __construct( $flags ) {
67 parent::__construct();
68
69 $this->ptRecorder = new PhpunitTestRecorder;
70 $this->ptRunner = new ParserTestRunner( $this->ptRecorder );
71
72 if ( is_string( $flags ) ) {
73 $flags = self::CORE_ONLY;
74 }
75 global $IP;
76
77 $mwTestDir = $IP . '/tests/';
78
79 # Human friendly helpers
80 $wantsCore = ( $flags & self::CORE_ONLY );
81 $wantsRest = ( $flags & self::NO_CORE );
82
83 # Will hold the .txt parser test files we will include
84 $filesToTest = [];
85
86 # Filter out .txt files
87 $files = ParserTestRunner::getParserTestFiles();
88 foreach ( $files as $extName => $parserTestFile ) {
89 $isCore = ( strpos( $parserTestFile, $mwTestDir ) === 0 );
90
91 if ( $isCore && $wantsCore ) {
92 self::debug( "included core parser tests: $parserTestFile" );
93 $filesToTest[$extName] = $parserTestFile;
94 } elseif ( !$isCore && $wantsRest ) {
95 self::debug( "included non core parser tests: $parserTestFile" );
96 $filesToTest[$extName] = $parserTestFile;
97 } else {
98 self::debug( "skipped parser tests: $parserTestFile" );
99 }
100 }
101 self::debug( 'parser tests files: '
102 . implode( ' ', $filesToTest ) );
103
104 $testList = [];
105 $counter = 0;
106 foreach ( $filesToTest as $extensionName => $fileName ) {
107 if ( is_int( $extensionName ) ) {
108 // If there's no extension name because this is coming
109 // from the legacy global, then assume the next level directory
110 // is the extension name (e.g. extensions/FooBar/parserTests.txt).
111 $extensionName = basename( dirname( $fileName ) );
112 }
113 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
114 $parserTestClassName = ucfirst( $testsName );
115
116 // Official spec for class names: https://secure.php.net/manual/en/language.oop5.basic.php
117 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
118 $parserTestClassName = 'ParserTest_' .
119 preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
120
121 if ( isset( $testList[$parserTestClassName] ) ) {
122 // If there is a conflict, append a number.
123 $counter++;
124 $parserTestClassName .= $counter;
125 }
126 $testList[$parserTestClassName] = true;
127
128 // Previously we actually created a class here, with eval(). We now
129 // just override the name.
130
131 self::debug( "Adding test class $parserTestClassName" );
132 $this->addTest( new ParserTestFileSuite(
133 $this->ptRunner, $parserTestClassName, $fileName ) );
134 }
135 }
136
137 public function setUp() {
138 wfDebug( __METHOD__ );
139
140 $db = wfGetDB( DB_MASTER );
141 $type = $db->getType();
142 $prefix = $type === 'oracle' ?
143 MediaWikiTestCase::ORA_DB_PREFIX : MediaWikiTestCase::DB_PREFIX;
144 $this->oldTablePrefix = $db->tablePrefix();
145 MediaWikiTestCase::setupTestDB( $db, $prefix );
146 CloneDatabase::changePrefix( $prefix );
147
148 $this->ptRunner->setDatabase( $db );
149
150 MediaWikiTestCase::resetNonServiceCaches();
151
152 MediaWikiTestCase::installMockMwServices();
153 $teardown = new ScopedCallback( function () {
154 MediaWikiTestCase::restoreMwServices();
155 } );
156
157 $teardown = $this->ptRunner->setupUploads( $teardown );
158 $this->ptTeardownScope = $teardown;
159 }
160
161 public function tearDown() {
162 wfDebug( __METHOD__ );
163 if ( $this->ptTeardownScope ) {
164 ScopedCallback::consume( $this->ptTeardownScope );
165 }
166 CloneDatabase::changePrefix( $this->oldTablePrefix );
167 }
168
169 /**
170 * Write $msg under log group 'tests-parser'
171 * @param string $msg Message to log
172 */
173 protected static function debug( $msg ) {
174 wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );
175 }
176 }