YearMonthTimeParserTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 195
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 18 1
B validInputProvider() 0 115 4
A invalidInputProvider() 0 45 2
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-or-later
18
 * @author Addshore
19
 * @author Thiemo Kreuz
20
 */
21
class YearMonthTimeParserTest extends ValueParserTestCase {
22
23
	/**
24
	 * @see ValueParserTestBase::getInstance
25
	 *
26
	 * @return YearMonthTimeParser
27
	 */
28
	protected function getInstance() {
29
		$monthNameProvider = $this->getMockBuilder( MonthNameProvider::class )
30
			->disableOriginalConstructor()
31
			->getMock();
32
		$monthNameProvider->expects( $this->once() )
33
			->method( 'getMonthNumbers' )
34
			->with( 'en' )
35
			->will( $this->returnValue( array(
36
				'January' => 1,
37
				'Jan' => 1,
38
				// to test Unicode (it's Czech)
39
				'Březen' => 3,
40
				'April' => 4,
41
				'June' => 6,
42
			) ) );
43
44
		return new YearMonthTimeParser( $monthNameProvider );
0 ignored issues
show
Documentation introduced by
$monthNameProvider is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<ValueParsers\MonthNameProvider>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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