Completed
Push — master ( 6aee84...9d465f )
by mw
127:50 queued 92:51
created

ExtraneousLanguageTest::testGetNamespaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 22
rs 9.2
c 1
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\ExtraneousLanguage;
4
5
use SMW\ExtraneousLanguage\ExtraneousLanguage;
6
use SMW\ExtraneousLanguage\LanguageFileContentsReader;
7
use SMW\ExtraneousLanguage\LanguageContents;
8
use SMW\ExtraneousLanguage\LanguageFallbackFinder;
9
10
/**
11
 * @covers \SMW\ExtraneousLanguage\ExtraneousLanguage
12
 * @group semantic-mediawiki
13
 *
14
 * @license GNU GPL v2+
15
 * @since 2.4
16
 *
17
 * @author mwjames
18
 */
19
class ExtraneousLanguageTest extends \PHPUnit_Framework_TestCase {
20
21
	private $languageContents;
22
23
	public function setUp() {
24
		parent::setUp();
25
26
		$this->languageContents = $this->getMockBuilder( LanguageContents::class )
27
			->disableOriginalConstructor()
28
			->getMock();
29
	}
30
31
	public function tearDown() {
32
		ExtraneousLanguage::clear();
33
		parent::tearDown();
34
	}
35
36
	public function testCanConstruct() {
37
38
		$this->assertInstanceOf(
39
			ExtraneousLanguage::class,
40
			new ExtraneousLanguage( $this->languageContents )
41
		);
42
43
		$this->assertInstanceOf(
44
			ExtraneousLanguage::class,
45
			ExtraneousLanguage::getInstance()
46
		);
47
48
		ExtraneousLanguage::clear();
49
	}
50
51
	public function testGetNamespaces() {
52
53
		$contents = array(
54
			"SMW_NS_PROPERTY" => "Property"
55
		);
56
57
		$this->languageContents->expects( $this->atLeastOnce() )
58
			->method( 'getContentsByLanguageWithIndex' )
59
			->with(
60
				$this->anything(),
61
				$this->equalTo( 'namespaces' ) )
62
			->will( $this->returnValue( $contents ) );
63
64
		$instance = new ExtraneousLanguage(
65
			$this->languageContents
66
		);
67
68
		$this->assertEquals(
69
			array( SMW_NS_PROPERTY => "Property" ),
70
			$instance->getNamespaces()
71
		);
72
	}
73
74
	public function testGetNamespaceAliases() {
75
76
		$contents = array(
77
			"Property" => "SMW_NS_PROPERTY"
78
		);
79
80
		$this->languageContents->expects( $this->atLeastOnce() )
81
			->method( 'getContentsByLanguageWithIndex' )
82
			->with(
83
				$this->anything(),
84
				$this->equalTo( 'namespaceAliases' ) )
85
			->will( $this->returnValue( $contents ) );
86
87
		$instance = new ExtraneousLanguage(
88
			$this->languageContents
89
		);
90
91
		$this->assertEquals(
92
			array( "Property" => SMW_NS_PROPERTY ),
93
			$instance->getNamespaceAliases()
94
		);
95
	}
96
97
	public function testGetPreferredDateFormatByPrecisionOnMatchedPrecision() {
98
99
		$contents = array(
100
			"SMW_PREC_YMDT" => "d m Y"
101
		);
102
103
		$this->languageContents->expects( $this->atLeastOnce() )
104
			->method( 'getContentsByLanguageWithIndex' )
105
			->with(
106
				$this->anything(),
107
				$this->equalTo( 'dateFormatsByPrecision' ) )
108
			->will( $this->returnValue( $contents ) );
109
110
		$instance = new ExtraneousLanguage(
111
			$this->languageContents
112
		);
113
114
		$this->assertEquals(
115
			'd m Y',
116
			$instance->getPreferredDateFormatByPrecision( SMW_PREC_YMDT )
117
		);
118
	}
119
120
	public function testGetPreferredDateFormatOnNotMatchablePrecision() {
121
122
		$contents = array(
123
			"Foo" => "d m Y"
124
		);
125
126
		$this->languageContents->expects( $this->atLeastOnce() )
127
			->method( 'getContentsByLanguageWithIndex' )
128
			->with(
129
				$this->anything(),
130
				$this->equalTo( 'dateFormatsByPrecision' ) )
131
			->will( $this->returnValue( $contents ) );
132
133
		$instance = new ExtraneousLanguage(
134
			$this->languageContents
135
		);
136
137
		$this->assertEquals(
138
			'd F Y H:i:s',
139
			$instance->getPreferredDateFormatByPrecision( SMW_PREC_YMDT )
140
		);
141
	}
142
143
	public function testGetDatatypeLabels() {
144
145
		$contents = array(
146
			"Foo" => "Bar"
147
		);
148
149
		$this->languageContents->expects( $this->atLeastOnce() )
150
			->method( 'getContentsByLanguageWithIndex' )
151
			->with(
152
				$this->anything(),
153
				$this->equalTo( 'dataTypeLabels' ) )
154
			->will( $this->returnValue( $contents ) );
155
156
		$instance = new ExtraneousLanguage(
157
			$this->languageContents
158
		);
159
160
		$this->assertEquals(
161
			array( "Foo" => 'Bar' ),
162
			$instance->getDatatypeLabels()
163
		);
164
	}
165
166
	public function testFindDatatypeByLabel() {
167
168
		$contents = array(
169
			"Bar" => "_foo"
170
		);
171
172
		$this->languageContents->expects( $this->atLeastOnce() )
173
			->method( 'getContentsByLanguageWithIndex' )
174
			->will( $this->returnValue( $contents ) );
175
176
		$instance = new ExtraneousLanguage(
177
			$this->languageContents
178
		);
179
180
		$this->assertEquals(
181
			'_foo',
182
			$instance->findDatatypeByLabel( 'Bar' )
183
		);
184
	}
185
186
	public function testGetPropertyIdByLabel() {
187
188
		$this->languageContents->expects( $this->at( 0 ) )
189
			->method( 'getContentsByLanguageWithIndex' )
190
			->with(
191
				$this->anything(),
192
				$this->equalTo( 'propertyLabels' ) )
193
			->will( $this->returnValue( array( "_FOO" => "Foo" ) ) );
194
195
		$this->languageContents->expects( $this->at( 2 ) )
196
			->method( 'getContentsByLanguageWithIndex' )
197
			->will( $this->returnValue( array() ) );
198
199
		$instance = new ExtraneousLanguage(
200
			$this->languageContents
201
		);
202
203
		$this->assertEquals(
204
			'_FOO',
205
			$instance->getPropertyIdByLabel( 'Foo' )
206
		);
207
	}
208
209
	public function testGetDateFormats() {
210
211
		$contents = array(
212
			array( 'SMW_Y' ),
213
			array( 'SMW_MY', 'SMW_YM' )
214
		);
215
216
		$this->languageContents->expects( $this->atLeastOnce() )
217
			->method( 'getContentsByLanguageWithIndex' )
218
			->with(
219
				$this->anything(),
220
				$this->equalTo( 'dateFormats' ) )
221
			->will( $this->returnValue( $contents ) );
222
223
		$instance = new ExtraneousLanguage(
224
			$this->languageContents
225
		);
226
227
		$this->assertEquals(
228
			array( array( 9 ), array( 97, 76 ) ),
229
			$instance->getDateFormats()
230
		);
231
	}
232
233
	public function testFindMonthNumberByLabelWithCaseInsensitiveSearch() {
234
235
		$contents = array(
236
			array( 'January', 'Jan' ),
237
			array( 'February', 'Feb' ),
238
			array( 'March', 'Mar' )
239
		);
240
241
		$this->languageContents->expects( $this->atLeastOnce() )
242
			->method( 'getContentsByLanguageWithIndex' )
243
			->with(
244
				$this->anything(),
245
				$this->equalTo( 'months' ) )
246
			->will( $this->returnValue( $contents ) );
247
248
		$instance = new ExtraneousLanguage(
249
			$this->languageContents
250
		);
251
252
		$this->assertEquals(
253
			3,
254
			$instance->findMonthNumberByLabel( 'mar' )
255
		);
256
	}
257
258
	public function testGetMonthLabelByNumber() {
259
260
		$contents = array(
261
			array( 'January', 'Jan' ),
262
			array( 'February', 'Feb' ),
263
			array( 'March', 'Mar' )
264
		);
265
266
		$this->languageContents->expects( $this->atLeastOnce() )
267
			->method( 'getContentsByLanguageWithIndex' )
268
			->with(
269
				$this->anything(),
270
				$this->equalTo( 'months' ) )
271
			->will( $this->returnValue( $contents ) );
272
273
		$instance = new ExtraneousLanguage(
274
			$this->languageContents
275
		);
276
277
		$this->assertEquals(
278
			'March',
279
			$instance->getMonthLabelByNumber( 3 )
280
		);
281
	}
282
283
}
284