Passed
Push — ymdMonthNames ( 4f0b3e...7ee93b )
by no
08:06
created

YearTimeParserTest::validInputProvider()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 53
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 53
rs 8.9849
cc 4
eloc 41
nc 5
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	 * @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() {
133
		$argLists = parent::invalidInputProvider();
134
135
		$invalid = array(
136
			// These are just wrong
137
			'June June June',
138
			'111 111 111',
139
			'Jann 2014',
140
141
			// Not within the scope of this parser
142
			'1 July 20000',
143
144
			// We should not try to parse these, this just gets confusing
145
			'-100BC',
146
			'+100BC',
147
			'-100 BC',
148
			'+100 BC',
149
			'+100 BCE',
150
			'+100BCE',
151
152
			// Non-default and invalid thousands separators
153
			'-,999',
154
			'-999,',
155
			'-19.000',
156
			'-1/000/000',
157
158
			// Positive years are unlikely to have thousands separators, it's more likely a date
159
			'1 000 000',
160
			'19_000',
161
			'1,99,999',
162
		);
163
164
		foreach ( $invalid as $value ) {
165
			$argLists[] = array( $value );
166
		}
167
168
		return $argLists;
169
	}
170
171
	/**
172
	 * @expectedException \ValueParsers\ParseException
173
	 * @expectedExceptionMessage Failed to parse year
174
	 */
175
	public function testParseExceptionMessage() {
176
		$parser = $this->getInstance();
177
		$parser->parse( 'ju5t 1nval1d' );
178
	}
179
180
}
181