Test Setup Failed
Push — master ( 4168c9...c3df72 )
by
unknown
04:59 queued 48s
created

TimeFormatterTest::testValidFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
namespace ValueFormatters\Test;
4
5
use DataValues\TimeValue;
6
use PHPUnit\Framework\TestCase;
7
use ValueFormatters\FormatterOptions;
8
use ValueFormatters\TimeFormatter;
9
use ValueFormatters\ValueFormatter;
10
11
/**
12
 * @covers ValueFormatters\TimeFormatter
13
 *
14
 * @group ValueFormatters
15
 * @group DataValueExtensions
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author H. Snater < [email protected] >
19
 * @author Thiemo Kreuz
20
 */
21
class TimeFormatterTest extends TestCase {
22
23
	/**
24
	 * @see ValueFormatterTestBase::getInstance
25
	 *
26
	 * @param FormatterOptions|null $options
27
	 *
28
	 * @return TimeFormatter
29
	 */
30
	protected function getInstance( FormatterOptions $options = null ) {
31
		return new TimeFormatter( $options );
32
	}
33
34
	/**
35
	 * @see ValueFormatterTestBase::validProvider
36
	 */
37
	public function validProvider() {
38
		$gregorian = 'http://www.wikidata.org/entity/Q1985727';
39
		$julian = 'http://www.wikidata.org/entity/Q1985786';
40
41
		$baseOptions = new FormatterOptions();
42
43
		$tests = array(
44
			'+2013-07-16T00:00:00Z' => array(
45
				'+2013-07-16T00:00:00Z',
46
			),
47
			'+0000-01-01T00:00:00Z' => array(
48
				'+0000-01-01T00:00:00Z',
49
			),
50
51
			// Different calendar models
52
			'+0001-01-14T00:00:00Z' => array(
53
				'+0001-01-14T00:00:00Z',
54
				TimeValue::PRECISION_DAY,
55
				$julian
56
			),
57
58
			// Different years
59
			'+10000-01-01T00:00:00Z' => array(
60
				'+10000-01-01T00:00:00Z',
61
			),
62
			'-0001-01-01T00:00:00Z' => array(
63
				'-0001-01-01T00:00:00Z',
64
			),
65
66
			// Different precisions
67
			'+2013-07-17T00:00:00Z' => array(
68
				'+2013-07-17T00:00:00Z',
69
				TimeValue::PRECISION_MONTH,
70
			),
71
			'+2013-07-18T00:00:00Z' => array(
72
				'+2013-07-18T00:00:00Z',
73
				TimeValue::PRECISION_YEAR,
74
			),
75
			'+2013-07-19T00:00:00Z' => array(
76
				'+2013-07-19T00:00:00Z',
77
				TimeValue::PRECISION_YEAR10,
78
			),
79
		);
80
81
		$argLists = array();
82
83
		foreach ( $tests as $expected => $args ) {
84
			$timestamp = $args[0];
85
			$precision = isset( $args[1] ) ? $args[1] : TimeValue::PRECISION_DAY;
86
			$calendarModel = isset( $args[2] ) ? $args[2] : $gregorian;
87
			$options = isset( $args[3] ) ? $args[3] : $baseOptions;
88
89
			$argLists[] = array(
90
				new TimeValue( $timestamp, 0, 0, 0, $precision, $calendarModel ),
91
				$expected,
92
				$options
93
			);
94
		}
95
96
		return $argLists;
97
	}
98
99
	/**
100
	 * @dataProvider validProvider
101
	 *
102
	 * @since 0.1
103
	 *
104
	 * @param mixed $value
105
	 * @param mixed $expected
106
	 * @param FormatterOptions|null $options
107
	 * @param ValueFormatter|null $formatter
108
	 */
109
	public function testValidFormat(
110
		$value,
111
		$expected,
112
		FormatterOptions $options = null,
113
		ValueFormatter $formatter = null
114
	) {
115
		if ( $formatter === null ) {
116
			$formatter = $this->getInstance( $options );
117
		}
118
119
		$this->assertSame( $expected, $formatter->format( $value ) );
120
	}
121
122
}
123