Completed
Push — master ( a09d40...ad1d59 )
by mw
35:44
created

testMediaWikiDate_WithDifferentLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace SMW\Tests\DataValues\ValueFormatters;
4
5
use SMW\DataValues\ValueFormatters\TimeValueFormatter;
6
use SMWTimeValue as TimeValue;
7
8
/**
9
 * @covers \SMW\DataValues\ValueFormatters\TimeValueFormatter
10
 * @group semantic-mediawiki
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.4
14
 *
15
 * @author mwjames
16
 */
17
class TimeValueFormatterTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testCanConstruct() {
20
21
		$this->assertInstanceOf(
22
			'\SMW\DataValues\ValueFormatters\TimeValueFormatter',
23
			new TimeValueFormatter()
24
		);
25
	}
26
27
	public function testIsFormatterForValidation() {
28
29
		$timeValue = $this->getMockBuilder( '\SMWTimeValue' )
30
			->disableOriginalConstructor()
31
			->getMock();
32
33
		$instance = new TimeValueFormatter();
34
35
		$this->assertTrue(
36
			$instance->isFormatterFor( $timeValue )
37
		);
38
	}
39
40
	public function testTryToFormatOnMissingDataValueThrowsException() {
41
42
		$instance = new TimeValueFormatter();
43
44
		$this->setExpectedException( 'RuntimeException' );
45
		$instance->format( TimeValueFormatter::VALUE );
46
	}
47
48
	public function testSetGetOptionValue() {
49
50
		$instance = new TimeValueFormatter();
51
		$instance->setOption( 'Foo', 1001 );
52
53
		$this->assertEquals(
54
			1001,
55
			$instance->getOptionValueFor( 'Foo' )
56
		);
57
	}
58
59
	public function testToUseCaptionOutput() {
60
61
		$timeValue = new TimeValue( '_dat' );
62
		$timeValue->setCaption( 'ABC[<>]' );
63
64
		$instance = new TimeValueFormatter( $timeValue );
65
66
		$this->assertEquals(
67
			'ABC[<>]',
68
			$instance->format( TimeValueFormatter::WIKI_SHORT )
69
		);
70
	}
71
72
	/**
73
	 * @dataProvider timeInputProvider
74
	 */
75
	public function testFormat( $timeUserValue, $type, $format, $linker, $languageCode, $expected ) {
76
77
		$timeValue = new TimeValue( '_dat' );
78
		$timeValue->setUserValue( $timeUserValue );
79
80
		$timeValue->setOutputFormat( $format );
81
		$timeValue->setLanguageCode( $languageCode );
82
83
		$instance = new TimeValueFormatter( $timeValue );
84
85
		$this->assertEquals(
86
			$expected,
87
			$instance->format( $type, $linker )
88
		);
89
	}
90
91
	public function testGetISO8601DateForMinDefault() {
92
93
		$timeValue = new TimeValue( '_dat' );
94
		$timeValue->setUserValue( '2000' );
95
96
		$instance = new TimeValueFormatter( $timeValue );
97
98
		$this->assertEquals(
99
			'2000-01-01',
100
			$instance->getISO8601Date( true )
101
		);
102
103
		$timeValue = new TimeValue( '_dat' );
104
		$timeValue->setUserValue( '2000-02-23 12:02' );
105
106
		$instance = new TimeValueFormatter( $timeValue );
107
108
		$this->assertEquals(
109
			'2000-02-23T12:02:00',
110
			$instance->getISO8601Date( true )
111
		);
112
	}
113
114
	public function testGetISO8601DateForMaxDefault() {
115
116
		$timeValue = new TimeValue( '_dat' );
117
		$timeValue->setUserValue( '2000' );
118
119
		$instance = new TimeValueFormatter( $timeValue );
120
121
		$this->assertEquals(
122
			'2000-12-31',
123
			$instance->getISO8601Date( false )
124
		);
125
126
		$timeValue = new TimeValue( '_dat' );
127
		$timeValue->setUserValue( '2000-02-23 12:02' );
128
129
		$instance = new TimeValueFormatter( $timeValue );
130
131
		$this->assertEquals(
132
			'2000-02-23T12:02:00',
133
			$instance->getISO8601Date( false )
134
		);
135
	}
136
137
	public function testGetCaptionFromDataItemForPositiveYearWithEraMarker() {
138
139
		$timeValue = new TimeValue( '_dat' );
140
		$timeValue->setUserValue( '2000 AD' );
141
142
		$instance = new TimeValueFormatter( $timeValue );
143
144
		$this->assertEquals(
145
			'AD 2000',
146
			$instance->getCaptionFromDataItem( $timeValue->getDataItem() )
0 ignored issues
show
Compatibility introduced by
$timeValue->getDataItem() of type object<SMWDataItem> is not a sub-type of object<SMWDITime>. It seems like you assume a child class of the class SMWDataItem to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
147
		);
148
	}
149
150
	public function testLeapYear() {
151
152
		$timeValue = new TimeValue( '_dat' );
153
		$timeValue->setUserValue( '2016-02-29' );
154
155
		$instance = new TimeValueFormatter( $timeValue );
156
157
		$this->assertEmpty(
158
			$instance->getErrors()
159
		);
160
161
		$this->assertEquals(
162
			'2016-02-29',
163
			$instance->getISO8601Date()
164
		);
165
166
		$timeValue = new TimeValue( '_dat' );
167
		$timeValue->setUserValue( '2016-02' );
168
169
		$instance = new TimeValueFormatter( $timeValue );
170
171
		$this->assertEquals(
172
			'2016-02-29',
173
			$instance->getISO8601Date( false )
174
		);
175
176
		$timeValue = new TimeValue( '_dat' );
177
		$timeValue->setUserValue( '2015-02' );
178
179
		$instance = new TimeValueFormatter( $timeValue );
180
181
		$this->assertEquals(
182
			'2015-02-28',
183
			$instance->getISO8601Date( false )
184
		);
185
	}
