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