MediaWiki REL1_31
ParserTestTopLevelSuite.php
Go to the documentation of this file.
1<?php
2use Wikimedia\ScopedCallback;
3
13class ParserTestTopLevelSuite extends PHPUnit_Framework_TestSuite {
15 private $ptRunner;
16
19
28 const CORE_ONLY = 1;
30 const NO_CORE = 2;
32 const WITH_ALL = 3; # CORE_ONLY | NO_CORE
33
59 public static function suite( $flags = self::CORE_ONLY ) {
60 return new self( $flags );
61 }
62
63 function __construct( $flags ) {
64 parent::__construct();
65
66 $this->ptRecorder = new PhpunitTestRecorder;
67 $this->ptRunner = new ParserTestRunner( $this->ptRecorder );
68
69 if ( is_string( $flags ) ) {
70 $flags = self::CORE_ONLY;
71 }
72 global $IP;
73
74 $mwTestDir = $IP . '/tests/';
75
76 # Human friendly helpers
77 $wantsCore = ( $flags & self::CORE_ONLY );
78 $wantsRest = ( $flags & self::NO_CORE );
79
80 # Will hold the .txt parser test files we will include
81 $filesToTest = [];
82
83 # Filter out .txt files
85 foreach ( $files as $extName => $parserTestFile ) {
86 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
87
88 if ( $isCore && $wantsCore ) {
89 self::debug( "included core parser tests: $parserTestFile" );
90 $filesToTest[$extName] = $parserTestFile;
91 } elseif ( !$isCore && $wantsRest ) {
92 self::debug( "included non core parser tests: $parserTestFile" );
93 $filesToTest[$extName] = $parserTestFile;
94 } else {
95 self::debug( "skipped parser tests: $parserTestFile" );
96 }
97 }
98 self::debug( 'parser tests files: '
99 . implode( ' ', $filesToTest ) );
100
101 $testList = [];
102 $counter = 0;
103 foreach ( $filesToTest as $extensionName => $fileName ) {
104 if ( is_int( $extensionName ) ) {
105 // If there's no extension name because this is coming
106 // from the legacy global, then assume the next level directory
107 // is the extension name (e.g. extensions/FooBar/parserTests.txt).
108 $extensionName = basename( dirname( $fileName ) );
109 }
110 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
111 $parserTestClassName = ucfirst( $testsName );
112
113 // Official spec for class names: https://secure.php.net/manual/en/language.oop5.basic.php
114 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
115 $parserTestClassName = 'ParserTest_' .
116 preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
117
118 if ( isset( $testList[$parserTestClassName] ) ) {
119 // If there is a conflict, append a number.
120 $counter++;
121 $parserTestClassName .= $counter;
122 }
123 $testList[$parserTestClassName] = true;
124
125 // Previously we actually created a class here, with eval(). We now
126 // just override the name.
127
128 self::debug( "Adding test class $parserTestClassName" );
129 $this->addTest( new ParserTestFileSuite(
130 $this->ptRunner, $parserTestClassName, $fileName ) );
131 }
132 }
133
134 public function setUp() {
135 wfDebug( __METHOD__ );
136 $db = wfGetDB( DB_MASTER );
137 $type = $db->getType();
138 $prefix = $type === 'oracle' ?
139 MediaWikiTestCase::ORA_DB_PREFIX : MediaWikiTestCase::DB_PREFIX;
140 MediaWikiTestCase::setupTestDB( $db, $prefix );
141 $teardown = $this->ptRunner->setDatabase( $db );
142 $teardown = $this->ptRunner->setupUploads( $teardown );
143 $this->ptTeardownScope = $teardown;
144 }
145
146 public function tearDown() {
147 wfDebug( __METHOD__ );
148 if ( $this->ptTeardownScope ) {
149 ScopedCallback::consume( $this->ptTeardownScope );
150 }
151 }
152
157 protected static function debug( $msg ) {
158 wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );
159 }
160}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
This is the suite class for running tests within a single .txt source file.
static getParserTestFiles()
Get list of filenames to extension and core parser tests.
The UnitTest must be either a class that inherits from MediaWikiTestCase or a class that provides a p...
static debug( $msg)
Write $msg under log group 'tests-parser'.
static suite( $flags=self::CORE_ONLY)
Get a PHPUnit test suite of parser tests.
$IP
Definition update.php:3
const NO_CORE
Include non core files as set in $wgParserTestFiles.
const WITH_ALL
Include anything set via $wgParserTestFiles.
const CORE_ONLY
Include files shipped with MediaWiki core.
const DB_MASTER
Definition defines.php:29