YearTimeParserTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 149
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 5 1
1
<?php
2
3
namespace ValueParsers\Test;
4
5
use DataValues\TimeValue;
6
use ValueParsers\EraParser;
7
use ValueParsers\ParseException;
8
use ValueParsers\ParserOptions;
9
use ValueParsers\YearTimeParser;
10
11
/**
12
 * @covers ValueParsers\YearTimeParser
13
 *
14
 * @group DataValue
15
 * @group DataValueExtensions
16
 * @group TimeParsers
17
 * @group ValueParsers
18
 *
19
 * @license GPL-2.0-or-later
20
 * @author Addshore
21
 * @author Thiemo Kreuz
22
 */
23
class YearTimeParserTest extends ValueParserTestCase {
24
25
	/**
26
	 * @see ValueParserTestBase::getInstance
27
	 *
28
	 * @return YearTimeParser
29
	 */
30
	protected function getInstance() {
31
		return new YearTimeParser( $this->getMockEraParser() );
0 ignored issues
show
Documentation introduced by
$this->getMockEraParser() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<ValueParsers\ValueParser>.

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