Completed
Push — master ( c83be9...d2b57a )
by mw
14s
created

testLOCLOutputFormatWithSpecificAnnotatedLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 1
b 0
f 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
82
		$timeValue->setOption( 'user.language', $languageCode );
83
		$timeValue->setOption( 'content.language', $languageCode );
84
85
		$instance = new TimeValueFormatter( $timeValue );
86
87
		$this->assertEquals(
88
			$expected,
89
			$instance->format( $type, $linker )
90
		);
91
	}
92
93
	public function testGetISO8601DateForMinDefault() {
94
95
		$timeValue = new TimeValue( '_dat' );
96
		$timeValue->setUserValue( '2000' );
97
98
		$instance = new TimeValueFormatter( $timeValue );
99
100
		$this->assertEquals(
101
			'2000-01-01',
102
			$instance->getISO8601Date( true )
103
		);
104
105
		$timeValue = new TimeValue( '_dat' );
106
		$timeValue->setUserValue( '2000-02-23 12:02' );
107
108
		$instance = new TimeValueFormatter( $timeValue );
109
110
		$this->assertEquals(
111
			'2000-02-23T12:02:00',
112
			$instance->getISO8601Date( true )
113
		);
114
	}
115
116
	public function testGetISO8601DateForMaxDefault() {
117
118
		$timeValue = new TimeValue( '_dat' );
119
		$timeValue->setUserValue( '2000' );
120
121
		$instance = new TimeValueFormatter( $timeValue );
122
123
		$this->assertEquals(
124
			'2000-12-31',
125
			$instance->getISO8601Date( false )
126
		);
127
128
		$timeValue = new TimeValue( '_dat' );
129
		$timeValue->setUserValue( '2000-02-23 12:02' );
130
131
		$instance = new TimeValueFormatter( $timeValue );
132
133
		$this->assertEquals(
134
			'2000-02-23T12:02:00',
135
			$instance->getISO8601Date( false )
136
		);
137
	}
138
139
	public function testGetCaptionFromDataItemForPositiveYearWithEraMarker() {
140
141
		$timeValue = new TimeValue( '_dat' );
142
		$timeValue->setUserValue( '2000 AD' );
143
144
		$instance = new TimeValueFormatter( $timeValue );
145
146
		$this->assertEquals(
147
			'AD 2000',
148
			$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...
149
		);
150
	}
151
152
	public function testLeapYear() {
153
154
		$timeValue = new TimeValue( '_dat' );
155
		$timeValue->setUserValue( '2016-02-29' );
156
157
		$instance = new TimeValueFormatter( $timeValue );
158
159
		$this->assertEmpty(
160
			$instance->getErrors()
161
		);
162
163
		$this->assertEquals(
164
			'2016-02-29',
165
			$instance->getISO8601Date()
166
		);
167
168
		$timeValue = new TimeValue( '_dat' );
169
		$timeValue->setUserValue( '2016-02' );
170
171
		$instance = new TimeValueFormatter( $timeValue );
172
173
		$this->assertEquals(
174
			'2016-02-29',
175
			$instance->getISO8601Date( false )
176
		);
177
178
		$timeValue = new TimeValue( '_dat' );
179
		$timeValue->setUserValue( '2015-02' );
180
181
		$instance = new TimeValueFormatter( $timeValue );
182
183
		$this->assertEquals(
184
			'2015-02-28',
185
			$instance->getISO8601Date( false )
186
		);
187
	}
188
189
	public function testInvalidLeapYear() {
190
191
		$timeValue = new TimeValue( '_dat' );
192
		$timeValue->setUserValue( '2015-02-29' );
193
194
		$instance = new TimeValueFormatter( $timeValue );
195
196
		$this->assertNotEmpty(
197
			$instance->getErrors()
198
		);
199
	}
200
201
	public function testMediaWikiDate_WithDifferentLanguage() {
202
203
		$timeValue = new TimeValue( '_dat' );
204
205
		$timeValue->setUserValue( '2015-02-28' );
206
		$timeValue->setOption( 'user.language', 'en' );
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a object<mxied>.

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...
207
208
		$instance = new TimeValueFormatter( $timeValue );
209
210
		$this->assertEquals(
211
			'28 February 2015',
212
			$instance->getMediaWikiDate()
213
		);
214
215
		$timeValue->setOption( 'user.language', 'ja' );
0 ignored issues
show
Documentation introduced by
'ja' is of type string, but the function expects a object<mxied>.

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...
216
217
		$instance = new TimeValueFormatter( $timeValue );
218
219
		$this->assertEquals(
220
			'2015年2月28日 (土)',
221
			$instance->getMediaWikiDate()
222
		);
223
	}
