1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests; |
4
|
|
|
|
5
|
|
|
use SMW\IntlTimeFormatter; |
6
|
|
|
use SMWDITime as DITime; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \SMW\IntlTimeFormatter |
10
|
|
|
* @group semantic-mediawiki |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 2.4 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class IntlTimeFormatterTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
public function testCanConstruct() { |
20
|
|
|
|
21
|
|
|
$dataItem = $this->getMockBuilder( '\SMWDITime' ) |
22
|
|
|
->disableOriginalConstructor() |
23
|
|
|
->getMock(); |
24
|
|
|
|
25
|
|
|
$this->assertInstanceOf( |
26
|
|
|
'\SMW\IntlTimeFormatter', |
27
|
|
|
new IntlTimeFormatter( $dataItem ) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @dataProvider formatProvider |
33
|
|
|
*/ |
34
|
|
|
public function testFormat( $serialization, $formatOption, $expected ) { |
35
|
|
|
|
36
|
|
|
$language = $this->getMockBuilder( '\Language' ) |
37
|
|
|
->disableOriginalConstructor() |
38
|
|
|
->getMock(); |
39
|
|
|
|
40
|
|
|
$instance = new IntlTimeFormatter( |
41
|
|
|
DITime::doUnserialize( $serialization ), |
42
|
|
|
$language |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$this->assertEquals( |
46
|
|
|
$expected, |
47
|
|
|
$instance->format( $formatOption ) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testFormatWithLocalizedMonthReplacement() { |
52
|
|
|
|
53
|
|
|
// F - A full textual representation of a month, such as January or March |
54
|
|
|
$formatOption = 'F Y/m/d H:i:s'; |
55
|
|
|
|
56
|
|
|
$language = $this->getMockBuilder( '\Language' ) |
57
|
|
|
->disableOriginalConstructor() |
58
|
|
|
->getMock(); |
59
|
|
|
|
60
|
|
|
$language->expects( $this->once() ) |
61
|
|
|
->method( 'getMonthName' ) |
62
|
|
|
->with( $this->equalTo( '12' ) ) |
63
|
|
|
->will( $this->returnValue( 'Foo' ) ); |
64
|
|
|
|
65
|
|
|
$instance = new IntlTimeFormatter( |
66
|
|
|
DITime::doUnserialize( '1/2000/12/12/1/1/20.200' ), |
67
|
|
|
$language |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$this->assertEquals( |
71
|
|
|
'Foo 2000/12/12 01:01:20', |
72
|
|
|
$instance->format( $formatOption ) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function formatProvider() { |
77
|
|
|
|
78
|
|
|
#0 |
79
|
|
|
$provider[] = array( |
|
|
|
|
80
|
|
|
'1/2000/12/12/1/1/20/200', |
81
|
|
|
'Y/m/d H:i:s', |
82
|
|
|
'2000/12/12 01:01:20' |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
#1 |
86
|
|
|
$provider[] = array( |
87
|
|
|
'2/2000/12/12/1/1/20/200', |
88
|
|
|
'Y/m/d H:i:s', |
89
|
|
|
'2000/12/12 01:01:20 JL' |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
#2 |
93
|
|
|
$provider[] = array( |
94
|
|
|
'1/2000/12/12/1/1/20.200', |
95
|
|
|
'Y/m/d H:i:s.u', |
96
|
|
|
'2000/12/12 01:01:20.200000' |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
#3 |
100
|
|
|
$provider[] = array( |
101
|
|
|
'2/1300/11/02/12/03/25.888499949', |
102
|
|
|
'Y-m-d H:i:s.u', |
103
|
|
|
'1300-11-02 12:03:25.888500 JL' |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
#4 time alone doesn't require a calendar model |
107
|
|
|
$provider[] = array( |
108
|
|
|
'2/1300/11/02/12/03/25.888499949', |
109
|
|
|
'H:i:s.u', |
110
|
|
|
'12:03:25.888500' |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return $provider; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
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:
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 thebar
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.