1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests; |
4
|
|
|
|
5
|
|
|
use SMW\ApplicationFactory; |
6
|
|
|
use SMW\DataValueFactory; |
7
|
|
|
use SMW\DeferredCallableUpdate; |
8
|
|
|
use SMW\Store; |
9
|
|
|
use SMW\Localizer; |
10
|
|
|
use SMW\Tests\Utils\UtilityFactory; |
11
|
|
|
use SMW\Tests\Utils\Mock\ConfigurableStub; |
12
|
|
|
use RuntimeException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @license GNU GPL v2+ |
16
|
|
|
* @since 2.4 |
17
|
|
|
* |
18
|
|
|
* @author mwjames |
19
|
|
|
*/ |
20
|
|
|
class TestEnvironment { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ApplicationFactory |
24
|
|
|
*/ |
25
|
|
|
private $applicationFactory = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var DataValueFactory |
29
|
|
|
*/ |
30
|
|
|
private $dataValueFactory = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $configuration = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @since 2.4 |
39
|
|
|
* |
40
|
|
|
* @param array $configuration |
41
|
|
|
*/ |
42
|
|
|
public function __construct( array $configuration = array() ) { |
43
|
|
|
$this->applicationFactory = ApplicationFactory::getInstance(); |
44
|
|
|
$this->dataValueFactory = DataValueFactory::getInstance(); |
45
|
|
|
|
46
|
|
|
$this->withConfiguration( $configuration ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @since 2.4 |
51
|
|
|
*/ |
52
|
|
|
public static function executePendingDeferredUpdates() { |
53
|
|
|
DeferredCallableUpdate::releasePendingUpdates(); |
54
|
|
|
\DeferredUpdates::doUpdates(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @since 2.4 |
59
|
|
|
*/ |
60
|
|
|
public static function clearPendingDeferredUpdates() { |
61
|
|
|
DeferredCallableUpdate::releasePendingUpdates(); |
62
|
|
|
\DeferredUpdates::clearPendingUpdates(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @since 2.4 |
67
|
|
|
* |
68
|
|
|
* @param string $key |
69
|
|
|
* @param mixed $value |
70
|
|
|
* |
71
|
|
|
* @return self |
72
|
|
|
*/ |
73
|
|
|
public function addConfiguration( $key, $value ) { |
74
|
|
|
return $this->withConfiguration( array( $key => $value ) ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @since 2.4 |
79
|
|
|
* |
80
|
|
|
* @param array $configuration |
81
|
|
|
* |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
|
|
public function withConfiguration( array $configuration = array() ) { |
|
|
|
|
85
|
|
|
|
86
|
|
|
foreach ( $configuration as $key => $value ) { |
87
|
|
|
$this->configuration[$key] = $GLOBALS[$key]; |
88
|
|
|
$GLOBALS[$key] = $value; |
89
|
|
|
$this->applicationFactory->getSettings()->set( $key, $value ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @since 2.4 |
97
|
|
|
* |
98
|
|
|
* @param string $name |
99
|
|
|
*/ |
100
|
|
|
public function resetMediaWikiService( $name ) { |
101
|
|
|
|
102
|
|
|
// MW 1.27+ (yet 1.27.0.rc has no access to "resetServiceForTesting") |
103
|
|
|
if ( !class_exists( '\MediaWiki\MediaWikiServices' ) || !method_exists( \MediaWiki\MediaWikiServices::getInstance(), 'resetServiceForTesting' ) ) { |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
try { |
108
|
|
|
\MediaWiki\MediaWikiServices::getInstance()->resetServiceForTesting( $name ); |
109
|
|
|
} catch( \Exception $e ) { |
110
|
|
|
// Do nothing just avoid a |
111
|
|
|
// MediaWiki\Services\NoSuchServiceException: No such service ... |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @since 2.4 |
119
|
|
|
* |
120
|
|
|
* @param string|array $poolCache |
121
|
|
|
* |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
|
|
public function resetPoolCacheFor( $poolCache ) { |
125
|
|
|
|
126
|
|
|
if ( is_array( $poolCache ) ) { |
127
|
|
|
foreach ( $poolCache as $pc ) { |
128
|
|
|
$this->resetPoolCacheFor( $pc ); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->applicationFactory->getInMemoryPoolCache()->resetPoolCacheFor( $poolCache ); |
|
|
|
|
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @since 2.4 |
139
|
|
|
* |
140
|
|
|
* @param string $id |
141
|
|
|
* @param mixed $object |
142
|
|
|
* |
143
|
|
|
* @return self |
144
|
|
|
*/ |
145
|
|
|
public function registerObject( $id, $object ) { |
146
|
|
|
$this->applicationFactory->registerObject( $id, $object ); |
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @since 2.4 |
152
|
|
|
*/ |
153
|
|
|
public function tearDown() { |
|
|
|
|
154
|
|
|
|
155
|
|
|
foreach ( $this->configuration as $key => $value ) { |
156
|
|
|
$GLOBALS[$key] = $value; |
157
|
|
|
$this->applicationFactory->getSettings()->set( $key, $value ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->applicationFactory->clear(); |
161
|
|
|
$this->dataValueFactory->clear(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @since 2.5 |
166
|
|
|
* |
167
|
|
|
* @param $originalClassName |
168
|
|
|
* @param array $configuration |
169
|
|
|
* |
170
|
|
|
* @return PHPUnit_Framework_MockObject_MockObject |
171
|
|
|
*/ |
172
|
|
|
public function createConfiguredStub( $originalClassName, array $configuration ) { |
173
|
|
|
$configurableStub = new ConfigurableStub(); |
174
|
|
|
return $configurableStub->createConfiguredStub( $originalClassName, $configuration ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @since 2.5 |
179
|
|
|
* |
180
|
|
|
* @param $originalClassName |
181
|
|
|
* @param array $configuration |
182
|
|
|
* |
183
|
|
|
* @return PHPUnit_Framework_MockObject_MockObject |
184
|
|
|
*/ |
185
|
|
|
public function createConfiguredAbstractStub( $originalClassName, array $configuration ) { |
186
|
|
|
$configurableStub = new ConfigurableStub(); |
187
|
|
|
return $configurableStub->createConfiguredAbstractStub( $originalClassName, $configuration ); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @since 2.5 |
192
|
|
|
* |
193
|
|
|
* @param array $pages |
194
|
|
|
*/ |
195
|
|
|
public function flushPages( $pages ) { |
196
|
|
|
$this->getUtilityFactory()->newPageDeleter()->doDeletePoolOfPages( $pages ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @since 2.4 |
201
|
|
|
* |
202
|
|
|
* @return UtilityFactory |
203
|
|
|
*/ |
204
|
|
|
public function getUtilityFactory() { |
205
|
|
|
return UtilityFactory::getInstance(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @since 2.5 |
210
|
|
|
* |
211
|
|
|
* @param integer $ns |
212
|
|
|
* @param string $url |
|
|
|
|
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
public function getLocalizedTextByNamespace( $ns, $text ) { |
217
|
|
|
|
218
|
|
|
$namespace = Localizer::getInstance()->getNamespaceTextById( $ns ); |
219
|
|
|
|
220
|
|
|
return str_replace( |
221
|
|
|
\MWNamespace::getCanonicalName( $ns ) . ':', |
222
|
|
|
$namespace . ':', |
223
|
|
|
$text |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @since 2.5 |
229
|
|
|
* |
230
|
|
|
* @param string $target |
231
|
|
|
* @param string $file |
232
|
|
|
* |
233
|
|
|
* @return string |
234
|
|
|
* @throws RuntimeException |
235
|
|
|
*/ |
236
|
|
|
public function getFixturesLocation( $target = '', $file = '' ) { |
237
|
|
|
|
238
|
|
|
$fixturesLocation = __DIR__ . '/Fixtures' . ( $target !== '' ? "/{$target}" : '' ) . ( $file !== '' ? '/' . $file : '' ); |
239
|
|
|
$fixturesLocation = str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $fixturesLocation ); |
240
|
|
|
|
241
|
|
|
if ( !file_exists( $fixturesLocation ) && !is_dir( $fixturesLocation ) ) { |
242
|
|
|
throw new RuntimeException( "{$fixturesLocation} does not exist." ); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $fixturesLocation; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: