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

YearMonthTimeParserTest::validInputProvider()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 111
Code Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 111
rs 8.1935
cc 4
eloc 87
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\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 Kreuz
20
 */
21
class YearMonthTimeParserTest extends StringValueParserTest {
22
23
	/**
24
	 * @deprecated since DataValues Common 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( MonthNameProvider::class )
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
				'June' => 6,
47
			) ) );
48
49
		return new YearMonthTimeParser( $monthNameProvider );
50
	}
51
52
	/**
53
	 * @see ValueParserTestBase::validInputProvider
54
	 */
55
	public function validInputProvider() {
56
		$gregorian = 'http://www.wikidata.org/entity/Q1985727';
57
		$julian = 'http://www.wikidata.org/entity/Q1985786';
58
59
		$argLists = array();
60
61
		$valid = array(
62
			// Whitespace
63
			"January 2016\n" =>
64
				array( '+2016-01-00T00:00:00Z' ),
65
			' January 2016 ' =>
66
				array( '+2016-01-00T00:00:00Z' ),
67
68
			// leading zeros
69
			'1 00001999' =>
70
				array( '+1999-01-00T00:00:00Z' ),
71
			'1 0000000100001999' =>
72
				array( '+100001999-01-00T00:00:00Z' ),
73
74
			// Negative years
75
			'4 -1998' =>
76
				array( '-1998-04-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
77
			'April -1998' =>
78
				array( '-1998-04-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
79
			'-1998 4' =>
80
				array( '-1998-04-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
81
			'-1998 April' =>
82
				array( '-1998-04-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
83
84
			// use string month names
85
			'Jan/1999' =>
86
				array( '+1999-01-00T00:00:00Z' ),
87
			'January/1999' =>
88
				array( '+1999-01-00T00:00:00Z' ),
89
			'January/1' =>
90
				array( '+0001-01-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
91
			'1999 January' =>
92
				array( '+1999-01-00T00:00:00Z' ),
93
			'January 1999' =>
94
				array( '+1999-01-00T00:00:00Z' ),
95
			'January-1' =>
96
				array( '+0001-01-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
97
			'JanuARY-1' =>
98
				array( '+0001-01-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
99
			'JaN/1999' =>
100
				array( '+1999-01-00T00:00:00Z' ),
101
			'januARY-1' =>
102
				array( '+0001-01-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
103
			'jan/1999' =>
104
				array( '+1999-01-00T00:00:00Z' ),
105
106
			// use different date separators
107
			'1-1999' =>
108
				array( '+1999-01-00T00:00:00Z' ),
109
			'1/1999' =>
110
				array( '+1999-01-00T00:00:00Z' ),
111
			'1 / 1999' =>
112
				array( '+1999-01-00T00:00:00Z' ),
113
			'1 1999' =>
114
				array( '+1999-01-00T00:00:00Z' ),
115
			'1,1999' =>
116
				array( '+1999-01-00T00:00:00Z' ),
117
			'1.1999' =>
118
				array( '+1999-01-00T00:00:00Z' ),
119
			'1. 1999' =>
120
				array( '+1999-01-00T00:00:00Z' ),
121
122
			// presume mm/yy unless impossible month, in which case switch
123
			'12/12' =>
124
				array( '+0012-12-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
125
			'12/11' =>
126
				array( '+0011-12-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
127
			'11/12' =>
128
				array( '+0012-11-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
129
			'13/12' =>
130
				array( '+0013-12-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
131
			'12/13' =>
132
				array( '+0013-12-00T00:00:00Z', TimeValue::PRECISION_MONTH, $julian ),
133
			'2000 1' =>
134
				array( '+2000-01-00T00:00:00Z' ),
135
136
			// big years
137
			'April-1000000001' =>
138
				array( '+1000000001-04-00T00:00:00Z' ),
139
			'April 1000000001' =>
140
				array( '+1000000001-04-00T00:00:00Z' ),
141
			'1000000001 April' =>
142
				array( '+1000000001-04-00T00:00:00Z' ),
143
			'1 13000' =>
144
				array( '+13000-01-00T00:00:00Z' ),
145
146
			// parse 0 month as if no month has been entered
147
			'0.1999' =>
148
				array( '+1999-00-00T00:00:00Z', TimeValue::PRECISION_YEAR ),
149
			'1999 0' =>
150
				array( '+1999-00-00T00:00:00Z', TimeValue::PRECISION_YEAR ),
151
		);
152
153
		foreach ( $valid as $value => $expected ) {
154
			$timestamp = $expected[0];
155
			$precision = isset( $expected[1] ) ? $expected[1] : TimeValue::PRECISION_MONTH;
156
			$calendarModel = isset( $expected[2] ) ? $expected[2] : $gregorian;
157
158
			$argLists[] = array(
159
				(string)$value,
160
				new TimeValue( $timestamp, 0, 0, 0, $precision, $calendarModel )
161
			);
162
		}
163
164
		return $argLists;
165
	}
166
167
	/**
168
	 * @see StringValueParserTest::invalidInputProvider
169
	 */
170
	public function invalidInputProvider() {
171
		$argLists = parent::invalidInputProvider();
172
173
		$invalid = array(
174
			// These are just wrong
175
			'June June June',
176
			'June June',
177
			'111 111 111',
178
			'Jann 2014',
179
			'13/13',
180
			'13,1999',
181
			'1999,13',
182
			"12 1950\n12",
183
184
			// Months with signs or more than two digits are most probably not a month
185
			'-0 1999',
186
			'-4 1999',
187
			'-4 -1999',
188
			'-April 1998',
189
			'000 1999',
190
			'012 1999',
191
			'00001 1999',
192
			'000000001 100001999',
193
194
			// Dont parse stuff with separators in the year
195
			'june 200,000,000',
196
			'june 200.000.000',
197
198
			// Not within the scope of this parser
199
			'1 June 20000',
200
			'20000',
201
			'-1998',
202
203
			// BCE is not supported yet
204
			'April 1998 BCE',
205
			'1998 April BCE',
206
			'1998 BCE April',
207
		);
208
209
		foreach ( $invalid as $value ) {
210
			$argLists[] = array( $value );
211
		}
212
213
		return $argLists;
214
	}
215
216
}
217