1 | <?php |
||
22 | class YearTimeParserTest extends StringValueParserTest { |
||
23 | |||
24 | /** |
||
25 | * @deprecated since DataValues Common 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( EraParser::class ) |
||
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 | // Whitespace |
||
75 | "1999\n" => |
||
76 | array( '+1999-00-00T00:00:00Z' ), |
||
77 | ' 2000 ' => |
||
78 | array( '+2000-00-00T00:00:00Z' ), |
||
79 | |||
80 | '2010' => |
||
81 | array( '+2010-00-00T00:00:00Z' ), |
||
82 | '2000000' => |
||
83 | array( '+2000000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR1M ), |
||
84 | '2000000000' => |
||
85 | array( '+2000000000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR1G ), |
||
86 | '2000020000' => |
||
87 | array( '+2000020000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR10K ), |
||
88 | '2000001' => |
||
89 | array( '+2000001-00-00T00:00:00Z' ), |
||
90 | '02000001' => |
||
91 | array( '+2000001-00-00T00:00:00Z' ), |
||
92 | '1' => |
||
93 | array( '+0001-00-00T00:00:00Z', TimeValue::PRECISION_YEAR, $julian ), |
||
94 | '000000001' => |
||
95 | array( '+0001-00-00T00:00:00Z', TimeValue::PRECISION_YEAR, $julian ), |
||
96 | '-1000000' => |
||
97 | array( '-1000000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR1M, $julian ), |
||
98 | '-1 000 000' => |
||
99 | array( '-1000000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR1M, $julian ), |
||
100 | '-19_000' => |
||
101 | array( '-19000-00-00T00:00:00Z', TimeValue::PRECISION_YEAR1K, $julian ), |
||
102 | // Digit grouping in the Indian numbering system |
||
103 | '-1,99,999' => |
||
104 | array( '-199999-00-00T00:00:00Z', TimeValue::PRECISION_YEAR, $julian ), |
||
105 | ); |
||
106 | |||
107 | foreach ( $valid as $value => $expected ) { |
||
108 | $timestamp = $expected[0]; |
||
109 | $precision = isset( $expected[1] ) ? $expected[1] : TimeValue::PRECISION_YEAR; |
||
110 | $calendarModel = isset( $expected[2] ) ? $expected[2] : $gregorian; |
||
111 | |||
112 | $argLists[] = array( |
||
113 | (string)$value, |
||
114 | new TimeValue( $timestamp, 0, 0, 0, $precision, $calendarModel ) |
||
115 | ); |
||
116 | } |
||
117 | |||
118 | return $argLists; |
||
119 | } |
||
120 | |||
121 | public function testDigitGroupSeparatorOption() { |
||
122 | $options = new ParserOptions(); |
||
123 | $options->setOption( YearTimeParser::OPT_DIGIT_GROUP_SEPARATOR, '.' ); |
||
124 | $parser = new YearTimeParser( null, $options ); |
||
125 | $timeValue = $parser->parse( '-19.000' ); |
||
126 | $this->assertSame( '-19000-00-00T00:00:00Z', $timeValue->getTime() ); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @see StringValueParserTest::invalidInputProvider |
||
131 | */ |
||
132 | public function invalidInputProvider() { |
||
170 | |||
171 | /** |
||
172 | * @expectedException \ValueParsers\ParseException |
||
173 | * @expectedExceptionMessage Failed to parse year |
||
174 | */ |
||
175 | public function testParseExceptionMessage() { |
||
179 | |||
180 | } |
||
181 |