Completed
Pull Request — master (#83)
by no
05:18 queued 02:36
created

DateFormatParserTest::getParserClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace ValueParsers\Test;
4
5
use DataValues\TimeValue;
6
use ValueParsers\DateFormatParser;
7
use ValueParsers\ParserOptions;
8
9
/**
10
 * @covers ValueParsers\DateFormatParser
11
 *
12
 * @group ValueParsers
13
 * @group WikibaseLib
14
 * @group Wikibase
15
 * @group TimeParsers
16
 *
17
 * @licence GNU GPL v2+
18
 * @author Thiemo Mättig
19
 */
20
class DateFormatParserTest extends StringValueParserTest {
21
22
	/**
23
	 * @deprecated since 0.3, just use getInstance.
24
	 */
25
	protected function getParserClass() {
26
		throw new \LogicException( 'Should not be called, use getInstance' );
27
	}
28
29
	/**
30
	 * @see ValueParserTestBase::getInstance
31
	 *
32
	 * @return DateFormatParser
33
	 */
34
	protected function getInstance() {
35
		return new DateFormatParser();
36
	}
37
38
	/**
39
	 * @see ValueParserTestBase::validInputProvider
40
	 */
41
	public function validInputProvider() {
42
		$monthNames = array( 9 => array( 'Sep', 'September' ) );
43
44
		$valid = array(
45
			'Default options' => array(
46
				'1 9 2014',
47
				'd M Y', null, null,
48
				'+2014-09-01T00:00:00Z'
49
			),
50
			'Transform map' => array(
51
				'Z g Zo15',
52
				'd M Y', array( '0' => 'o', 2 => 'Z', 9 => 'g' ), null,
53
				'+2015-09-02T00:00:00Z'
54
			),
55
			'Default month map' => array(
56
				'1. September 2014',
57
				'd. M Y', null, $monthNames,
58
				'+2014-09-01T00:00:00Z'
59
			),
60
			'Simple month map' => array(
61
				'1 September 2014',
62
				'd M Y', null, array( 9 => 'September' ),
63
				'+2014-09-01T00:00:00Z'
64
			),
65
			'Escapes' => array(
66
				'1s 9s 2014',
67
				'd\s M\s Y', null, null,
68
				'+2014-09-01T00:00:00Z'
69
			),
70
			'Quotes' => array(
71
				'1th 9th 2014',
72
				'd"th" M"th" Y', null, null,
73
				'+2014-09-01T00:00:00Z'
74
			),
75
			'Raw modifiers' => array(
76
				'2014 9 1',
77
				'Y xNmxN xnd', null, null,
78
				'+2014-09-01T00:00:00Z'
79
			),
80
			'Whitespace is optional' => array(
81
				'1September2014',
82
				'd M Y', null, $monthNames,
83
				'+2014-09-01T00:00:00Z'
84
			),
85
			'Delimiters are optional' => array(
86
				'1 9 2014',
87
				'd. M. Y', null, null,
88
				'+2014-09-01T00:00:00Z'
89
			),
90
			'Delimiters are ignored' => array(
91
				'1. 9. 2014',
92
				'd M Y', null, null,
93
				'+2014-09-01T00:00:00Z'
94
			),
95
			'Year precision' => array(
96
				'2014',
97
				'Y', null, null,
98
				'+2014-00-00T00:00:00Z', TimeValue::PRECISION_YEAR
99
			),
100
			'Month precision' => array(
101
				'9 2014',
102
				'M Y', null, null,
103
				'+2014-09-00T00:00:00Z', TimeValue::PRECISION_MONTH
104
			),
105
			'Minute precision' => array(
106
				'1 9 2014 15:30',
107
				'd M Y H:i', null, null,
108
				'+2014-09-01T15:30:00Z', TimeValue::PRECISION_MINUTE
109
			),
110
			'Second precision' => array(
111
				'1 9 2014 15:30:59',
112
				'd M Y H:i:s', null, null,
113
				'+2014-09-01T15:30:59Z', TimeValue::PRECISION_SECOND
114
			),
115
		);
116
117
		$cases = array();
118
119
		foreach ( $valid as $key => $args ) {
120
			$dateString = $args[0];
121
			$dateFormat = $args[1];
122
			$digitTransformTable = $args[2];
123
			$monthNames = $args[3];
124
			$timestamp = $args[4];
125
			$precision = isset( $args[5] ) ? $args[5] : TimeValue::PRECISION_DAY;
126
			$calendarModel = isset( $args[6] ) ? $args[6] : TimeValue::CALENDAR_GREGORIAN;
127
128
			$cases[$key] = array(
129
				$dateString,
130
				new TimeValue( $timestamp, 0, 0, 0, $precision, $calendarModel ),
131
				new DateFormatParser( new ParserOptions( array(
132
					DateFormatParser::OPT_DATE_FORMAT => $dateFormat,
133
					DateFormatParser::OPT_DIGIT_TRANSFORM_TABLE => $digitTransformTable,
134
					DateFormatParser::OPT_MONTH_NAMES => $monthNames,
135
				) ) )
136
			);
137
		}
138
139
		return $cases;
140
	}
141
142
	/**
143
	 * @see StringValueParserTest::invalidInputProvider
144
	 */
145
	public function invalidInputProvider() {
146
		$invalid = array(
147
			'',
148
		);
149
150
		$cases = parent::invalidInputProvider();
151
152
		foreach ( $invalid as $value ) {
153
			$cases[] = array( $value );
154
		}
155
156
		return $cases;
157
	}
158
159
	public function testInvalidDateFormatOption() {
160
		$parser = new DateFormatParser( new ParserOptions( array(
161
			DateFormatParser::OPT_DATE_FORMAT => 'YY',
162
		) ) );
163
		$this->setExpectedException( 'ValueParsers\ParseException' );
164
		$parser->parse( '' );
165
	}
166
167
}
168