MediaWiki  master
ParserTestTopLevelSuite.php
Go to the documentation of this file.
1 <?php
2 
5 
15 class ParserTestTopLevelSuite extends PHPUnit_Framework_TestSuite {
17  private $ptRunner;
18 
21 
22  private $oldTablePrefix = '';
23 
32  const CORE_ONLY = 1;
34  const NO_CORE = 2;
36  const WITH_ALL = self::CORE_ONLY | self::NO_CORE;
37 
63  public static function suite( $flags = self::CORE_ONLY ) {
64  return new self( $flags );
65  }
66 
67  function __construct( $flags ) {
68  parent::__construct();
69 
70  $this->ptRecorder = new PhpunitTestRecorder;
71  $this->ptRunner = new ParserTestRunner( $this->ptRecorder );
72 
73  if ( is_string( $flags ) ) {
74  $flags = self::CORE_ONLY;
75  }
76  global $IP;
77 
78  $mwTestDir = $IP . '/tests/';
79 
80  # Human friendly helpers
81  $wantsCore = ( $flags & self::CORE_ONLY );
82  $wantsRest = ( $flags & self::NO_CORE );
83 
84  # Will hold the .txt parser test files we will include
85  $filesToTest = [];
86 
87  # Filter out .txt files
89  foreach ( $files as $extName => $parserTestFile ) {
90  $isCore = ( strpos( $parserTestFile, $mwTestDir ) === 0 );
91 
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;
98  } else {
99  self::debug( "skipped parser tests: $parserTestFile" );
100  }
101  }
102  self::debug( 'parser tests files: '
103  . implode( ' ', $filesToTest ) );
104 
105  $testList = [];
106  $counter = 0;
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 ) );
113  }
114  $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
115  $parserTestClassName = ucfirst( $testsName );
116 
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 );
121 
122  if ( isset( $testList[$parserTestClassName] ) ) {
123  // If there is a conflict, append a number.
124  $counter++;
125  $parserTestClassName .= $counter;
126  }
127  $testList[$parserTestClassName] = true;
128 
129  // Previously we actually created a class here, with eval(). We now
130  // just override the name.
131 
132  self::debug( "Adding test class $parserTestClassName" );
133  $this->addTest( new ParserTestFileSuite(
134  $this->ptRunner, $parserTestClassName, $fileName ) );
135  }
136  }
137 
138  public function setUp() {
139  wfDebug( __METHOD__ );
140 
141  $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
142  $db = $lb->getConnection( DB_MASTER );
143  $type = $db->getType();
144  $prefix = MediaWikiTestCase::DB_PREFIX;
145  $this->oldTablePrefix = $db->tablePrefix();
146  MediaWikiTestCase::setupTestDB( $db, $prefix );
147  CloneDatabase::changePrefix( $prefix );
148 
149  $this->ptRunner->setDatabase( $db );
150 
151  MediaWikiTestCase::resetNonServiceCaches();
152 
153  MediaWikiTestCase::installMockMwServices();
154  $teardown = new ScopedCallback( function () {
155  MediaWikiTestCase::restoreMwServices();
156  } );
157 
158  $teardown = $this->ptRunner->setupUploads( $teardown );
159  $this->ptTeardownScope = $teardown;
160  }
161 
162  public function tearDown() {
163  wfDebug( __METHOD__ );
164  if ( $this->ptTeardownScope ) {
165  ScopedCallback::consume( $this->ptTeardownScope );
166  }
167  CloneDatabase::changePrefix( $this->oldTablePrefix );
168  }
169 
174  protected static function debug( $msg ) {
175  wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );
176  }
177 }
$IP
Definition: WebStart.php:41
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
static debug( $msg)
Write $msg under log group &#39;tests-parser&#39;.
const DB_MASTER
Definition: defines.php:26
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
const CORE_ONLY
Include files shipped with MediaWiki core.
static suite( $flags=self::CORE_ONLY)
Get a PHPUnit test suite of parser tests.
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
const WITH_ALL
Include anything set via $wgParserTestFiles.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
const NO_CORE
Include non core files as set in $wgParserTestFiles.
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.
static changePrefix( $prefix)
Change the table prefix on all open DB connections.
The UnitTest must be either a class that inherits from MediaWikiTestCase or a class that provides a p...