1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueParsers\Test; |
4
|
|
|
|
5
|
|
|
use DataValues\TimeValue; |
6
|
|
|
use ValueParsers\EraParser; |
7
|
|
|
use ValueParsers\ParserOptions; |
8
|
|
|
use ValueParsers\YearTimeParser; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers ValueParsers\YearTimeParser |
12
|
|
|
* |
13
|
|
|
* @group DataValue |
14
|
|
|
* @group DataValueExtensions |
15
|
|
|
* @group TimeParsers |
16
|
|
|
* @group ValueParsers |
17
|
|
|
* |
18
|
|
|
* @license GPL-2.0+ |
19
|
|
|
* @author Addshore |
20
|
|
|
* @author Thiemo Mättig |
21
|
|
|
*/ |
22
|
|
|
class YearTimeParserTest extends StringValueParserTest { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @deprecated since 0.3, just use getInstance. |
26
|
|
|
*/ |
27
|
|
|
protected function getParserClass() { |
28
|
|
|
throw new \LogicException( 'Should not be called, use getInstance' ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @see ValueParserTestBase::getInstance |
33
|
|
|
* |
34
|
|
|
* @return YearTimeParser |
35
|
|
|
*/ |
36
|
|
|
protected function getInstance() { |
37
|
|
|
return new YearTimeParser( $this->getMockEraParser() ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return EraParser |
42
|
|
|
*/ |
43
|
|
|
private function getMockEraParser() { |
44
|
|
|
$mock = $this->getMockBuilder( 'ValueParsers\EraParser' ) |
45
|
|
|
->disableOriginalConstructor() |
46
|
|
|
->getMock(); |
47
|
|
|
$mock->expects( $this->any() ) |
48
|
|
|
->method( 'parse' ) |
49
|
|
|
->with( $this->isType( 'string' ) ) |
50
|
|
|
->will( $this->returnCallback( |
51
|
|
|
function( $value ) { |
52
|
|
|
$sign = '+'; |
53
|
|
|
// Tiny parser that supports a single negative sign only |
54
|
|
|
if ( $value[0] === '-' ) { |
55
|
|
|
$sign = '-'; |
56
|
|
|
$value = substr( $value, 1 ); |
57
|
|
|
} |
58
|
|
|
return array( $sign, $value ); |
59
|
|
|
} |
60
|
|
|
) ); |
61
|
|
|
return $mock; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @see ValueParserTestBase::validInputProvider |
66
|
|
|
*/ |
67
|
|
|
public function validInputProvider() { |
68
|
|
|
$gregorian = 'http://www.wikidata.org/entity/Q1985727'; |
69
|
|
|
$julian = 'http://www.wikidata.org/entity/Q1985786'; |
70
|
|
|
|
71
|
|
|
$argLists = array(); |
72
|
|
|
|
73
|
|
|
$valid = array( |
74
|
|
|
'1999' => |
75
|
|
|
array( '+1999-00-00T00:00:00Z' ), |
76
|
|
|
'2000' => |
77
|
|
|
array( '+2000-00-00T00:00:00Z' ), |
78
|
|
|
'2010' => |
79
|
|
|
array( '+2010-00-00T00:00:00Z' ), |
80
|
|
|
'2000000' => |
81
|
|
|
array( '+2000000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR1M ), |
82
|
|
|
'2000000000' => |
83
|
|
|
array( '+2000000000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR1G ), |
84
|
|
|
'2000020000' => |
85
|
|
|
array( '+2000020000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR10K ), |
86
|
|
|
'2000001' => |
87
|
|
|
array( '+2000001-00-00T00:00:00Z' ), |
88
|
|
|
'02000001' => |
89
|
|
|
array( '+2000001-00-00T00:00:00Z' ), |
90
|
|
|
'1' => |
91
|
|
|
array( '+0001-00-00T00:00:00Z', TimeValue::PRECISION_YEAR, $julian ), |
92
|
|
|
'000000001' => |
93
|
|
|
array( '+0001-00-00T00:00:00Z', TimeValue::PRECISION_YEAR, $julian ), |
94
|
|
|
'-1000000' => |
95
|
|
|
array( '-1000000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR1M, $julian ), |
96
|
|
|
'-1 000 000' => |
97
|
|
|
array( '-1000000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR1M, $julian ), |
98
|
|
|
'-19_000' => |
99
|
|
|
array( '-19000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR1K, $julian ), |
100
|
|
|
// Digit grouping in the Indian numbering system |
101
|
|
|
'-1,99,999' => |
102
|
|
|
array( '-199999-00-00T00:00:00Z', TimeValue::PRECISION_YEAR, $julian ), |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
foreach ( $valid as $value => $expected ) { |
106
|
|
|
$timestamp = $expected[0]; |
107
|
|
|
$precision = isset( $expected[1] ) ? $expected[1] : TimeValue::PRECISION_YEAR; |
108
|
|
|
$calendarModel = isset( $expected[2] ) ? $expected[2] : $gregorian; |
109
|
|
|
|
110
|
|
|
$argLists[] = array( |
111
|
|
|
(string)$value, |
112
|
|
|
new TimeValue( $timestamp, 0, 0, 0, $precision, $calendarModel ) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $argLists; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testDigitGroupSeparatorOption() { |
120
|
|
|
$options = new ParserOptions(); |
121
|
|
|
$options->setOption( YearTimeParser::OPT_DIGIT_GROUP_SEPARATOR, '.' ); |
122
|
|
|
$parser = new YearTimeParser( null, $options ); |
123
|
|
|
$timeValue = $parser->parse( '-19.000' ); |
124
|
|
|
$this->assertSame( '-19000-00-00T00:00:00Z', $timeValue->getTime() ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @see StringValueParserTest::invalidInputProvider |
129
|
|
|
*/ |
130
|
|
|
public function invalidInputProvider() { |
131
|
|
|
$argLists = parent::invalidInputProvider(); |
132
|
|
|
|
133
|
|
|
$invalid = array( |
134
|
|
|
//These are just wrong! |
135
|
|
|
'June June June', |
136
|
|
|
'111 111 111', |
137
|
|
|
'Jann 2014', |
138
|
|
|
|
139
|
|
|
//Not within the scope of this parser |
140
|
|
|
'1 July 20000', |
141
|
|
|
|
142
|
|
|
//We should not try to parse these, this just gets confusing |
143
|
|
|
'-100BC', |
144
|
|
|
'+100BC', |
145
|
|
|
'-100 BC', |
146
|
|
|
'+100 BC', |
147
|
|
|
'+100 BCE', |
148
|
|
|
'+100BCE', |
149
|
|
|
|
150
|
|
|
// Non-default and invalid thousands separators |
151
|
|
|
'-,999', |
152
|
|
|
'-999,', |
153
|
|
|
'-19.000', |
154
|
|
|
'-1/000/000', |
155
|
|
|
|
156
|
|
|
// Positive years are unlikely to have thousands separators, it's more likely a date |
157
|
|
|
'1 000 000', |
158
|
|
|
'19_000', |
159
|
|
|
'1,99,999', |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
foreach ( $invalid as $value ) { |
163
|
|
|
$argLists[] = array( $value ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $argLists; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @expectedException \ValueParsers\ParseException |
171
|
|
|
* @expectedExceptionMessage Failed to parse year |
172
|
|
|
*/ |
173
|
|
|
public function testParseExceptionMessage() { |
174
|
|
|
$parser = $this->getInstance(); |
175
|
|
|
$parser->parse( 'ju5t 1nval1d' ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|