1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests; |
4
|
|
|
|
5
|
|
|
use SMW\ApplicationFactory; |
6
|
|
|
use SMW\DataValueFactory; |
7
|
|
|
use SMW\Store; |
8
|
|
|
use SMW\Tests\Utils\UtilityFactory; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 2.4 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class TestEnvironment { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ApplicationFactory |
20
|
|
|
*/ |
21
|
|
|
private $applicationFactory = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var DataValueFactory |
25
|
|
|
*/ |
26
|
|
|
private $dataValueFactory = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $configuration = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @since 2.4 |
35
|
|
|
* |
36
|
|
|
* @param array $configuration |
37
|
|
|
*/ |
38
|
|
|
public function __construct( array $configuration = array() ) { |
39
|
|
|
$this->applicationFactory = ApplicationFactory::getInstance(); |
40
|
|
|
$this->dataValueFactory = DataValueFactory::getInstance(); |
41
|
|
|
|
42
|
|
|
$this->withConfiguration( $configuration ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @since 2.4 |
47
|
|
|
*/ |
48
|
|
|
public static function executePendingDeferredUpdates() { |
49
|
|
|
\DeferredUpdates::doUpdates(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @since 2.4 |
54
|
|
|
*/ |
55
|
|
|
public static function clearPendingDeferredUpdates() { |
56
|
|
|
\DeferredUpdates::clearPendingUpdates(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @since 2.4 |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
* @param mixed $value |
64
|
|
|
* |
65
|
|
|
* @return self |
66
|
|
|
*/ |
67
|
|
|
public function addConfiguration( $key, $value ) { |
68
|
|
|
return $this->withConfiguration( array( $key => $value ) ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @since 2.4 |
73
|
|
|
* |
74
|
|
|
* @param array $configuration |
75
|
|
|
* |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
|
|
public function withConfiguration( array $configuration = array() ) { |
|
|
|
|
79
|
|
|
|
80
|
|
|
foreach ( $configuration as $key => $value ) { |
81
|
|
|
$this->configuration[$key] = $GLOBALS[$key]; |
82
|
|
|
$GLOBALS[$key] = $value; |
83
|
|
|
$this->applicationFactory->getSettings()->set( $key, $value ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @since 2.4 |
91
|
|
|
* |
92
|
|
|
* @param string $name |
93
|
|
|
*/ |
94
|
|
|
public function resetMediaWikiService( $name ) { |
95
|
|
|
|
96
|
|
|
// MW 1.27+ |
97
|
|
|
if ( !class_exists( '\MediaWiki\MediaWikiServices' ) ) { |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
\MediaWiki\MediaWikiServices::getInstance()->resetServiceForTesting( $name ); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @since 2.4 |
108
|
|
|
* |
109
|
|
|
* @param string $poolCache |
110
|
|
|
* |
111
|
|
|
* @return self |
112
|
|
|
*/ |
113
|
|
|
public function resetPoolCacheFor( $poolCache ) { |
114
|
|
|
$this->applicationFactory->getInMemoryPoolCache()->resetPoolCacheFor( $poolCache ); |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @since 2.4 |
120
|
|
|
* |
121
|
|
|
* @param string $id |
122
|
|
|
* @param mixed $object |
123
|
|
|
* |
124
|
|
|
* @return self |
125
|
|
|
*/ |
126
|
|
|
public function registerObject( $id, $object ) { |
127
|
|
|
$this->applicationFactory->registerObject( $id, $object ); |
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @since 2.4 |
133
|
|
|
*/ |
134
|
|
|
public function tearDown() { |
|
|
|
|
135
|
|
|
|
136
|
|
|
foreach ( $this->configuration as $key => $value ) { |
137
|
|
|
$GLOBALS[$key] = $value; |
138
|
|
|
$this->applicationFactory->getSettings()->set( $key, $value ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->applicationFactory->clear(); |
142
|
|
|
$this->dataValueFactory->clear(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @since 2.4 |
147
|
|
|
* |
148
|
|
|
* @return UtilityFactory |
149
|
|
|
*/ |
150
|
|
|
public function getUtilityFactory() { |
151
|
|
|
return UtilityFactory::getInstance(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
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: