Completed
Push — master ( fa5c40...b9f5d8 )
by mw
77:33 queued 47:29
created

IntlTimeFormatterTest::localizedFormatProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 34
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 60
rs 9.5555

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\IntlTimeFormatter;
6
use SMW\Localizer;
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, $languageCode, $formatOption, $expected ) {
36
37
		$language = $this->getMockBuilder( '\Language' )
0 ignored issues
show
Unused Code introduced by
$language is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38
			->disableOriginalConstructor()
39
			->getMock();
40
41
		$instance = new IntlTimeFormatter(
42
			DITime::doUnserialize( $serialization ),
43
			Localizer::getInstance()->getLanguage( $languageCode )
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
			Localizer::getInstance()->getLanguage( $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
			'en',
117
			'Y/m/d H:i:s',
118
			'2000/12/12 01:01:20'
119
		);
120
121
		#1
122
		$provider[] = array(
123
			'2/2000/12/12/1/1/20/200',
124
			'en',
125
			'Y/m/d H:i:s',
126
			'2000/12/12 01:01:20'
127
		);
128
129
		#2
130
		$provider[] = array(
131
			'1/2000/12/12/1/1/20.200',
132
			'en',
133
			'Y/m/d H:i:s.u',
134
			'2000/12/12 01:01:20.200000'
135
		);
136
137
		#3
138
		$provider[] = array(
139
			'2/1300/11/02/12/03/25.888499949',
140
			'en',
141
			'Y-m-d H:i:s.u',
142
			'1300-11-02 12:03:25.888500'
143
		);
144
145
		#4 time alone doesn't require a calendar model
146
		$provider[] = array(
147
			'2/1300/11/02/12/03/25.888499949',
148
			'en',
149
			'H:i:s.u',
150
			'12:03:25.888500'
151
		);
152
153
		#5
154
		$provider['on monthnumber 12'] = array(
155
			'1/2000/12/12',
156
			'en',
157
			'Y-m-d M',
158
			'2000-12-12 Dec'
159
		);
160
161
		#4
162
		$provider['on daynumber 7'] = array(
163
			'1/2016/05/08/1/1/20/200',
164
			'en',
165
			'Y-m-d D',
166
			'2016-05-08 Sun'
167
		);
168
169
		return $provider;
170
	}
171
172
	public function localizedFormatProvider() {
173
174
		#0
175
		$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...
176
			'1/2000/12/12/1/1/20/200',
177
			'en',
178
			'01:01:20, 12 December 2000'
179
		);
180
181
		#1
182
		$provider[] = array(
183
			'1/2000/12/12/1/1/20/200',
184
			'ja',
185
			'2000年12月12日 (火) 01:01:20'
186
		);
187
188
		#2
189
		$provider[] = array(
190
			'1/2000/12/12/1/1/20/200',
191
			'es',
192
			'01:01:20 12 dic 2000'
193
		);
194
195
		#3
196
		$provider['on daynumber 1'] = array(
197
			'1/2016/05/02/1/1/20/200',
198
			'ja',
199
			'2016年5月2日 (月) 01:01:20'
200
		);
201
202
		#4
203
		$provider['on daynumber 7'] = array(
204
			'1/2016/05/08/1/1/20/200',
205
			'ja',
206
			'2016年5月8日 (日) 01:01:20'
207
		);
208
209
		#5
210
		$provider['midnight-ja'] = array(
211
			'1/2016/05/08/00/00/00/00',
212
			'ja',
213
			'2016年5月8日 (日) 00:00:00'
214
		);
215
216
		#6
217
		$provider['midnight-en'] = array(
218
			'1/2016/05/08/0/0/0/0',
219
			'en',
220
			'00:00:00, 8 May 2016'
221
		);
222
223
		#6
224
		$provider['after-midnight'] = array(
225
			'1/2016/05/08/0/0/01/0',
226
			'en',
227
			'00:00:01, 8 May 2016'
228
		);
229
230
		return $provider;
231
	}
232
}
233