186
187
	public function testInvalidLeapYear() {
188
189
		$timeValue = new TimeValue( '_dat' );
190
		$timeValue->setUserValue( '2015-02-29' );
191
192
		$instance = new TimeValueFormatter( $timeValue );
193
194
		$this->assertNotEmpty(
195
			$instance->getErrors()
196
		);
197
	}
198
199
	public function testMediaWikiDate_WithDifferentLanguage() {
200
201
		$timeValue = new TimeValue( '_dat' );
202
		$timeValue->setUserValue( '2015-02-28' );
203
		$timeValue->setLanguageCode( 'en' );
204
205
		$instance = new TimeValueFormatter( $timeValue );
206
207
		$this->assertEquals(
208
			'28 February 2015',
209
			$instance->getMediaWikiDate()
210
		);
211
212
		$timeValue->setLanguageCode( 'ja' );
213
214
		$instance = new TimeValueFormatter( $timeValue );
215
216
		$this->assertEquals(
217
			'2015年2月28日 (土)',
218
			$instance->getMediaWikiDate()
219
		);
220
	}
221
222
	public function testLOCLOutputFormat() {
223
224
		$timeValue = new TimeValue( '_dat' );
225
		$timeValue->setUserValue( '2015-02-28' );
226
227
		$timeValue->setLanguageCode( 'en' );
228
		$timeValue->setOutputFormat( 'LOCL' );
229
230
		$instance = new TimeValueFormatter( $timeValue );
231
232
		$this->assertEquals(
233
			$instance->format( TimeValueFormatter::WIKI_LONG ),
234
			$instance->getLocalizedFormat( $timeValue->getDataItem() )
0 ignored issues
show
Documentation introduced by
$timeValue->getDataItem() is of type object<SMWDataItem>, but the function expects a null|object<SMWDITime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
235
		);
236
237
		$this->assertEquals(
238
			$instance->format( TimeValueFormatter::HTML_LONG ),
239
			$instance->getLocalizedFormat( $timeValue->getDataItem() )
0 ignored issues
show
Documentation introduced by
$timeValue->getDataItem() is of type object<SMWDataItem>, but the function expects a null|object<SMWDITime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
240
		);
241
	}
242
243
	public function timeInputProvider() {
244
245
		#0
246
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
247
			'2000',
248
			TimeValueFormatter::VALUE,
249
			'',
250
			null,
251
			'',
252
			'2000'
253
		);
254
255
		#1
256
		$provider[] = array(
257
			'2000',
258
			TimeValueFormatter::VALUE,
259
			'ISO',
260
			null,
261
			'',
262
			'2000'
263
		);
264
265
		#2
266
		$provider[] = array(
267
			'2000',
268
			TimeValueFormatter::WIKI_SHORT,
269
			'ISO',
270
			null,
271
			'',
272
			'2000'
273
		);
274
275
		#3
276
		$provider[] = array(
277
			'2000',
278
			TimeValueFormatter::HTML_SHORT,
279
			'ISO',
280
			null,
281
			'',
282
			'2000'
283
		);
284
285
		#4
286
		$provider[] = array(
287
			'2000',
288
			TimeValueFormatter::WIKI_LONG,
289
			'ISO',
290
			null,
291
			'',
292
			'2000-01-01'
293
		);
294
295
		#5
296
		$provider[] = array(
297
			'2000',
298
			TimeValueFormatter::HTML_LONG,
299
			'ISO',
300
			null,
301
			'',
302
			'2000-01-01'
303
		);
304
305
		#6
306
		$provider[] = array(
307
			'2000',
308
			TimeValueFormatter::WIKI_LONG,
309
			'MEDIAWIKI',
310
			null,
311
			'',
312
			'2000'
313
		);
314
315
		#7
316
		$provider[] = array(
317
			'2000',
318
			TimeValueFormatter::HTML_LONG,
319
			'MEDIAWIKI',
320
			null,
321
			'',
322
			'2000'
323
		);
324
325
		#8
326
		$provider[] = array(
327
			'2000-02',
328
			TimeValueFormatter::VALUE,
329
			'',
330
			null,
331
			'',
332
			'2000-02'
333
		);
334
335
		#9
336
		$provider[] = array(
337
			'2000-02',
338
			TimeValueFormatter::VALUE,
339
			'ISO',
340
			null,
341
			'',
342
			'2000-02'
343
		);
344
345
		#10
346
		$provider[] = array(
347
			'2000-02',
348
			TimeValueFormatter::WIKI_SHORT,
349
			'',
350
			null,
351
			'',
352
			'2000-02'
353
		);
354
355
		#11
356
		$provider[] = array(
357
			'2000-02',
358
			TimeValueFormatter::HTML_SHORT,
359
			'ISO',
360
			null,
361
			'',
362
			'2000-02'
363
		);
364
365
		#12
366
		$provider[] = array(
367
			'2000-02',
368
			TimeValueFormatter::WIKI_LONG,
369
			'ISO',
370
			null,
371
			'',
372
			'2000-02-01'
373
		);
374
375
		#13
376
		$provider[] = array(
377
			'2000-02',
378
			TimeValueFormatter::HTML_LONG,
379
			'ISO',
380
			null,
381
			'',
382
			'2000-02-01'
383
		);
384
385
		#14
386
		$provider[] = array(
387
			'2000-02',
388
			TimeValueFormatter::HTML_LONG,
389
			'LOCL',
390
			null,
391
			'en',
392
			'February 2000'
393
		);
394
395
		return $provider;
396
	}
397
398
}
399