Passed
Push — master ( 4168c9...c3df72 )
by
unknown
46s queued 11s
created

TimeValueTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 321
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 321
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getClass() 0 3 1
A newInstance() 0 6 1
A instanceProvider() 0 13 1
B validConstructorArgumentsProvider() 0 76 1
B invalidConstructorArgumentsProvider() 0 124 1
A testGetTime() 0 3 1
A testGetTimezone() 0 3 1
A testGetBefore() 0 3 1
A testGetAfter() 0 3 1
A testGetPrecision() 0 3 1
A testGetCalendarModel() 0 3 1
A testGetValue() 0 3 1
A testGivenUnpaddedYear_yearIsPadded() 0 9 1
A unpaddedYearsProvider() 0 11 1
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\TimeValue;
6
use PHPUnit\Framework\TestCase;
7
use ReflectionClass;
8
9
/**
10
 * @covers DataValues\TimeValue
11
 *
12
 * @group DataValue
13
 * @group DataValueExtensions
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Jeroen De Dauw < [email protected] >
17
 * @author Thiemo Kreuz
18
 */
19
class TimeValueTest extends TestCase {
20
21
	/**
22
	 * @see DataValueTest::getClass
23
	 *
24
	 * @return string
25
	 */
26
	public function getClass() {
27
		return TimeValue::class;
28
	}
29
30
	/**
31
	 * Creates and returns a new instance of the concrete class.
32
	 *
33
	 * @since 0.1
34
	 *
35
	 * @return mixed
36
	 */
37
	public function newInstance() {
38
		$reflector = new ReflectionClass( $this->getClass() );
39
		$args = func_get_args();
40
		$instance = $reflector->newInstanceArgs( $args );
41
		return $instance;
42
	}
43
44
	/**
45
	 * @since 0.1
46
	 *
47
	 * @return array [instance, constructor args]
48
	 */
49
	public function instanceProvider() {
50
		$instanceBuilder = [ $this, 'newInstance' ];
51
52
		return array_map(
53
			function ( array $args ) use ( $instanceBuilder ) {
54
				return [
55
					call_user_func_array( $instanceBuilder, $args ),
56
					$args
57
				];
58
			},
59
			$this->validConstructorArgumentsProvider()
60
		);
61
	}
62
63
	public function validConstructorArgumentsProvider() {
64
		return array(
65
			'1 January' => array(
66
				'+2013-01-01T00:00:00Z',
67
				0, 0, 0,
68
				TimeValue::PRECISION_SECOND,
69
				'http://nyan.cat/original.php'
70
			),
71
			'Maximum timezone' => array(
72
				'+2013-01-01T00:00:00Z',
73
				7200, 9001, 9001,
74
				TimeValue::PRECISION_YEAR1G,
75
				'http://nyan.cat/original.php'
76
			),
77
			'Minimum timezone' => array(
78
				'+2013-01-01T00:00:00Z',
79
				-7200, 0, 42,
80
				TimeValue::PRECISION_YEAR,
81
				'http://nyan.cat/original.php'
82
			),
83
			'Negative year' => array(
84
				'-0005-01-01T00:00:00Z',
85
				0, 0, 0,
86
				TimeValue::PRECISION_SECOND,
87
				'http://nyan.cat/original.php'
88
			),
89
			'No day' => array(
90
				'+2015-01-00T00:00:00Z',
91
				0, 0, 0,
92
				TimeValue::PRECISION_YEAR,
93
				'http://nyan.cat/original.php'
94
			),
95
			'No day and month' => array(
96
				'+2015-00-00T00:00:00Z',
97
				0, 0, 0,
98
				TimeValue::PRECISION_YEAR,
99
				'http://nyan.cat/original.php'
100
			),
101
			'Zero' => array(
102
				'+0000-00-00T00:00:00Z',
103
				0, 0, 0,
104
				TimeValue::PRECISION_SECOND,
105
				'http://nyan.cat/original.php'
106
			),
107
			'Minimum timestamp' => array(
108
				'-9999999999999999-12-31T23:59:61Z',
109
				0, 0, 0,
110
				TimeValue::PRECISION_SECOND,
111
				'http://nyan.cat/original.php'
112
			),
113
			'Maximum timestamp' => array(
114
				'+9999999999999999-12-31T23:59:61Z',
115
				0, 0, 0,
116
				TimeValue::PRECISION_SECOND,
117
				'http://nyan.cat/original.php'
118
			),
119
			'Leap year' => array(
120
				'+2000-02-29T00:00:00Z',
121
				0, 0, 0,
122
				TimeValue::PRECISION_DAY,
123
				'http://nyan.cat/original.php'
124
			),
125
			'Non-leap year 29 February' => array(
126
				'+2015-02-29T00:00:00Z',
127
				0, 0, 0,
128
				TimeValue::PRECISION_DAY,
129
				'http://nyan.cat/original.php'
130
			),
131
			'31 November' => array(
132
				'+2015-11-31T00:00:00Z',
133
				0, 0, 0,
134
				TimeValue::PRECISION_DAY,
135
				'http://nyan.cat/original.php'
136
			),
137
		);
138
	}
139
140
	public function invalidConstructorArgumentsProvider() {
141
		return array(
142
			'String timezone' => array(
143
				'+00000002013-01-01T00:00:00Z',
144
				'0', 0, 0,
145
				TimeValue::PRECISION_SECOND,
146
				'http://nyan.cat/original.php'
147
			),
148
			'Float timezone' => array(
149
				'+00000002013-01-01T00:00:00Z',
150
				4.2, 0, 0,
151
				TimeValue::PRECISION_SECOND,
152
				'http://nyan.cat/original.php'
153
			),
154
			'Timezone out of range' => array(
155
				'+00000002013-01-01T00:00:00Z',
156
				-20 * 3600, 0, 0,
157
				TimeValue::PRECISION_SECOND,
158
				'http://nyan.cat/original.php'
159
			),
160
			'Precision out of range' => array(
161
				'+00000002013-01-01T00:00:00Z',
162
				0, 0, 0,
163
				15,
164
				'http://nyan.cat/original.php'
165
			),
166
			'Integer timestamp' => array(
167
				42,
168
				0, 0, 0,
169
				TimeValue::PRECISION_SECOND,
170
				'http://nyan.cat/original.php'
171
			),
172
			'Float before' => array(
173
				'+00000002013-01-01T00:00:00Z',
174
				0, 4.2, 0,
175
				TimeValue::PRECISION_SECOND,
176
				'http://nyan.cat/original.php'
177
			),
178
			'Negative after' => array(
179
				'+00000002013-01-01T00:00:00Z',
180
				0, 0, -1,
181
				TimeValue::PRECISION_SECOND,
182
				'http://nyan.cat/original.php'
183
			),
184
			'Non-ISO timestamp' => array(
185
				'bla',
186
				0, 0, 0,
187
				TimeValue::PRECISION_SECOND,
188
				'http://nyan.cat/original.php'
189
			),
190
			'Invalid separators' => array(
191
				'+00000002013/01/01 00:00:00',
192
				0, 0, 0,
193
				TimeValue::PRECISION_SECOND,
194
				'http://nyan.cat/original.php'
195
			),
196
			'No month' => array(
197
				'+2015-00-01T00:00:00Z',
198
				0, 0, 0,
199
				TimeValue::PRECISION_DAY,
200
				'http://nyan.cat/original.php'
201
			),
202
			'No day but hour' => array(
203
				'+2015-01-00T01:00:00Z',
204
				0, 0, 0,
205
				TimeValue::PRECISION_DAY,
206
				'http://nyan.cat/original.php'
207
			),
208
			'No day but minute' => array(
209
				'+2015-01-00T00:01:00Z',
210
				0, 0, 0,
211
				TimeValue::PRECISION_DAY,
212
				'http://nyan.cat/original.php'
213
			),
214
			'No day but second' => array(
215
				'+2015-01-00T00:00:01Z',
216
				0, 0, 0,
217
				TimeValue::PRECISION_DAY,
218
				'http://nyan.cat/original.php'
219
			),
220
			'Month out of range' => array(
221
				'+00000002013-13-01T00:00:00Z',
222
				0, 0, 0,
223
				TimeValue::PRECISION_SECOND,
224
				'http://nyan.cat/original.php'
225
			),
226
			'Day out of range' => array(
227
				'+00000002013-01-32T00:00:00Z',
228
				0, 0, 0,
229
				TimeValue::PRECISION_SECOND,
230
				'http://nyan.cat/original.php'
231
			),
232
			'Hour out of range' => array(
233
				'+00000002013-01-01T24:00:00Z',
234
				0, 0, 0,
235
				TimeValue::PRECISION_SECOND,
236
				'http://nyan.cat/original.php'
237
			),
238
			'Minute out of range' => array(
239
				'+00000002013-01-01T00:60:00Z',
240
				0, 0, 0,
241
				TimeValue::PRECISION_SECOND,
242
				'http://nyan.cat/original.php'
243
			),
244
			'Second out of range' => array(
245
				'+00000002013-01-01T00:00:62Z',
246
				0, 0, 0,
247
				TimeValue::PRECISION_SECOND,
248
				'http://nyan.cat/original.php'
249
			),
250
			'Invalid timezone' => array(
251
				'+00000002013-01-01T00:00:00+60',
252
				0, 0, 0,
253
				TimeValue::PRECISION_SECOND,
254
				'http://nyan.cat/original.php'
255
			),
256
			'Year to long' => array(
257
				'+00000000000000001-01-01T00:00:00Z',
258
				0, 0, 0,
259
				TimeValue::PRECISION_DAY,
260
				'http://nyan.cat/original.php'
261
			),
262
		);
263
	}
264
265
	/**
266
	 * @dataProvider instanceProvider
267
	 */
268
	public function testGetTime( TimeValue $time, array $arguments ) {
269
		$this->assertEquals( $arguments[0], $time->getTime() );
270
	}
271
272
	/**
273
	 * @dataProvider instanceProvider
274
	 */
275
	public function testGetTimezone( TimeValue $time, array $arguments ) {
276
		$this->assertEquals( $arguments[1], $time->getTimezone() );
277
	}
278
279
	/**
280
	 * @dataProvider instanceProvider
281
	 */
282
	public function testGetBefore( TimeValue $time, array $arguments ) {
283
		$this->assertEquals( $arguments[2], $time->getBefore() );
284
	}
285
286
	/**
287
	 * @dataProvider instanceProvider
288
	 */
289
	public function testGetAfter( TimeValue $time, array $arguments ) {
290
		$this->assertEquals( $arguments[3], $time->getAfter() );
291
	}
292
293
	/**
294
	 * @dataProvider instanceProvider
295
	 */
296
	public function testGetPrecision( TimeValue $time, array $arguments ) {
297
		$this->assertEquals( $arguments[4], $time->getPrecision() );
298
	}
299
300
	/**
301
	 * @dataProvider instanceProvider
302
	 */
303
	public function testGetCalendarModel( TimeValue $time, array $arguments ) {
304
		$this->assertEquals( $arguments[5], $time->getCalendarModel() );
305
	}
306
307
	/**
308
	 * @dataProvider instanceProvider
309
	 */
310
	public function testGetValue( TimeValue $time, array $arguments ) {
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
311
		$this->assertTrue( $time->equals( $time->getValue() ) );
312
	}
313
314
	/**
315
	 * @dataProvider unpaddedYearsProvider
316
	 */
317
	public function testGivenUnpaddedYear_yearIsPadded( $year, $expected ) {
318
		$timeValue = new TimeValue(
319
			$year . '-01-01T00:00:00Z',
320
			0, 0, 0,
321
			TimeValue::PRECISION_DAY,
322
			'Stardate'
323
		);
324
		$this->assertSame( $expected . '-01-01T00:00:00Z', $timeValue->getTime() );
325
	}
326
327
	public function unpaddedYearsProvider() {
328
		return array(
329
			array( '+1', '+0001' ),
330
			array( '-10', '-0010' ),
331
			array( '+2015', '+2015' ),
332
			array( '+02015', '+2015' ),
333
			array( '+00000010000', '+10000' ),
334
			array( '+0000000000000001', '+0001' ),
335
			array( '+9999999999999999', '+9999999999999999' ),
336
		);
337
	}
338
339
}
340