Completed
Push — master ( 419e79...27e7c2 )
by mw
32:50
created

testGetCaptionFromDataItemForPositiveYearWithEraMarker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 7
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, $expected ) {
76
77
		$timeValue = new TimeValue( '_dat' );
78
		$timeValue->setUserValue( $timeUserValue );
79
		$timeValue->setOutputFormat( $format );
80
81
		$instance = new TimeValueFormatter( $timeValue );
82
83
		$this->assertEquals(
84
			$expected,
85
			$instance->format( $type, $linker )
86
		);
87
	}
88
89
	public function testGetISO8601DateForMinDefault() {
90
91
		$timeValue = new TimeValue( '_dat' );
92
		$timeValue->setUserValue( '2000' );
93
94
		$instance = new TimeValueFormatter( $timeValue );
95
96
		$this->assertEquals(
97
			'2000-01-01',
98
			$instance->getISO8601Date( true )
99
		);
100
101
		$timeValue = new TimeValue( '_dat' );
102
		$timeValue->setUserValue( '2000-02-23 12:02' );
103
104
		$instance = new TimeValueFormatter( $timeValue );
105
106
		$this->assertEquals(
107
			'2000-02-23T12:02:00',
108
			$instance->getISO8601Date( true )
109
		);
110
	}
111
112
	public function testGetISO8601DateForMaxDefault() {
113
114
		$timeValue = new TimeValue( '_dat' );
115
		$timeValue->setUserValue( '2000' );
116
117
		$instance = new TimeValueFormatter( $timeValue );
118
119
		$this->assertEquals(
120
			'2000-12-31',
121
			$instance->getISO8601Date( false )
122
		);
123
124
		$timeValue = new TimeValue( '_dat' );
125
		$timeValue->setUserValue( '2000-02-23 12:02' );
126
127
		$instance = new TimeValueFormatter( $timeValue );
128
129
		$this->assertEquals(
130
			'2000-02-23T12:02:00',
131
			$instance->getISO8601Date( false )
132
		);
133
	}
134
135
	public function testGetCaptionFromDataItemForPositiveYearWithEraMarker() {
136
137
		$timeValue = new TimeValue( '_dat' );
138
		$timeValue->setUserValue( '2000 AD' );
139
140
		$instance = new TimeValueFormatter( $timeValue );
141
142
		$this->assertEquals(
143
			'AD 2000',
144
			$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...
145
		);
146
	}
147
148
	public function testLeapYear() {
149
150
		$timeValue = new TimeValue( '_dat' );
151
		$timeValue->setUserValue( '2016-02-29' );
152
153
		$instance = new TimeValueFormatter( $timeValue );
154
155
		$this->assertEmpty(
156
			$instance->getErrors()
157
		);
158
159
		$this->assertEquals(
160
			'2016-02-29',
161
			$instance->getISO8601Date()
162
		);
163
164
		$timeValue = new TimeValue( '_dat' );
165
		$timeValue->setUserValue( '2016-02' );
166
167
		$instance = new TimeValueFormatter( $timeValue );
168
169
		$this->assertEquals(
170
			'2016-02-29',
171
			$instance->getISO8601Date( false )
172
		);
173
174
		$timeValue = new TimeValue( '_dat' );
175
		$timeValue->setUserValue( '2015-02' );
176
177
		$instance = new TimeValueFormatter( $timeValue );
178
179
		$this->assertEquals(
180
			'2015-02-28',
181
			$instance->getISO8601Date( false )
182
		);
183
	}
184
185
	public function testInvalidLeapYear() {
186
187
		$timeValue = new TimeValue( '_dat' );
188
		$timeValue->setUserValue( '2015-02-29' );
189
190
		$instance = new TimeValueFormatter( $timeValue );
191
192
		$this->assertNotEmpty(
193
			$instance->getErrors()
194
		);
195
	}
196
197
	public function timeInputProvider() {
198
199
		$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...
200
			'2000',
201
			TimeValueFormatter::VALUE,
202
			'',
203
			null,
204
			'2000'
205
		);
206
207
		$provider[] = array(
208
			'2000',
209
			TimeValueFormatter::VALUE,
210
			'ISO',
211
			null,
212
			'2000'
213
		);
214
215
		$provider[] = array(
216
			'2000',
217
			TimeValueFormatter::WIKI_SHORT,
218
			'ISO',
219
			null,
220
			'2000'
221
		);
222
223
		$provider[] = array(
224
			'2000',
225
			TimeValueFormatter::HTML_SHORT,
226
			'ISO',
227
			null,
228
			'2000'
229
		);
230
231
		$provider[] = array(
232
			'2000',
233
			TimeValueFormatter::WIKI_LONG,
234
			'ISO',
235
			null,
236
			'2000-01-01'
237
		);
238
239
		$provider[] = array(
240
			'2000',
241
			TimeValueFormatter::HTML_LONG,
242
			'ISO',
243
			null,
244
			'2000-01-01'
245
		);
246
247
		$provider[] = array(
248
			'2000',
249
			TimeValueFormatter::WIKI_LONG,
250
			'MEDIAWIKI',
251
			null,
252
			'2000'
253
		);
254
255
		$provider[] = array(
256
			'2000',
257
			TimeValueFormatter::HTML_LONG,
258
			'MEDIAWIKI',
259
			null,
260
			'2000'
261
		);
262
263
		$provider[] = array(
264
			'2000-02',
265
			TimeValueFormatter::VALUE,
266
			'',
267
			null,
268
			'2000-02'
269
		);
270
271
		$provider[] = array(
272
			'2000-02',
273
			TimeValueFormatter::VALUE,
274
			'ISO',
275
			null,
276
			'2000-02'
277
		);
278
279
		$provider[] = array(
280
			'2000-02',
281
			TimeValueFormatter::WIKI_SHORT,
282
			'',
283
			null,
284
			'2000-02'
285
		);
286
287
		$provider[] = array(
288
			'2000-02',
289
			TimeValueFormatter::HTML_SHORT,
290
			'ISO',
291
			null,
292
			'2000-02'
293
		);
294
295
		$provider[] = array(
296
			'2000-02',
297
			TimeValueFormatter::WIKI_LONG,
298
			'ISO',
299
			null,
300
			'2000-02-01'
301
		);
302
303
		$provider[] = array(
304
			'2000-02',
305
			TimeValueFormatter::HTML_LONG,
306
			'ISO',
307
			null,
308
			'2000-02-01'
309
		);
310
311
		return $provider;
312
	}
313
314
}
315