Completed
Push — master ( d0ca26...98fb90 )
by mw
37:57
created

Unit/ExtraneousLanguage/ExtraneousLanguageTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
	public function testCanConstruct() {
22
23
		$languageContents = $this->getMockBuilder( LanguageContents::class )
24
			->disableOriginalConstructor()
25
			->getMock();
26
27
		$this->assertInstanceOf(
28
			ExtraneousLanguage::class,
29
			new ExtraneousLanguage( $languageContents )
30
		);
31
32
		$this->assertInstanceOf(
33
			ExtraneousLanguage::class,
34
			ExtraneousLanguage::getInstance()
35
		);
36
37
		ExtraneousLanguage::clear();
38
	}
39
40
	public function testGetNamespaces() {
41
42
		$contents = array(
43
			"SMW_NS_PROPERTY" => "Property"
44
		);
45
46
		$languageContents = $this->getMockBuilder( LanguageContents::class )
47
			->disableOriginalConstructor()
48
			->getMock();
49
50
		$languageContents->expects( $this->atLeastOnce() )
51
			->method( 'getFromLanguageWithIndex' )
52
			->with(
53
				$this->anything(),
54
				$this->equalTo( 'namespaces' ) )
55
			->will( $this->returnValue( $contents ) );
56
57
		$instance = new ExtraneousLanguage(
58
			$languageContents
59
		);
60
61
		$this->assertEquals(
62
			array( SMW_NS_PROPERTY => "Property" ),
63
			$instance->getNamespaces()
64
		);
65
	}
66
67
	public function testGetNamespaceAliases() {
68
69
		$contents = array(
70
			"Property" => "SMW_NS_PROPERTY"
71
		);
72
73
		$languageContents = $this->getMockBuilder( LanguageContents::class )
74
			->disableOriginalConstructor()
75
			->getMock();
76
77
		$languageContents->expects( $this->atLeastOnce() )
78
			->method( 'getFromLanguageWithIndex' )
79
			->with(
80
				$this->anything(),
81
				$this->equalTo( 'namespaceAliases' ) )
82
			->will( $this->returnValue( $contents ) );
83
84
		$instance = new ExtraneousLanguage(
85
			$languageContents
86
		);
87
88
		$this->assertEquals(
89
			array( "Property" => SMW_NS_PROPERTY ),
90
			$instance->getNamespaceAliases()
91
		);
92
	}
93
94
	public function testGetPreferredDateFormatByPrecisionOnMatchedPrecision() {
95
96
		$contents = array(
97
			"SMW_PREC_YMDT" => "d m Y"
98
		);
99
100
		$languageContents = $this->getMockBuilder( LanguageContents::class )
101
			->disableOriginalConstructor()
102
			->getMock();
103
104
		$languageContents->expects( $this->atLeastOnce() )
105
			->method( 'getFromLanguageWithIndex' )
106
			->with(
107
				$this->anything(),
108
				$this->equalTo( 'dateFormatsByPrecision' ) )
109
			->will( $this->returnValue( $contents ) );
110
111
		$instance = new ExtraneousLanguage(
112
			$languageContents
113
		);
114
115
		$this->assertEquals(
116
			'd m Y',
117
			$instance->getPreferredDateFormatByPrecision( SMW_PREC_YMDT )
118
		);
119
	}
120
121
	public function testGetPreferredDateFormatOnNotMatchablePrecision() {
122
123
		$contents = array(
124
			"Foo" => "d m Y"
125
		);
126
127
		$languageContents = $this->getMockBuilder( LanguageContents::class )
128
			->disableOriginalConstructor()
129
			->getMock();
130
131
		$languageContents->expects( $this->atLeastOnce() )
132
			->method( 'getFromLanguageWithIndex' )
133
			->with(
134
				$this->anything(),
135
				$this->equalTo( 'dateFormatsByPrecision' ) )
136
			->will( $this->returnValue( $contents ) );
137
138
		$instance = new ExtraneousLanguage(
139
			$languageContents
140
		);
141
142
		$this->assertEquals(
143
			'd F Y H:i:s',
144
			$instance->getPreferredDateFormatByPrecision( SMW_PREC_YMDT )
145
		);
146
	}
