TimeValueCalculatorTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 196
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A getTimeValueMock() 0 20 1
A timestampProvider() 0 53 1
A testGetTimestamp() 0 6 1
A yearProvider() 0 49 1
A testIsLeapYear() 0 5 1
A testGetNumberOfLeapYears() 0 5 1
A precisionProvider() 0 15 1
A testGetSecondsForPrecision() 0 5 1
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\TimeValue;
6
use DataValues\TimeValueCalculator;
7
use PHPUnit\Framework\TestCase;
8
9
/**
10
 * @covers DataValues\TimeValueCalculator
11
 *
12
 * @group DataValue
13
 * @group DataValueExtensions
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Thiemo Kreuz
17
 */
18
class TimeValueCalculatorTest extends TestCase {
19
20
	/**
21
	 * @var TimeValueCalculator
22
	 */
23
	private $calculator;
24
25
	protected function setUp() : void {
26
		$this->calculator = new TimeValueCalculator();
27
	}
28
29
	/**
30
	 * @param string $time an ISO 8601 date and time
31
	 * @param int $timezone offset from UTC in minutes
32
	 *
33
	 * @return TimeValue
34
	 */
35
	private function getTimeValueMock( $time, $timezone = 0 ) {
36
		$timeValue = $this->getMockBuilder( TimeValue::class )
37
			->disableOriginalConstructor()
38
			->getMock();
39
40
		$timeValue->expects( $this->any() )
41
			->method( 'getTime' )
42
			->will( $this->returnValue( $time ) );
43
		$timeValue->expects( $this->any() )
44
			->method( 'getTimezone' )
45
			->will( $this->returnValue( $timezone ) );
46
		$timeValue->expects( $this->any() )
47
			->method( 'getPrecision' )
48
			->will( $this->returnValue( TimeValue::PRECISION_DAY ) );
49
		$timeValue->expects( $this->any() )
50
			->method( 'getCalendarModel' )
51
			->will( $this->returnValue( 'Stardate' ) );
52
53
		return $timeValue;
54
	}
55
56
	public function timestampProvider() {
57
		return array(
58
			// Make sure it's identical to the PHP/Unix time stamps in current years
59
			array( '+2004-02-29T00:00:00Z', strtotime( '2004-02-29T00:00:00+00:00' ) ),
60
			array( '+2038-00-00T00:00:00Z', strtotime( '2038-01-01T00:00:00+00:00' ) ),
61
62
			// Time zones
63
			array( '+2000-01-01T12:59:59', strtotime( '2000-01-01T12:59:59-02:00' ), -120 ),
64
			array( '+2000-01-01T12:59:59', strtotime( '2000-01-01T12:59:59+04:45' ), 285 ),
65
66
			array( '+0401-00-00T00:00:00Z', -49512816000 ),
67
			array( '+1902-00-00T00:00:00Z', -2145916800 ),
68
			array( '+1939-00-00T00:00:00Z', -978307200 ),
69
			array( '+1969-12-31T23:59:00Z', -60 ),
70
			array( '+1969-12-31T23:59:59Z', -1 ),
71
			array( '+1970-01-01T00:00:00Z', 0 ),
72
			array( '+1970-01-01T00:00:01Z', 1 ),
73
			array( '+1970-01-01T00:01:00Z', 60 ),
74
			array( '+1972-02-29T00:00:00Z', 68169600 ),
75
			array( '+1996-02-29T00:00:00Z', 825552000 ),
76
			array( '+1999-12-31T23:59:59Z', 946684799 ),
77
			array( '+2000-01-01T00:00:00Z', 946684800 ),
78
			array( '+2000-02-01T00:00:00Z', 949363200 ),
79
			array( '+2000-02-29T00:00:00Z', 951782400 ),
80
			array( '+2001-00-00T00:00:00Z', 978307200 ),
81
			array( '+2001-01-01T00:00:00Z', 978307200 ),
82
			array( '+2014-04-30T12:35:55Z', 1398861355 ),
83
			array( '+2401-00-00T00:00:00Z', 13601088000 ),
84
85
			// Make sure there is only 1 second between these two
86
			array( '-0001-12-31T23:59:59Z', -62135596801 ),
87
			array( '+0001-00-00T00:00:00Z', -62135596800 ),
88
89
			// No special calculation for leap seconds, just make sure they pass
90
			array( '+1970-10-11T12:13:61Z', 24495241 ),
91
			array( '+1970-10-11T12:14:01Z', 24495241 ),
92
93
			// Year 0 does not exist, but we do not complain, assume -1
94
			array( '-0000-12-31T23:59:59Z', -62135596801 ),
95
			array( '+0000-00-00T00:00:00Z', floor( ( -1 - 1969 ) * 365.2425 ) * 86400 ),
96
97
			// Since there is no year 0, negative leap years are -1, -5 and so on
98
			array( '-8001-00-00T00:00:00Z', floor( ( -8001 - 1969 ) * 365.2425 ) * 86400 ),
99
			array( '-0005-00-00T00:00:00Z', floor( ( -5 - 1969 ) * 365.2425 ) * 86400 ),
100
			array( '+0004-00-00T00:00:00Z', floor( ( 4 - 1970 ) * 365.2425 ) * 86400 ),
101
			array( '+8000-00-00T00:00:00Z', floor( ( 8000 - 1970 ) * 365.2425 ) * 86400 ),
102
103
			// PHP_INT_MIN is -2147483648
104
			array( '-2147484001-00-00T00:00:00Z', floor( ( -2147484001 - 1969 ) * 365.2425 ) * 86400 ),
105
			// PHP_INT_MAX is +2147483647
106
			array( '+2147484000-00-00T00:00:00Z', floor( ( 2147484000 - 1970 ) * 365.2425 ) * 86400 ),
107
		);
108
	}
109
110
	/**
111
	 * @dataProvider timestampProvider
112
	 */
113
	public function testGetTimestamp( $time, $expectedTimestamp = 0.0, $timezone = 0 ) {
114
		$timeValue = $this->getTimeValueMock( $time, $timezone );
115
		$timestamp = $this->calculator->getTimestamp( $timeValue );
0 ignored issues
show
Documentation introduced by
$timeValue is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<DataValues\TimeValue>.

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...
116
117
		$this->assertEquals( $expectedTimestamp, $timestamp );
118
	}
119
120
	public function yearProvider() {
121
		return array(
122
			// Every 4 years
123
			array( 1895, 459 ),
124
			array( 1896, 460, true ),
125
			array( 1897, 460 ),
126
127
			// Not every 100 years but every 400 years
128
			array( 1900, 460 ),
129
			array( 2000, 485, true ),
130
			array( 2100, 509 ),
131
132
			// Extremes
133
			array( 1, 0 ),
134
			array( 9999, 2424 ),
135
			array( 2147483647, 520764784 ),
136
137
			// There is no year zero, assume -1
138
			array( -1, 0, true ),
139
			array( 0, 0, true ),
140
141
			// Off by 1 for negative years because zero is skipped
142
			array( -6, -2 ),
143
			array( -5, -1, true ),
144
			array( -4, -1 ),
145
			array( -3, -1 ),
146
			array( -2, -1 ),
147
			array( -1, 0, true ),
148
			array( 1, 0 ),
149
			array( 2, 0 ),
150
			array( 3, 0 ),
151
			array( 4, 1, true ),
152
			array( 5, 1 ),
153
154
			// Because we can
155
			array( -6.9, -2 ),
156
			array( -6.1, -2 ),
157
			array( -5.501, -1, true ),
158
			array( -5.499, -1, true ),
159
			array( -4.6, -1 ),
160
			array( -4.4, -1 ),
161
			array( 1995.01, 483 ),
162
			array( 1995.09, 483 ),
163
			array( 1996.001, 484, true ),
164
			array( 1996.009, 484, true ),
165
			array( 1997.1, 484 ),
166
			array( 1997.9, 484 ),
167
		);
168
	}
169
170
	/**
171
	 * @dataProvider yearProvider
172
	 */
173
	public function testIsLeapYear( $year, $numberOfLeapYears, $expected = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $numberOfLeapYears 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...
174
		$isLeapYear = $this->calculator->isLeapYear( $year );
175
176
		$this->assertEquals( $expected, $isLeapYear );
177
	}
178
179
	/**
180
	 * @dataProvider yearProvider
181
	 */
182
	public function testGetNumberOfLeapYears( $year, $expected, $isLeapYear = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $isLeapYear 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...
183
		$numberOfLeapYears = $this->calculator->getNumberOfLeapYears( $year );
184
185
		$this->assertEquals( $expected, $numberOfLeapYears );
186
	}
187
188
	public function precisionProvider() {
189
		$secondsPerDay = 60 * 60 * 24;
190
		$daysPerGregorianYear = 365 + 1 / 4 - 1 / 100 + 1 / 400;
191
192
		return array(
193
			array( TimeValue::PRECISION_SECOND, 1 ),
194
			array( TimeValue::PRECISION_MINUTE, 60 ),
195
			array( TimeValue::PRECISION_HOUR, 60 * 60 ),
196
			array( TimeValue::PRECISION_DAY, $secondsPerDay ),
197
			array( TimeValue::PRECISION_MONTH, $secondsPerDay * $daysPerGregorianYear / 12 ),
198
			array( TimeValue::PRECISION_YEAR, $secondsPerDay * $daysPerGregorianYear ),
199
			array( TimeValue::PRECISION_YEAR10, $secondsPerDay * $daysPerGregorianYear * 1e1 ),
200
			array( TimeValue::PRECISION_YEAR1G, $secondsPerDay * $daysPerGregorianYear * 1e9 ),
201
		);
202
	}
203
204
	/**
205
	 * @dataProvider precisionProvider
206
	 */
207
	public function testGetSecondsForPrecision( $precision, $expected ) {
208
		$seconds = $this->calculator->getSecondsForPrecision( $precision );
209
210
		$this->assertEquals( $expected, $seconds );
211
	}
212
213
}
214