TimeValueTest::testGetValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\IllegalValueException;
6
use DataValues\TimeValue;
7
use PHPUnit\Framework\TestCase;
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
	public function instanceProvider() {
22
		foreach ( $this->validConstructorArgumentsProvider() as $key => $args ) {
23
			yield $key => [ new TimeValue( ...$args ), $args ];
0 ignored issues
show
Bug introduced by
The call to TimeValue::__construct() misses some required arguments starting with $timezone.
Loading history...
Documentation introduced by
$args is of type array<integer,string|int...integer","5":"string"}>, but the function expects a string.

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...
24
		}
25
	}
26
27
	public function validConstructorArgumentsProvider() {
28
		return array(
29
			'1 January' => array(
30
				'+2013-01-01T00:00:00Z',
31
				0, 0, 0,
32
				TimeValue::PRECISION_SECOND,
33
				'http://nyan.cat/original.php'
34
			),
35
			'Maximum timezone' => array(
36
				'+2013-01-01T00:00:00Z',
37
				7200, 9001, 9001,
38
				TimeValue::PRECISION_YEAR1G,
39
				'http://nyan.cat/original.php'
40
			),
41
			'Minimum timezone' => array(
42
				'+2013-01-01T00:00:00Z',
43
				-7200, 0, 42,
44
				TimeValue::PRECISION_YEAR,
45
				'http://nyan.cat/original.php'
46
			),
47
			'Negative year' => array(
48
				'-0005-01-01T00:00:00Z',
49
				0, 0, 0,
50
				TimeValue::PRECISION_SECOND,
51
				'http://nyan.cat/original.php'
52
			),
53
			'No day' => array(
54
				'+2015-01-00T00:00:00Z',
55
				0, 0, 0,
56
				TimeValue::PRECISION_YEAR,
57
				'http://nyan.cat/original.php'
58
			),
59
			'No day and month' => array(
60
				'+2015-00-00T00:00:00Z',
61
				0, 0, 0,
62
				TimeValue::PRECISION_YEAR,
63
				'http://nyan.cat/original.php'
64
			),
65
			'Zero' => array(
66
				'+0000-00-00T00:00:00Z',
67
				0, 0, 0,
68
				TimeValue::PRECISION_SECOND,
69
				'http://nyan.cat/original.php'
70
			),
71
			'Minimum timestamp' => array(
72
				'-9999999999999999-12-31T23:59:61Z',
73
				0, 0, 0,
74
				TimeValue::PRECISION_SECOND,
75
				'http://nyan.cat/original.php'
76
			),
77
			'Maximum timestamp' => array(
78
				'+9999999999999999-12-31T23:59:61Z',
79
				0, 0, 0,
80
				TimeValue::PRECISION_SECOND,
81
				'http://nyan.cat/original.php'
82
			),
83
			'Leap year' => array(
84
				'+2000-02-29T00:00:00Z',
85
				0, 0, 0,
86
				TimeValue::PRECISION_DAY,
87
				'http://nyan.cat/original.php'
88
			),
89
			'Non-leap year 29 February' => array(
90
				'+2015-02-29T00:00:00Z',
91
				0, 0, 0,
92
				TimeValue::PRECISION_DAY,
93
				'http://nyan.cat/original.php'
94
			),
95
			'31 November' => array(
96
				'+2015-11-31T00:00:00Z',
97
				0, 0, 0,
98
				TimeValue::PRECISION_DAY,
99
				'http://nyan.cat/original.php'
100
			),
101
		);
102
	}
