Rename Convert*a*bleTimestamp to Convert*i*bleTimestamp
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / time / ConvertibleTimestampTest.php
1 <?php
2
3 /**
4 * Tests timestamp parsing and output.
5 */
6 class ConvertibleTimestampTest extends PHPUnit_Framework_TestCase {
7 /**
8 * @covers ConvertibleTimestamp::__construct
9 */
10 public function testConstructWithNoTimestamp() {
11 $timestamp = new ConvertibleTimestamp();
12 $this->assertInternalType( 'string', $timestamp->getTimestamp() );
13 $this->assertNotEmpty( $timestamp->getTimestamp() );
14 $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW ) ) );
15 }
16
17 /**
18 * @covers ConvertibleTimestamp::__toString
19 */
20 public function testToString() {
21 $timestamp = new ConvertibleTimestamp( '1406833268' ); // Equivalent to 20140731190108
22 $this->assertEquals( '1406833268', $timestamp->__toString() );
23 }
24
25 public static function provideValidTimestampDifferences() {
26 return [
27 [ '1406833268', '1406833269', '00 00 00 01' ],
28 [ '1406833268', '1406833329', '00 00 01 01' ],
29 [ '1406833268', '1406836929', '00 01 01 01' ],
30 [ '1406833268', '1406923329', '01 01 01 01' ],
31 ];
32 }
33
34 /**
35 * @dataProvider provideValidTimestampDifferences
36 * @covers ConvertibleTimestamp::diff
37 */
38 public function testDiff( $timestamp1, $timestamp2, $expected ) {
39 $timestamp1 = new ConvertibleTimestamp( $timestamp1 );
40 $timestamp2 = new ConvertibleTimestamp( $timestamp2 );
41 $diff = $timestamp1->diff( $timestamp2 );
42 $this->assertEquals( $expected, $diff->format( '%D %H %I %S' ) );
43 }
44
45 /**
46 * Test parsing of valid timestamps and outputing to MW format.
47 * @dataProvider provideValidTimestamps
48 * @covers ConvertibleTimestamp::getTimestamp
49 */
50 public function testValidParse( $format, $original, $expected ) {
51 $timestamp = new ConvertibleTimestamp( $original );
52 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
53 }
54
55 /**
56 * Test outputting valid timestamps to different formats.
57 * @dataProvider provideValidTimestamps
58 * @covers ConvertibleTimestamp::getTimestamp
59 */
60 public function testValidOutput( $format, $expected, $original ) {
61 $timestamp = new ConvertibleTimestamp( $original );
62 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
63 }
64
65 /**
66 * Test an invalid timestamp.
67 * @expectedException TimestampException
68 * @covers ConvertibleTimestamp
69 */
70 public function testInvalidParse() {
71 new ConvertibleTimestamp( "This is not a timestamp." );
72 }
73
74 /**
75 * @dataProvider provideValidTimestamps
76 * @covers ConvertibleTimestamp::convert
77 */
78 public function testConvert( $format, $expected, $original ) {
79 $this->assertSame( $expected, ConvertibleTimestamp::convert( $format, $original ) );
80 }
81
82 /**
83 * Format an invalid timestamp.
84 * @covers ConvertibleTimestamp::convert
85 */
86 public function testConvertInvalid() {
87 $this->assertSame( false, ConvertibleTimestamp::convert( 'Not a timestamp', 0 ) );
88 }
89
90 /**
91 * Test an out of range timestamp
92 * @dataProvider provideOutOfRangeTimestamps
93 * @expectedException TimestampException
94 * @covers ConvertibleTimestamp
95 */
96 public function testOutOfRangeTimestamps( $format, $input ) {
97 $timestamp = new ConvertibleTimestamp( $input );
98 $timestamp->getTimestamp( $format );
99 }
100
101 /**
102 * Test requesting an invalid output format.
103 * @expectedException TimestampException
104 * @covers ConvertibleTimestamp::getTimestamp
105 */
106 public function testInvalidOutput() {
107 $timestamp = new ConvertibleTimestamp( '1343761268' );
108 $timestamp->getTimestamp( 98 );
109 }
110
111 /**
112 * Returns a list of valid timestamps in the format:
113 * [ type, timestamp_of_type, timestamp_in_MW ]
114 */
115 public static function provideValidTimestamps() {
116 return [
117 // Various formats
118 [ TS_UNIX, '1343761268', '20120731190108' ],
119 [ TS_MW, '20120731190108', '20120731190108' ],
120 [ TS_DB, '2012-07-31 19:01:08', '20120731190108' ],
121 [ TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ],
122 [ TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ],
123 [ TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ],
124 [ TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ],
125 [ TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ],
126 [ TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ],
127 // Some extremes and weird values
128 [ TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ],
129 [ TS_UNIX, '-62135596801', '00001231235959' ]
130 ];
131 }
132
133 /**
134 * Returns a list of out of range timestamps in the format:
135 * [ type, timestamp_of_type ]
136 */
137 public static function provideOutOfRangeTimestamps() {
138 return [
139 // Various formats
140 [ TS_MW, '-62167219201' ], // -0001-12-31T23:59:59Z
141 [ TS_MW, '253402300800' ], // 10000-01-01T00:00:00Z
142 ];
143 }
144 }