147
148
	public function testGetDatatypeLabels() {
149
150
		$contents = array(
151
			"Foo" => "Bar"
152
		);
153
154
		$languageContents = $this->getMockBuilder( LanguageContents::class )
155
			->disableOriginalConstructor()
156
			->getMock();
157
158
		$languageContents->expects( $this->atLeastOnce() )
159
			->method( 'getFromLanguageWithIndex' )
160
			->with(
161
				$this->anything(),
162
				$this->equalTo( 'dataTypeLabels' ) )
163
			->will( $this->returnValue( $contents ) );
164
165
		$instance = new ExtraneousLanguage(
166
			$languageContents
167
		);
168
169
		$this->assertEquals(
170
			array( "Foo" => 'Bar' ),
171
			$instance->getDatatypeLabels()
172
		);
173
	}
174
175
	public function testGetPropertyIdByLabel() {
176
177
		$languageContents = $this->getMockBuilder( LanguageContents::class )
178
			->disableOriginalConstructor()
179
			->getMock();
180
181
		$languageContents->expects( $this->at( 0 ) )
182
			->method( 'getFromLanguageWithIndex' )
183
			->with(
184
				$this->anything(),
185
				$this->equalTo( 'propertyLabels' ) )
186
			->will( $this->returnValue( array( "_FOO" => "Foo" ) ) );
187
188
		$languageContents->expects( $this->at( 2 ) )
189
			->method( 'getFromLanguageWithIndex' )
190
			->will( $this->returnValue( array() ) );
191
192
//		$languageContents->expects( $this->at( 5 ) )
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
193
//			->method( 'getFromLanguageWithIndex' )
194
//			->with(
195
//				$this->anything(),
196
//				$this->equalTo( 'propertyAliases' ) )
197
//			->will( $this->returnValue( array( "Foo" => "_FOO" ) ) );
198
199
		$instance = new ExtraneousLanguage(
200
			$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
		$languageContents = $this->getMockBuilder( LanguageContents::class )
217
			->disableOriginalConstructor()
218
			->getMock();
219
220
		$languageContents->expects( $this->atLeastOnce() )
221
			->method( 'getFromLanguageWithIndex' )
222
			->with(
223
				$this->anything(),
224
				$this->equalTo( 'dateFormats' ) )
225
			->will( $this->returnValue( $contents ) );
226
227
		$instance = new ExtraneousLanguage(
228
			$languageContents
229
		);
230
231
		$this->assertEquals(
232
			array( array( 9 ), array( 97, 76 ) ),
233
			$instance->getDateFormats()
234
		);
235
	}
236
237
	public function testFindMonthNumberByLabelWithCaseInsensitiveSearch() {
238
239
		$contents = array(
240
			array( 'January', 'Jan' ),
241
			array( 'February', 'Feb' ),
242
			array( 'March', 'Mar' )
243
		);
244
245
		$languageContents = $this->getMockBuilder( LanguageContents::class )
246
			->disableOriginalConstructor()
247
			->getMock();
248
249
		$languageContents->expects( $this->atLeastOnce() )
250
			->method( 'getFromLanguageWithIndex' )
251
			->with(
252
				$this->anything(),
253
				$this->equalTo( 'months' ) )
254
			->will( $this->returnValue( $contents ) );
255
256
		$instance = new ExtraneousLanguage(
257
			$languageContents
258
		);
259
260
		$this->assertEquals(
261
			3,
262
			$instance->findMonthNumberByLabel( 'mar' )
263
		);
264
	}
265
266
	public function testGetMonthLabelByNumber() {
267
268
		$contents = array(
269
			array( 'January', 'Jan' ),
270
			array( 'February', 'Feb' ),
271
			array( 'March', 'Mar' )
272
		);
273
274
		$languageContents = $this->getMockBuilder( LanguageContents::class )
275
			->disableOriginalConstructor()
276
			->getMock();
277
278
		$languageContents->expects( $this->atLeastOnce() )
279
			->method( 'getFromLanguageWithIndex' )
280
			->with(
281
				$this->anything(),
282
				$this->equalTo( 'months' ) )
283
			->will( $this->returnValue( $contents ) );
284
285
		$instance = new ExtraneousLanguage(
286
			$languageContents
287
		);
288
289
		$this->assertEquals(
290
			'March',
291
			$instance->getMonthLabelByNumber( 3 )
292
		);
293
	}
294
295
}
296