1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueParsers\Test; |
4
|
|
|
|
5
|
|
|
use DataValues\TimeValue; |
6
|
|
|
use ValueParsers\MonthNameProvider; |
7
|
|
|
use ValueParsers\YearMonthTimeParser; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers ValueParsers\YearMonthTimeParser |
11
|
|
|
* |
12
|
|
|
* @group DataValue |
13
|
|
|
* @group DataValueExtensions |
14
|
|
|
* @group TimeParsers |
15
|
|
|
* @group ValueParsers |
16
|
|
|
* |
17
|
|
|
* @license GPL-2.0+ |
18
|
|
|
* @author Addshore |
19
|
|
|
* @author Thiemo Mättig |
20
|
|
|
*/ |
21
|
|
|
class YearMonthTimeParserTest extends StringValueParserTest { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @deprecated since 0.3, just use getInstance. |
25
|
|
|
*/ |
26
|
|
|
protected function getParserClass() { |
27
|
|
|
throw new \LogicException( 'Should not be called, use getInstance' ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @see ValueParserTestBase::getInstance |
32
|
|
|
* |
33
|
|
|
* @return YearMonthTimeParser |
34
|
|
|
*/ |
35
|
|
|
protected function getInstance() { |
36
|
|
|
$monthNameProvider = $this->getMockBuilder( 'ValueParsers\MonthNameProvider' ) |
37
|
|
|
->disableOriginalConstructor() |
38
|
|
|
->getMock(); |
39
|
|
|
$monthNameProvider->expects( $this->once() ) |
40
|
|
|
->method( 'getMonthNumbers' ) |
41
|
|
|
->with( 'en' ) |
42
|
|
|
->will( $this->returnValue( array( |
43
|
|
|
'January' => 1, |
44
|
|
|
'Jan' => 1, |
45
|
|
|
'April' => 4, |
46
|
|
|
) ) ); |
47
|
|
|
|
48
|
|
|
return new YearMonthTimeParser( $monthNameProvider ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @see ValueParserTestBase::validInputProvider |
53
|
|
|
*/ |
54
|
|
|
public function validInputProvider() { |
55
|
|
|
$gregorian = 'http://www.wikidata.org/entity/Q1985727'; |
56
|
|
|
$julian = 'http://www.wikidata.org/entity/Q1985786'; |
57
|
|
|
|
58
|
|
|
$argLists = array(); |
59
|
|
|
|
60
|
|
|
$valid = array( |
61
|
|
|
// leading zeros |
62
|
|
|
'00001 1999' => |
63
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
64
|
|
|
'000000001 100001999' => |
65
|
|
|
array( '+100001999-01-00T00:00:00Z' ), |
66
|
|
|
|
67
|
|
|
// use string month names |
68
|
|
|
'Jan/1999' => |
69
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
70
|
|
|
'January/1999' => |
71
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
72
|
|
|
'January/1' => |
73
|
|
|
array( '+0001-01-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ), |
74
|
|
|
'1999 January' => |
75
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
76
|
|
|
'January 1999' => |
77
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
78
|
|
|
'January-1' => |
79
|
|
|
array( '+0001-01-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ), |
80
|
|
|
'JanuARY-1' => |
81
|
|
|
array( '+0001-01-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ), |
82
|
|
|
'JaN/1999' => |
83
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
84
|
|
|
'januARY-1' => |
85
|
|
|
array( '+0001-01-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ), |
86
|
|
|
'jan/1999' => |
87
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
88
|
|
|
|
89
|
|
|
// use different date separators |
90
|
|
|
'1-1999' => |
91
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
92
|
|
|
'1/1999' => |
93
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
94
|
|
|
'1 / 1999' => |
95
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
96
|
|
|
'1 1999' => |
97
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
98
|
|
|
'1,1999' => |
99
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
100
|
|
|
'1.1999' => |
101
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
102
|
|
|
'1. 1999' => |
103
|
|
|
array( '+1999-01-00T00:00:00Z' ), |
104
|
|
|
|
105
|
|
|
// presume mm/yy unless impossible month, in which case switch |
106
|
|
|
'12/12' => |
107
|
|
|
array( '+0012-12-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ), |
108
|
|
|
'12/11' => |
109
|
|
|
array( '+0011-12-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ), |
110
|
|
|
'11/12' => |
111
|
|
|
array( '+0012-11-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ), |
112
|
|
|
'13/12' => |
113
|
|
|
array( '+0013-12-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ), |
114
|
|
|
'12/13' => |
115
|
|
|
array( '+0013-12-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ), |
116
|
|
|
'2000 1' => |
117
|
|
|
array( '+2000-01-00T00:00:00Z' ), |
118
|
|
|
|
119
|
|
|
// big years |
120
|
|
|
'April-1000000001' => |
121
|
|
|
array( '+1000000001-04-00T00:00:00Z' ), |
122
|
|
|
'April 1000000001' => |
123
|
|
|
array( '+1000000001-04-00T00:00:00Z' ), |
124
|
|
|
'1000000001 April' => |
125
|
|
|
array( '+1000000001-04-00T00:00:00Z' ), |
126
|
|
|
'1 13000' => |
127
|
|
|
array( '+13000-01-00T00:00:00Z' ), |
128
|
|
|
|
129
|
|
|
// parse 0 month as if no month has been entered |
130
|
|
|
'0.1999' => |
131
|
|
|
array( '+1999-00-00T00:00:00Z', TimeValue::PRECISION_YEAR ), |
132
|
|
|
'1999 0' => |
133
|
|
|
array( '+1999-00-00T00:00:00Z', TimeValue::PRECISION_YEAR ), |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
foreach ( $valid as $value => $expected ) { |
137
|
|
|
$timestamp = $expected[0]; |
138
|
|
|
$precision = isset( $expected[1] ) ? $expected[1] : TimeValue::PRECISION_MONTH; |
139
|
|
|
$calendarModel = isset( $expected[2] ) ? $expected[2] : $gregorian; |
140
|
|
|
|
141
|
|
|
$argLists[] = array( |
142
|
|
|
(string)$value, |
143
|
|
|
new TimeValue( $timestamp, 0, 0, 0, $precision, $calendarModel ) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $argLists; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @see StringValueParserTest::invalidInputProvider |
152
|
|
|
*/ |
153
|
|
|
public function invalidInputProvider() { |
154
|
|
|
$argLists = parent::invalidInputProvider(); |
155
|
|
|
|
156
|
|
|
$invalid = array( |
157
|
|
|
//These are just wrong! |
158
|
|
|
'June June June', |
159
|
|
|
'111 111 111', |
160
|
|
|
'Jann 2014', |
161
|
|
|
'13/13', |
162
|
|
|
'13,1999', |
163
|
|
|
'1999,13', |
164
|
|
|
|
165
|
|
|
//Dont parse stuff with separators in the year |
166
|
|
|
'june 200,000,000', |
167
|
|
|
'june 200.000.000', |
168
|
|
|
|
169
|
|
|
//Not within the scope of this parser |
170
|
|
|
'1 July 20000', |
171
|
|
|
'20000', |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
foreach ( $invalid as $value ) { |
175
|
|
|
$argLists[] = array( $value ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $argLists; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|