224
225
	public function testLOCLOutputFormat() {
226
227
		$timeValue = new TimeValue( '_dat' );
228
		$timeValue->setUserValue( '2015-02-28' );
229
230
		$timeValue->setOption( TimeValue::OPT_USER_LANGUAGE, 'en' );
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a object<mxied>.

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...
231
		$timeValue->setOutputFormat( 'LOCL' );
232
233
		$instance = new TimeValueFormatter( $timeValue );
234
235
		$this->assertEquals(
236
			$instance->format( TimeValueFormatter::WIKI_LONG ),
237
			$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...
238
		);
239
240
		$this->assertEquals(
241
			$instance->format( TimeValueFormatter::HTML_LONG ),
242
			$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...
243
		);
244
	}
245
246
	public function testLOCLOutputFormatWithSpecificAnnotatedLanguage() {
247
248
		$timeValue = new TimeValue( '_dat' );
249
		$timeValue->setUserValue( '2015-02-28' );
250
251
		$timeValue->setOption( TimeValue::OPT_USER_LANGUAGE, 'en' );
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a object<mxied>.

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...
252
		$timeValue->setOutputFormat( 'LOCL@ja' );
253
254
		$instance = new TimeValueFormatter( $timeValue );
255
256
		$this->assertEquals(
257
			'2015年2月28日 (土)',
258
			$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...
259
		);
260
	}
261
262
	public function timeInputProvider() {
263
264
		#0
265
		$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...
266
			'2000',
267
			TimeValueFormatter::VALUE,
268
			'',
269
			null,
270
			'',
271
			'2000'
272
		);
273
274
		#1
275
		$provider[] = array(
276
			'2000',
277
			TimeValueFormatter::VALUE,
278
			'ISO',
279
			null,
280
			'',
281
			'2000'
282
		);
283
284
		#2
285
		$provider[] = array(
286
			'2000',
287
			TimeValueFormatter::WIKI_SHORT,
288
			'ISO',
289
			null,
290
			'',
291
			'2000'
292
		);
293
294
		#3
295
		$provider[] = array(
296
			'2000',
297
			TimeValueFormatter::HTML_SHORT,
298
			'ISO',
299
			null,
300
			'',
301
			'2000'
302
		);
303
304
		#4
305
		$provider[] = array(
306
			'2000',
307
			TimeValueFormatter::WIKI_LONG,
308
			'ISO',
309
			null,
310
			'',
311
			'2000-01-01'
312
		);
313
314
		#5
315
		$provider[] = array(
316
			'2000',
317
			TimeValueFormatter::HTML_LONG,
318
			'ISO',
319
			null,
320
			'',
321
			'2000-01-01'
322
		);
323
324
		#6
325
		$provider[] = array(
326
			'2000',
327
			TimeValueFormatter::WIKI_LONG,
328
			'MEDIAWIKI',
329
			null,
330
			'',
331
			'2000'
332
		);
333
334
		#7
335
		$provider[] = array(
336
			'2000',
337
			TimeValueFormatter::HTML_LONG,
338
			'MEDIAWIKI',
339
			null,
340
			'',
341
			'2000'
342
		);
343
344
		#8
345
		$provider[] = array(
346
			'2000-02',
347
			TimeValueFormatter::VALUE,
348
			'',
349
			null,
350
			'',
351
			'2000-02'
352
		);
353
354
		#9
355
		$provider[] = array(
356
			'2000-02',
357
			TimeValueFormatter::VALUE,
358
			'ISO',
359
			null,
360
			'',
361
			'2000-02'
362
		);
363
364
		#10
365
		$provider[] = array(
366
			'2000-02',
367
			TimeValueFormatter::WIKI_SHORT,
368
			'',
369
			null,
370
			'',
371
			'2000-02'
372
		);
373
374
		#11
375
		$provider[] = array(
376
			'2000-02',
377
			TimeValueFormatter::HTML_SHORT,
378
			'ISO',
379
			null,
380
			'',
381
			'2000-02'
382
		);
383
384
		#12
385
		$provider[] = array(
386
			'2000-02',
387
			TimeValueFormatter::WIKI_LONG,
388
			'ISO',
389
			null,
390
			'',
391
			'2000-02-01'
392
		);
393
394
		#13
395
		$provider[] = array(
396
			'2000-02',
397
			TimeValueFormatter::HTML_LONG,
398
			'ISO',
399
			null,
400
			'',
401
			'2000-02-01'
402
		);
403
404
		#14
405
		$provider[] = array(
406
			'2000-02',
407
			TimeValueFormatter::HTML_LONG,
408
			'LOCL',
409
			null,
410
			'en',
411
			'February 2000'
412
		);
413
414
		return $provider;
415
	}
416
417
}
418