Completed
Push — master ( f26bc0...123a08 )
by mw
13s
created

IntlTimeFormatterTest::testGetLocalizedFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\IntlTimeFormatter;
6
use Language;
7
use SMWDITime as DITime;
8
9
/**
10
 * @covers \SMW\IntlTimeFormatter
11
 * @group semantic-mediawiki
12
 *
13
 * @license GNU GPL v2+
14
 * @since 2.4
15
 *
16
 * @author mwjames
17
 */
18
class IntlTimeFormatterTest extends \PHPUnit_Framework_TestCase {
19
20
	public function testCanConstruct() {
21
22
		$dataItem = $this->getMockBuilder( '\SMWDITime' )
23
			->disableOriginalConstructor()
24
			->getMock();
25
26
		$this->assertInstanceOf(
27
			'\SMW\IntlTimeFormatter',
28
			new IntlTimeFormatter( $dataItem )
29
		);
30
	}
31
32
	/**
33
	 * @dataProvider formatProvider
34
	 */
35
	public function testFormat( $serialization, $formatOption, $expected ) {
36
37
		$language = $this->getMockBuilder( '\Language' )
38
			->disableOriginalConstructor()
39
			->getMock();
40
41
		$instance = new IntlTimeFormatter(
42
			DITime::doUnserialize( $serialization ),
43
			$language
44
		);
45
46
		$this->assertEquals(
47
			$expected,
48
			$instance->format( $formatOption )
49
		);
50
	}
51
52
	/**
53
	 * @dataProvider localizedFormatProvider
54
	 */
55
	public function testGetLocalizedFormat( $serialization, $languageCode, $expected ) {
56
57
		$instance = new IntlTimeFormatter(
58
			DITime::doUnserialize( $serialization ),
59
			Language::factory( $languageCode )
60
		);
61
62
		$this->assertEquals(
63
			$expected,
64
			$instance->getLocalizedFormat()
65
		);
66
	}
67
68
	public function testContainsValidDateFormatRule() {
69
70
		$formatOption = 'F Y/m/d H:i:s';
71
72
		$language = $this->getMockBuilder( '\Language' )
73
			->disableOriginalConstructor()
74
			->getMock();
75
76
		$instance = new IntlTimeFormatter(
77
			DITime::doUnserialize( '1/2000/12/12/1/1/20.200' ),
78
			$language
79
		);
80
81
		$this->assertTrue(
82
			$instance->containsValidDateFormatRule( $formatOption )
83
		);
84
	}
85
86
	public function testFormatWithLocalizedMonthReplacement() {
87
88
		// F - A full textual representation of a month, such as January or March
89
		$formatOption = 'F Y/m/d H:i:s';
90
91
		$language = $this->getMockBuilder( '\Language' )
92
			->disableOriginalConstructor()
93
			->getMock();
94
95
		$language->expects( $this->once() )
96
			->method( 'getMonthName' )
97
			->with( $this->equalTo( '12' ) )
98
			->will( $this->returnValue( 'Foo' ) );
99
100
		$instance = new IntlTimeFormatter(
101
			DITime::doUnserialize( '1/2000/12/12/1/1/20.200' ),
102
			$language
103
		);
104
105
		$this->assertEquals(
106
			'Foo 2000/12/12 01:01:20',
107
			$instance->format( $formatOption )
108
		);
109
	}
110
111
	public function formatProvider() {
112
113
		#0
114
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
115
			'1/2000/12/12/1/1/20/200',
116
			'Y/m/d H:i:s',
117
			'2000/12/12 01:01:20'
118
		);
119
120
		#1
121
		$provider[] = array(
122
			'2/2000/12/12/1/1/20/200',
123
			'Y/m/d H:i:s',
124
			'2000/12/12 01:01:20'
125
		);
126
127
		#2
128
		$provider[] = array(
129
			'1/2000/12/12/1/1/20.200',
130
			'Y/m/d H:i:s.u',
131
			'2000/12/12 01:01:20.200000'
132
		);
133
134
		#3
135
		$provider[] = array(
136
			'2/1300/11/02/12/03/25.888499949',
137
			'Y-m-d H:i:s.u',
138
			'1300-11-02 12:03:25.888500'
139
		);
140
141
		#4 time alone doesn't require a calendar model
142
		$provider[] = array(
143
			'2/1300/11/02/12/03/25.888499949',
144
			'H:i:s.u',
145
			'12:03:25.888500'
146
		);
147
148
		return $provider;
149
	}
150
151
	public function localizedFormatProvider() {
152
153
		#0
154
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
155
			'1/2000/12/12/1/1/20/200',
156
			'en',
157
			'01:01:20, 12 December 2000'
158
		);
159
160
		#1
161
		$provider[] = array(
162
			'1/2000/12/12/1/1/20/200',
163
			'ja',
164
			'2000年12月12日 (月) 01:01:20'
165
		);
166
167
		#2
168
		$provider[] = array(
169
			'1/2000/12/12/1/1/20/200',
170
			'es',
171
			'01:01:20 12 dic 2000'
172
		);
173
174
		return $provider;
175
	}
176
}
177