-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBootstrapComponentsJSONScriptTestCaseRunnerTest.php
More file actions
222 lines (189 loc) · 6.35 KB
/
Copy pathBootstrapComponentsJSONScriptTestCaseRunnerTest.php
File metadata and controls
222 lines (189 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace MediaWiki\Extension\BootstrapComponents\Tests\Integration;
use MediaWiki\Extension\BootstrapComponents\ApplicationFactory;
use MediaWiki\Extension\BootstrapComponents\Hooks\OutputPageParserOutput;
use MediaWiki\Extension\BootstrapComponents\HookRegistry;
use SMW\DIWikiPage;
use SMW\Tests\JSONScriptTestCaseRunner;
use SMW\Tests\Utils\JSONScript\JsonTestCaseFileHandler;
use SMW\Tests\Utils\Validators\StringValidator;
/**
* @see https://github.com/SemanticMediaWiki/SemanticMediaWiki/tree/master/tests#write-integration-tests-using-json-script
*
* `JSONScriptTestCaseRunner` provisioned by SMW is a base class allowing to use a JSON
* format to create test definitions with the objective to compose "real" content
* and test integration with MediaWiki, Semantic MediaWiki, and Scribunto.
*
* The focus is on describing test definitions with its content and specify assertions
* to control the expected baseline.
*
* `JSONScriptTestCaseRunner` will handle the tearDown process and ensures that no test
* data are leaked into a production system but requires an active DB connection.
*
* @group extension-bootstrap-components
* @group medium
*
* @license GNU GPL v3+
* @since 1.0
*
* @author mwjames
* @author Tobias Oetterer
*/
class BootstrapComponentsJSONScriptTestCaseRunnerTest extends JSONScriptTestCaseRunner {
/**
* @var StringValidator
*/
private $stringValidator;
/**
* @throws \ConfigException
* @throws \MWException
*/
protected function setUp(): void {
wfDebugLog( 'BootstrapComponents', 'Running the JSONScriptTestCaseRunnerTest setup.' );
parent::setUp();
$validatorFactory = $this->testEnvironment->getUtilityFactory()->newValidatorFactory();
$this->stringValidator = $validatorFactory->newStringValidator();
// at this point, I normally would need to reset the complete lookup in the ApplicationFactory.
// Unfortunately, this causes problems with the re-registering of the parser function hooks and
// passing them the correct new NestingController
// here's to hoping, this is only botched in testing environment.
ApplicationFactory::getInstance()->resetLookup( 'ParserOutputHelper' );
#@fixme this is foobar to make modals work in integration tests. find a better solution
# see also \BootstrapComponents\AbstractComponent::getParserOutputHelper
if ( !defined( 'BSC_INTEGRATION_TEST' ) ) {
define( 'BSC_INTEGRATION_TEST', true );
}
// hook registry was removed in 5.2.0
#$hookRegistry = new HookRegistry();
#$hookRegistry->clear();
#$hookCallbackList = $hookRegistry->buildHookCallbackListFor(
# HookRegistry::AVAILABLE_HOOKS
#);
#$hookRegistry->register( $hookCallbackList );
}
/**
* @see JSONScriptTestCaseRunner::getRequiredJsonTestCaseMinVersion
* @return string
*/
protected function getRequiredJsonTestCaseMinVersion() {
return '1';
}
/**
* @see JSONScriptTestCaseRunner::getTestCaseLocation
* @return string
*/
protected function getTestCaseLocation() {
return __DIR__ . '/TestCases';
}
/**
* Returns a list of files, an empty list is a sign to run all registered
* tests.
*
* @see JSONScriptTestCaseRunner::getListOfAllowedTestCaseFiles
*/
protected function getAllowedTestCaseFiles() {
return array();
}
/**
* @see JSONScriptTestCaseRunner::runTestCaseFile
*
* @param JsonTestCaseFileHandler $jsonTestCaseFileHandler
*/
protected function runTestCaseFile( JsonTestCaseFileHandler $jsonTestCaseFileHandler ) {
$this->checkEnvironmentToSkipCurrentTest( $jsonTestCaseFileHandler );
// Setup
$this->prepareTest( $jsonTestCaseFileHandler );
// Run test cases
$this->doRunParserTests( $jsonTestCaseFileHandler );
}
/**
* @param JsonTestCaseFileHandler $jsonTestCaseFileHandler
*/
private function doRunParserTests( JsonTestCaseFileHandler $jsonTestCaseFileHandler ) {
foreach ( $jsonTestCaseFileHandler->findTestCasesByType( 'parser' ) as $case ) {
if ( !isset( $case['subject'] ) ) {
break;
}
// Assert function are defined individually by each TestCaseRunner
// to ensure a wide range of scenarios can be supported.
$this->assertParserOutputForCase( $case );
}
}
/**
* Prepares the test case: setting of global configuration changes (json section "settings",
* creation of defined pages (json section "setup")
*
* @param JsonTestCaseFileHandler $jsonTestCaseFileHandler
*/
private function prepareTest( JsonTestCaseFileHandler $jsonTestCaseFileHandler ) {
// Defines settings that can be altered during a test run with each test
// having the possibility to change those values, settings will be reset to
// the original value (from before the test) after the test has finished.
$permittedSettings = array(
'wgLanguageCode',
'wgContLang',
'wgLang'
);
foreach ( $permittedSettings as $key ) {
$this->changeGlobalSettingTo(
$key,
$jsonTestCaseFileHandler->getSettingsFor( $key )
);
}
$this->createPagesFrom(
$jsonTestCaseFileHandler->getPageCreationSetupList(),
NS_MAIN
);
}
/**
* Assert the text content if available from the parse process and
* accessible using the ParserOutput object.
*
* ```
* "assert-output": {
* "to-contain": [
* "Foo"
* ],
* "not-contain": [
* "Bar"
* ]
* }
* ```
* @param array $case
*/
private function assertParserOutputForCase( array $case ) {
if ( !isset( $case['assert-output'] ) ) {
return;
}
$subject = DIWikiPage::newFromText(
$case['subject'],
isset( $case['namespace'] ) ? constant( $case['namespace'] ) : NS_MAIN
);
/** @var \ParserOutput $parserOutput */
$parserOutput = $this->testEnvironment->getUtilityFactory()->newPageReader()->getEditInfo( $subject->getTitle() )->output;
try {
$outputPage = $this->getMockBuilder( 'OutputPage' )
->disableOriginalConstructor()
->getMock();
/** @noinspection PhpParamsInspection */
$hook = new OutputPageParserOutput( $outputPage, $parserOutput );
$hook->process();
} catch ( \Exception $e ) {
// nothing
}
if ( isset( $case['assert-output']['to-contain'] ) ) {
$this->stringValidator->assertThatStringContains(
$case['assert-output']['to-contain'],
$parserOutput->getText(),
$case['about']
);
}
if ( isset( $case['assert-output']['not-contain'] ) ) {
$this->stringValidator->assertThatStringNotContains(
$case['assert-output']['not-contain'],
$parserOutput->getText(),
$case['about']
);
}
}
}