Completed
Push — master ( 944e8c...873a6a )
by mw
32:14
created

TimeValueFormatterTest::testToUseCaptionOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 1
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 testLeapYear() {
136
137
		$timeValue = new TimeValue( '_dat' );
138
		$timeValue->setUserValue( '2016-02-29' );
139
140
		$instance = new TimeValueFormatter( $timeValue );
141
142
		$this->assertEmpty(
143
			$instance->getErrors()
144
		);
145
146
		$this->assertEquals(
147
			'2016-02-29',
148
			$instance->getISO8601Date()
149
		);
150
151
		$timeValue = new TimeValue( '_dat' );
152
		$timeValue->setUserValue( '2016-02' );
153
154
		$instance = new TimeValueFormatter( $timeValue );
155
156
		$this->assertEquals(
157
			'2016-02-29',
158
			$instance->getISO8601Date( false )
159
		);
160
161
		$timeValue = new TimeValue( '_dat' );
162
		$timeValue->setUserValue( '2015-02' );
163
164
		$instance = new TimeValueFormatter( $timeValue );
165
166
		$this->assertEquals(
167
			'2015-02-28',
168
			$instance->getISO8601Date( false )
169
		);
170
	}
171
172
	public function testInvalidLeapYear() {
173
174
		$timeValue = new TimeValue( '_dat' );
175
		$timeValue->setUserValue( '2015-02-29' );
176
177
		$instance = new TimeValueFormatter( $timeValue );
178
179
		$this->assertNotEmpty(
180
			$instance->getErrors()
181
		);
182
	}
183
184
	public function timeInputProvider() {
185
186
		$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...
187
			'2000',
188
			TimeValueFormatter::VALUE,
189
			'',
190
			null,
191
			'2000'
192
		);
193
194
		$provider[] = array(
195
			'2000',
196
			TimeValueFormatter::VALUE,
197
			'ISO',
198
			null,
199
			'2000'
200
		);
201
202
		$provider[] = array(
203
			'2000',
204
			TimeValueFormatter::WIKI_SHORT,
205
			'ISO',
206
			null,
207
			'2000'
208
		);
209
210
		$provider[] = array(
211
			'2000',
212
			TimeValueFormatter::HTML_SHORT,
213
			'ISO',
214
			null,
215
			'2000'
216
		);
217
218
		$provider[] = array(
219
			'2000',
220
			TimeValueFormatter::WIKI_LONG,
221
			'ISO',
222
			null,
223
			'2000-01-01'
224
		);
225
226
		$provider[] = array(
227
			'2000',
228
			TimeValueFormatter::HTML_LONG,
229
			'ISO',
230
			null,
231
			'2000-01-01'
232
		);
233
234
		$provider[] = array(
235
			'2000',
236
			TimeValueFormatter::WIKI_LONG,
237
			'MEDIAWIKI',
238
			null,
239
			'2000'
240
		);
241
242
		$provider[] = array(
243
			'2000',
244
			TimeValueFormatter::HTML_LONG,
245
			'MEDIAWIKI',
246
			null,
247
			'2000'
248
		);
249
250
		$provider[] = array(
251
			'2000-02',
252
			TimeValueFormatter::VALUE,
253
			'',
254
			null,
255
			'2000-02'
256
		);
257
258
		$provider[] = array(
259
			'2000-02',
260
			TimeValueFormatter::VALUE,
261
			'ISO',
262
			null,
263
			'2000-02'
264
		);
265
266
		$provider[] = array(
267
			'2000-02',
268
			TimeValueFormatter::WIKI_SHORT,
269
			'',
270
			null,
271
			'2000-02'
272
		);
273
274
		$provider[] = array(
275
			'2000-02',
276
			TimeValueFormatter::HTML_SHORT,
277
			'ISO',
278
			null,
279
			'2000-02'
280
		);
281
282
		$provider[] = array(
283
			'2000-02',
284
			TimeValueFormatter::WIKI_LONG,
285
			'ISO',
286
			null,
287
			'2000-02-01'
288
		);
289
290
		$provider[] = array(
291
			'2000-02',
292
			TimeValueFormatter::HTML_LONG,
293
			'ISO',
294
			null,
295
			'2000-02-01'
296
		);
297
298
		return $provider;
299
	}
300
301
}
302