Passed
Push — master ( 7af698...31dd3f )
by Jeroen De
01:04 queued 34s
created

YearTimeParserTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 152
rs 10
c 0
b 0
f 0

6 Methods

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