1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueFormatters\Test; |
4
|
|
|
|
5
|
|
|
use DataValues\TimeValue; |
6
|
|
|
use ValueFormatters\FormatterOptions; |
7
|
|
|
use ValueFormatters\TimeFormatter; |
8
|
|
|
use ValueFormatters\ValueFormatter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers ValueFormatters\TimeFormatter |
12
|
|
|
* |
13
|
|
|
* @group ValueFormatters |
14
|
|
|
* @group DataValueExtensions |
15
|
|
|
* |
16
|
|
|
* @license GPL-2.0+ |
17
|
|
|
* @author H. Snater < [email protected] > |
18
|
|
|
* @author Thiemo Mättig |
19
|
|
|
*/ |
20
|
|
|
class TimeFormatterTest extends ValueFormatterTestBase { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @deprecated since DataValues Interfaces 0.2, just use getInstance. |
24
|
|
|
*/ |
25
|
|
|
protected function getFormatterClass() { |
26
|
|
|
throw new \LogicException( 'Should not be called, use getInstance' ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @see ValueFormatterTestBase::getInstance |
31
|
|
|
* |
32
|
|
|
* @param FormatterOptions|null $options |
33
|
|
|
* |
34
|
|
|
* @return TimeFormatter |
35
|
|
|
*/ |
36
|
|
|
protected function getInstance( FormatterOptions $options = null ) { |
37
|
|
|
return new TimeFormatter( $options ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return ValueFormatter |
42
|
|
|
*/ |
43
|
|
|
private function getTimestampFormatter() { |
44
|
|
|
$mock = $this->getMock( 'ValueFormatters\ValueFormatter' ); |
45
|
|
|
$mock->expects( $this->any() ) |
46
|
|
|
->method( 'format' ) |
47
|
|
|
->will( $this->returnValue( '<timestamp>' ) ); |
48
|
|
|
|
49
|
|
|
return $mock; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @see ValueFormatterTestBase::validProvider |
54
|
|
|
*/ |
55
|
|
|
public function validProvider() { |
56
|
|
|
$gregorian = 'http://www.wikidata.org/entity/Q1985727'; |
57
|
|
|
$julian = 'http://www.wikidata.org/entity/Q1985786'; |
58
|
|
|
|
59
|
|
|
$baseOptions = new FormatterOptions(); |
60
|
|
|
$baseOptions->setOption( TimeFormatter::OPT_CALENDARNAMES, array( |
61
|
|
|
$gregorian => '<Gregorian>', |
62
|
|
|
$julian => '<Julian>', |
63
|
|
|
) ); |
64
|
|
|
|
65
|
|
|
$timestampFormatterOptions = new FormatterOptions(); |
66
|
|
|
$timestampFormatterOptions->setOption( |
67
|
|
|
TimeFormatter::OPT_TIME_ISO_FORMATTER, |
68
|
|
|
$this->getTimestampFormatter() |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$tests = array( |
72
|
|
|
'2013-07-16' => array( |
73
|
|
|
'+2013-07-16T00:00:00Z', |
74
|
|
|
), |
75
|
|
|
|
76
|
|
|
// Custom timestamp formatter |
77
|
|
|
'<timestamp>' => array( |
78
|
|
|
'+2013-07-16T00:00:00Z', |
79
|
|
|
TimeValue::PRECISION_DAY, |
80
|
|
|
$gregorian, |
81
|
|
|
$timestampFormatterOptions, |
82
|
|
|
), |
83
|
|
|
|
84
|
|
|
// Different calendar models |
85
|
|
|
'1701-12-14' => array( |
86
|
|
|
'+1701-12-14T00:00:00Z', |
87
|
|
|
TimeValue::PRECISION_DAY, |
88
|
|
|
$julian, |
89
|
|
|
), |
90
|
|
|
'1702-12-14' => array( |
91
|
|
|
'+1702-12-14T00:00:00Z', |
92
|
|
|
TimeValue::PRECISION_DAY, |
93
|
|
|
'Stardate', |
94
|
|
|
), |
95
|
|
|
|
96
|
|
|
// Different years |
97
|
|
|
"\xE2\x88\x9210000-01-01" => array( |
98
|
|
|
'-010000-01-01T00:00:00Z', |
99
|
|
|
), |
100
|
|
|
"\xE2\x88\x920001-01-01" => array( |
101
|
|
|
'-1-01-01T00:00:00Z', |
102
|
|
|
), |
103
|
|
|
"\xE2\x88\x920100-01-01" => array( |
104
|
|
|
'-100-01-01T00:00:00Z', |
105
|
|
|
), |
106
|
|
|
"\xE2\x88\x920000-01-01" => array( |
107
|
|
|
'-0-01-01T00:00:00Z', |
108
|
|
|
), |
109
|
|
|
'0000-01-01' => array( |
110
|
|
|
'+0-01-01T00:00:00Z', |
111
|
|
|
), |
112
|
|
|
'0001-01-01' => array( |
113
|
|
|
'+1-01-01T00:00:00Z', |
114
|
|
|
), |
115
|
|
|
'0100-01-01' => array( |
116
|
|
|
'+100-01-01T00:00:00Z', |
117
|
|
|
), |
118
|
|
|
'10000-01-01' => array( |
119
|
|
|
'+010000-01-01T00:00:00Z', |
120
|
|
|
), |
121
|
|
|
|
122
|
|
|
// Different precisions |
123
|
|
|
'2000' => array( |
124
|
|
|
'+2000-01-01T00:00:00Z', |
125
|
|
|
TimeValue::PRECISION_YEAR1G, |
126
|
|
|
), |
127
|
|
|
'2008' => array( |
128
|
|
|
'+2008-01-08T00:00:00Z', |
129
|
|
|
TimeValue::PRECISION_YEAR10, |
130
|
|
|
), |
131
|
|
|
'2009' => array( |
132
|
|
|
'+2009-01-09T00:00:00Z', |
133
|
|
|
TimeValue::PRECISION_YEAR, |
134
|
|
|
), |
135
|
|
|
'2010-07' => array( |
136
|
|
|
'+2010-07-10T00:00:00Z', |
137
|
|
|
TimeValue::PRECISION_MONTH, |
138
|
|
|
), |
139
|
|
|
'2011-07-11' => array( |
140
|
|
|
'+2011-07-11T00:00:00Z', |
141
|
|
|
TimeValue::PRECISION_DAY, |
142
|
|
|
), |
143
|
|
|
'2012-07-12T00' => array( |
144
|
|
|
'+2012-07-12T00:00:00Z', |
145
|
|
|
TimeValue::PRECISION_HOUR, |
146
|
|
|
), |
147
|
|
|
'2013-07-13T00:00' => array( |
148
|
|
|
'+2013-07-13T00:00:00Z', |
149
|
|
|
TimeValue::PRECISION_MINUTE, |
150
|
|
|
), |
151
|
|
|
'2014-07-14T00:00:00' => array( |
152
|
|
|
'+2014-07-14T00:00:00Z', |
153
|
|
|
TimeValue::PRECISION_SECOND, |
154
|
|
|
), |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$argLists = array(); |
158
|
|
|
|
159
|
|
|
foreach ( $tests as $expected => $args ) { |
160
|
|
|
$timestamp = $args[0]; |
161
|
|
|
$precision = isset( $args[1] ) ? $args[1] : TimeValue::PRECISION_DAY; |
162
|
|
|
$calendarModel = isset( $args[2] ) ? $args[2] : $gregorian; |
163
|
|
|
$options = isset( $args[3] ) ? $args[3] : $baseOptions; |
164
|
|
|
|
165
|
|
|
$argLists[] = array( |
166
|
|
|
new TimeValue( $timestamp, 0, 0, 0, $precision, $calendarModel ), |
|
|
|
|
167
|
|
|
(string)$expected, |
168
|
|
|
$options |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $argLists; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.