103
104
	public function invalidConstructorArgumentsProvider() {
105
		return array(
106
			'String timezone' => array(
107
				'+00000002013-01-01T00:00:00Z',
108
				'0', 0, 0,
109
				TimeValue::PRECISION_SECOND,
110
				'http://nyan.cat/original.php'
111
			),
112
			'Float timezone' => array(
113
				'+00000002013-01-01T00:00:00Z',
114
				4.2, 0, 0,
115
				TimeValue::PRECISION_SECOND,
116
				'http://nyan.cat/original.php'
117
			),
118
			'Timezone out of range' => array(
119
				'+00000002013-01-01T00:00:00Z',
120
				-20 * 3600, 0, 0,
121
				TimeValue::PRECISION_SECOND,
122
				'http://nyan.cat/original.php'
123
			),
124
			'Precision out of range' => array(
125
				'+00000002013-01-01T00:00:00Z',
126
				0, 0, 0,
127
				15,
128
				'http://nyan.cat/original.php'
129
			),
130
			'Integer timestamp' => array(
131
				42,
132
				0, 0, 0,
133
				TimeValue::PRECISION_SECOND,
134
				'http://nyan.cat/original.php'
135
			),
136
			'Float before' => array(
137
				'+00000002013-01-01T00:00:00Z',
138
				0, 4.2, 0,
139
				TimeValue::PRECISION_SECOND,
140
				'http://nyan.cat/original.php'
141
			),
142
			'Negative after' => array(
143
				'+00000002013-01-01T00:00:00Z',
144
				0, 0, -1,
145
				TimeValue::PRECISION_SECOND,
146
				'http://nyan.cat/original.php'
147
			),
148
			'Non-ISO timestamp' => array(
149
				'bla',
150
				0, 0, 0,
151
				TimeValue::PRECISION_SECOND,
152
				'http://nyan.cat/original.php'
153
			),
154
			'Invalid separators' => array(
155
				'+00000002013/01/01 00:00:00',
156
				0, 0, 0,
157
				TimeValue::PRECISION_SECOND,
158
				'http://nyan.cat/original.php'
159
			),
160
			'No month' => array(
161
				'+2015-00-01T00:00:00Z',
162
				0, 0, 0,
163
				TimeValue::PRECISION_DAY,
164
				'http://nyan.cat/original.php'
165
			),
166
			'No day but hour' => array(
167
				'+2015-01-00T01:00:00Z',
168
				0, 0, 0,
169
				TimeValue::PRECISION_DAY,
170
				'http://nyan.cat/original.php'
171
			),
172
			'No day but minute' => array(
173
				'+2015-01-00T00:01:00Z',
174
				0, 0, 0,
175
				TimeValue::PRECISION_DAY,
176
				'http://nyan.cat/original.php'
177
			),
178
			'No day but second' => array(
179
				'+2015-01-00T00:00:01Z',
180
				0, 0, 0,
181
				TimeValue::PRECISION_DAY,
182
				'http://nyan.cat/original.php'
183
			),
184
			'Month out of range' => array(
185
				'+00000002013-13-01T00:00:00Z',
186
				0, 0, 0,
187
				TimeValue::PRECISION_SECOND,
188
				'http://nyan.cat/original.php'
189
			),
190
			'Day out of range' => array(
191
				'+00000002013-01-32T00:00:00Z',
192
				0, 0, 0,
193
				TimeValue::PRECISION_SECOND,
194
				'http://nyan.cat/original.php'
195
			),
196
			'Hour out of range' => array(
197
				'+00000002013-01-01T24:00:00Z',
198
				0, 0, 0,
199
				TimeValue::PRECISION_SECOND,
200
				'http://nyan.cat/original.php'
201
			),
202
			'Minute out of range' => array(
203
				'+00000002013-01-01T00:60:00Z',
204
				0, 0, 0,
205
				TimeValue::PRECISION_SECOND,
206
				'http://nyan.cat/original.php'
207
			),
208
			'Second out of range' => array(
209
				'+00000002013-01-01T00:00:62Z',
210
				0, 0, 0,
211
				TimeValue::PRECISION_SECOND,
212
				'http://nyan.cat/original.php'
213
			),
214
			'Invalid timezone' => array(
215
				'+00000002013-01-01T00:00:00+60',
216
				0, 0, 0,
217
				TimeValue::PRECISION_SECOND,
218
				'http://nyan.cat/original.php'
219
			),
220
			'Year to long' => array(
221
				'+00000000000000001-01-01T00:00:00Z',
222
				0, 0, 0,
223
				TimeValue::PRECISION_DAY,
224
				'http://nyan.cat/original.php'
225
			),
226
		);
227
	}
228
229
	/**
230
	 * @dataProvider invalidConstructorArgumentsProvider
231
	 */
232
	public function testConstructorInvalid( $timestamp, $timezone, $before, $after, $precision, $calendarModel ) {
233
		$this->expectException( IllegalValueException::class );
234
		new TimeValue( $timestamp, $timezone, $before, $after, $precision, $calendarModel );
235
	}
236
237
	/**
238
	 * @dataProvider instanceProvider
239
	 */
240
	public function testGetTime( TimeValue $time, array $arguments ) {
241
		$this->assertEquals( $arguments[0], $time->getTime() );
242
	}
243
244
	/**
245
	 * @dataProvider instanceProvider
246
	 */
247
	public function testGetTimezone( TimeValue $time, array $arguments ) {
248
		$this->assertEquals( $arguments[1], $time->getTimezone() );
249
	}
250
251
	/**
252
	 * @dataProvider instanceProvider
253
	 */
254
	public function testGetBefore( TimeValue $time, array $arguments ) {
255
		$this->assertEquals( $arguments[2], $time->getBefore() );
256
	}
257
258
	/**
259
	 * @dataProvider instanceProvider
260
	 */
261
	public function testGetAfter( TimeValue $time, array $arguments ) {
262
		$this->assertEquals( $arguments[3], $time->getAfter() );
263
	}
264
265
	/**
266
	 * @dataProvider instanceProvider
267
	 */
268
	public function testGetPrecision( TimeValue $time, array $arguments ) {
269
		$this->assertEquals( $arguments[4], $time->getPrecision() );
270
	}
271
272
	/**
273
	 * @dataProvider instanceProvider
274
	 */
275
	public function testGetCalendarModel( TimeValue $time, array $arguments ) {
276
		$this->assertEquals( $arguments[5], $time->getCalendarModel() );
277
	}
278
279
	/**
280
	 * @dataProvider instanceProvider
281
	 */
282
	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...
283
		$this->assertTrue( $time->equals( $time->getValue() ) );
284
	}
285
286
	/**
287
	 * @dataProvider unpaddedYearsProvider
288
	 */
289
	public function testGivenUnpaddedYear_yearIsPadded( $year, $expected ) {
290
		$timeValue = new TimeValue(
291
			$year . '-01-01T00:00:00Z',
292
			0, 0, 0,
293
			TimeValue::PRECISION_DAY,
294
			'Stardate'
295
		);
296
		$this->assertSame( $expected . '-01-01T00:00:00Z', $timeValue->getTime() );
297
	}
298
299
	public function unpaddedYearsProvider() {
300
		return array(
301
			array( '+1', '+0001' ),
302
			array( '-10', '-0010' ),
303
			array( '+2015', '+2015' ),
304
			array( '+02015', '+2015' ),
305
			array( '+00000010000', '+10000' ),
306
			array( '+0000000000000001', '+0001' ),
307
			array( '+9999999999999999', '+9999999999999999' ),
308
		);
309
	}
310
311
}
312