Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / libs / time / ConvertibleTimestampTest.php
blobd48caf37f0e943cd273e22ed51b05bfbe6478535
1 <?php
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 ) ) );
17 /**
18 * @covers ConvertibleTimestamp::__toString
20 public function testToString() {
21 $timestamp = new ConvertibleTimestamp( '1406833268' ); // Equivalent to 20140731190108
22 $this->assertEquals( '1406833268', $timestamp->__toString() );
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' ],
34 /**
35 * @dataProvider provideValidTimestampDifferences
36 * @covers ConvertibleTimestamp::diff
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' ) );
45 /**
46 * Test parsing of valid timestamps and outputing to MW format.
47 * @dataProvider provideValidTimestamps
48 * @covers ConvertibleTimestamp::getTimestamp
50 public function testValidParse( $format, $original, $expected ) {
51 $timestamp = new ConvertibleTimestamp( $original );
52 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
55 /**
56 * Test outputting valid timestamps to different formats.
57 * @dataProvider provideValidTimestamps
58 * @covers ConvertibleTimestamp::getTimestamp
60 public function testValidOutput( $format, $expected, $original ) {
61 $timestamp = new ConvertibleTimestamp( $original );
62 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
65 /**
66 * Test an invalid timestamp.
67 * @expectedException TimestampException
68 * @covers ConvertibleTimestamp
70 public function testInvalidParse() {
71 new ConvertibleTimestamp( "This is not a timestamp." );
74 /**
75 * @dataProvider provideValidTimestamps
76 * @covers ConvertibleTimestamp::convert
78 public function testConvert( $format, $expected, $original ) {
79 $this->assertSame( $expected, ConvertibleTimestamp::convert( $format, $original ) );
82 /**
83 * Format an invalid timestamp.
84 * @covers ConvertibleTimestamp::convert
86 public function testConvertInvalid() {
87 $this->assertSame( false, ConvertibleTimestamp::convert( 'Not a timestamp', 0 ) );
90 /**
91 * Test an out of range timestamp
92 * @dataProvider provideOutOfRangeTimestamps
93 * @expectedException TimestampException
94 * @covers ConvertibleTimestamp
96 public function testOutOfRangeTimestamps( $format, $input ) {
97 $timestamp = new ConvertibleTimestamp( $input );
98 $timestamp->getTimestamp( $format );
102 * Test requesting an invalid output format.
103 * @expectedException TimestampException
104 * @covers ConvertibleTimestamp::getTimestamp
106 public function testInvalidOutput() {
107 $timestamp = new ConvertibleTimestamp( '1343761268' );
108 $timestamp->getTimestamp( 98 );
112 * Returns a list of valid timestamps in the format:
113 * [ type, timestamp_of_type, timestamp_in_MW ]
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' ]
134 * Returns a list of out of range timestamps in the format:
135 * [ type, timestamp_of_type ]
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