Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / tests / phpunit / suites / ParserTestTopLevelSuite.php
blob09052f3c610476bad71edacf1329f71c24ead326
1 <?php
2 use Wikimedia\ScopedCallback;
4 /**
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
9 * @group Parser
10 * @group ParserTests
11 * @group Database
13 class ParserTestTopLevelSuite extends PHPUnit_Framework_TestSuite {
14 /** @var ParserTestRunner */
15 private $ptRunner;
17 /** @var ScopedCallback */
18 private $ptTeardownScope;
20 /**
21 * @defgroup filtering_constants Filtering constants
23 * Limit inclusion of parser tests files coming from MediaWiki core
24 * @{
27 /** Include files shipped with MediaWiki core */
28 const CORE_ONLY = 1;
29 /** Include non core files as set in $wgParserTestFiles */
30 const NO_CORE = 2;
31 /** Include anything set via $wgParserTestFiles */
32 const WITH_ALL = 3; # CORE_ONLY | NO_CORE
34 /** @} */
36 /**
37 * Get a PHPUnit test suite of parser tests. Optionally filtered with
38 * $flags.
40 * @par Examples:
41 * Get a suite of parser tests shipped by MediaWiki core:
42 * @code
43 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::CORE_ONLY );
44 * @endcode
45 * Get a suite of various parser tests, like extensions:
46 * @code
47 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::NO_CORE );
48 * @endcode
49 * Get any test defined via $wgParserTestFiles:
50 * @code
51 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::WITH_ALL );
52 * @endcode
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 $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
81 $filesToTest = [];
83 # Filter out .txt files
84 $files = ParserTestRunner::getParserTestFiles();
85 foreach ( $files as $parserTestFile ) {
86 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
88 if ( $isCore && $wantsCore ) {
89 self::debug( "included core parser tests: $parserTestFile" );
90 $filesToTest[] = $parserTestFile;
91 } elseif ( !$isCore && $wantsRest ) {
92 self::debug( "included non core parser tests: $parserTestFile" );
93 $filesToTest[] = $parserTestFile;
94 } else {
95 self::debug( "skipped parser tests: $parserTestFile" );
98 self::debug( 'parser tests files: '
99 . implode( ' ', $filesToTest ) );
101 $testList = [];
102 $counter = 0;
103 foreach ( $filesToTest as $fileName ) {
104 // Call the highest level directory the extension name.
105 // It may or may not actually be, but it should be close
106 // enough to cause there to be separate names for different
107 // things, which is good enough for our purposes.
108 $extensionName = basename( dirname( $fileName ) );
109 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
110 $parserTestClassName = ucfirst( $testsName );
112 // Official spec for class names: https://secure.php.net/manual/en/language.oop5.basic.php
113 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
114 $parserTestClassName = 'ParserTest_' .
115 preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
117 if ( isset( $testList[$parserTestClassName] ) ) {
118 // If there is a conflict, append a number.
119 $counter++;
120 $parserTestClassName .= $counter;
122 $testList[$parserTestClassName] = true;
124 // Previously we actually created a class here, with eval(). We now
125 // just override the name.
127 self::debug( "Adding test class $parserTestClassName" );
128 $this->addTest( new ParserTestFileSuite(
129 $this->ptRunner, $parserTestClassName, $fileName ) );
133 public function setUp() {
134 wfDebug( __METHOD__ );
135 $db = wfGetDB( DB_MASTER );
136 $type = $db->getType();
137 $prefix = $type === 'oracle' ?
138 MediaWikiTestCase::ORA_DB_PREFIX : MediaWikiTestCase::DB_PREFIX;
139 MediaWikiTestCase::setupTestDB( $db, $prefix );
140 $teardown = $this->ptRunner->setDatabase( $db );
141 $teardown = $this->ptRunner->setupUploads( $teardown );
142 $this->ptTeardownScope = $teardown;
145 public function tearDown() {
146 wfDebug( __METHOD__ );
147 if ( $this->ptTeardownScope ) {
148 ScopedCallback::consume( $this->ptTeardownScope );
153 * Write $msg under log group 'tests-parser'
154 * @param string $msg Message to log
156 protected static function debug( $msg ) {
157 return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );