forked from SemanticMediaWiki/SemanticMediaWiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsTest.php
207 lines (153 loc) · 4.33 KB
/
SettingsTest.php
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
<?php
namespace SMW\Tests;
use SMW\Settings;
use SMW\Tests\TestEnvironment;
use SMW\Tests\PHPUnitCompat;
/**
* @covers \SMW\Settings
* @group semantic-mediawiki
*
* @license GNU GPL v2+
* @since 1.9
*
* @author mwjames
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class SettingsTest extends \PHPUnit_Framework_TestCase {
use PHPUnitCompat;
private $hookDispatcher;
protected function setUp() : void {
parent::setUp();
$this->hookDispatcher = $this->getMockBuilder( '\SMW\MediaWiki\HookDispatcher' )
->disableOriginalConstructor()
->getMock();
}
protected function tearDown() : void {
parent::tearDown();
}
/**
* @dataProvider settingsProvider
*/
public function testCanConstruct( array $settings ) {
$instance = Settings::newFromArray( $settings );
$this->assertInstanceOf(
Settings::class,
$instance
);
$this->assertFalse(
$instance === Settings::newFromArray( $settings )
);
}
/**
* @dataProvider settingsProvider
*/
public function testGet( array $settings ) {
$instance = Settings::newFromArray( $settings );
foreach ( $settings as $name => $value ) {
$this->assertEquals( $value, $instance->get( $name ) );
}
$this->assertTrue( true );
}
public function testUnknownSettingThrowsException() {
$instance = Settings::newFromArray( [ 'Foo' => 'bar' ] );
$this->expectException( '\SMW\Exception\SettingNotFoundException' );
$instance->get( 'foo' );
}
public function testSafeGetOnUnknownSetting() {
$instance = Settings::newFromArray( [ 'Foo' => 'bar' ] );
$this->assertFalse(
$instance->safeGet( 'foo', false )
);
}
public function testRegisterChangeListener() {
$changeListener = $this->getMockBuilder( '\SMW\Listener\ChangeListener\ChangeListener' )
->disableOriginalConstructor()
->getMock();
$changeListener->expects( $this->once() )
->method( 'canTrigger' )
->with( $this->equalTo( 'Foo' ) )
->will( $this->returnValue( true ) );
$changeListener->expects( $this->once() )
->method( 'setAttrs' )
->with( $this->equalTo( [ 'Foo' => 'Bar' ] ) );
$changeListener->expects( $this->once() )
->method( 'trigger' )
->with( $this->equalTo( 'Foo' ) );
$instance = Settings::newFromArray( [] );
$instance->registerChangeListener( $changeListener );
$instance->set( 'Foo', 'Bar' );
$instance->clearChangeListeners();
}
/**
* @dataProvider settingsProvider
*/
public function testSet( array $settings ) {
$instance = Settings::newFromArray( [] );
foreach ( $settings as $name => $value ) {
$instance->set( $name, $value );
$this->assertEquals( $value, $instance->get( $name ) );
}
$this->assertTrue( true );
}
/**
* @dataProvider globalsSettingsProvider
*/
public function testNewFromGlobals( $setting ) {
$instance = new Settings();
$instance->setHookDispatcher(
$this->hookDispatcher
);
$instance->loadFromGlobals();
$this->assertTrue(
$instance->has( $setting ),
"Failed asserting that `{$setting}` exists. It could be that `{$setting}`\n" .
"is invoked by the `LocalSettings.php` or some parameter is using the `smwg` prefix.\n"
);
}
public function testReloadAttemptThrowsException() {
$instance = new Settings();
$instance->setHookDispatcher(
$this->hookDispatcher
);
$instance->loadFromGlobals();
$this->expectException( '\SMW\Exception\SettingsAlreadyLoadedException' );
$instance->loadFromGlobals();
}
public function testMung() {
$instance = Settings::newFromArray( [ 'Foo' => 123 ] );
$this->assertEquals(
'123bar',
$instance->mung( 'Foo', 'bar' )
);
}
public function testMungOnUnknownTypeThrowsException() {
$instance = Settings::newFromArray( [ 'Foo' => 123 ] );
$this->expectException( '\RuntimeException' );
$instance->mung( 'Foo', 456 );
}
/**
* Provides sample data to be tested
*
* @return array
*/
public function settingsProvider() {
return [ [ [
'baz' => 'BAH',
'bar' => [ '9001' ],
'foo' => [ '9001', [ 9001, 4.2 ] ],
'~[,,_,,]:3' => [ 9001, 4.2 ],
] ] ];
}
/**
* Provides and collects individual smwg* settings
*/
public function globalsSettingsProvider() {
$settings = array_intersect_key( $GLOBALS,
array_flip( preg_grep('/^smwg/', array_keys( $GLOBALS ) ) )
);
unset( $settings['smwgDeprecationNotices'] );
foreach ( $settings as $key => $value ) {
yield [ $key ];
}
}
}