MediaWiki  1.28.0
ConvertibleTimestampTest.php
Go to the documentation of this file.
1 <?php
2 
6 class ConvertibleTimestampTest extends PHPUnit_Framework_TestCase {
10  public function testConstructWithNoTimestamp() {
12  $this->assertInternalType( 'string', $timestamp->getTimestamp() );
13  $this->assertNotEmpty( $timestamp->getTimestamp() );
14  $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW ) ) );
15  }
16 
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 
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 
50  public function testValidParse( $format, $original, $expected ) {
51  $timestamp = new ConvertibleTimestamp( $original );
52  $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
53  }
54 
60  public function testValidOutput( $format, $expected, $original ) {
61  $timestamp = new ConvertibleTimestamp( $original );
62  $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
63  }
64 
70  public function testInvalidParse() {
71  new ConvertibleTimestamp( "This is not a timestamp." );
72  }
73 
78  public function testConvert( $format, $expected, $original ) {
79  $this->assertSame( $expected, ConvertibleTimestamp::convert( $format, $original ) );
80  }
81 
86  public function testConvertInvalid() {
87  $this->assertSame( false, ConvertibleTimestamp::convert( 'Not a timestamp', 0 ) );
88  }
89 
96  public function testOutOfRangeTimestamps( $format, $input ) {
97  $timestamp = new ConvertibleTimestamp( $input );
98  $timestamp->getTimestamp( $format );
99  }
100 
106  public function testInvalidOutput() {
107  $timestamp = new ConvertibleTimestamp( '1343761268' );
108  $timestamp->getTimestamp( 98 );
109  }
110 
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 
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 }
Library for creating, parsing, and converting timestamps.
testConvert($format, $expected, $original)
provideValidTimestamps ConvertibleTimestamp::convert
const TS_RFC2822
RFC 2822 format, for E-mail and HTTP headers.
Definition: defines.php:21
testConvertInvalid()
Format an invalid timestamp.
static provideValidTimestamps()
Returns a list of valid timestamps in the format: [ type, timestamp_of_type, timestamp_in_MW ]...
Tests timestamp parsing and output.
const TS_ISO_8601
ISO 8601 format with no timezone: 1986-02-09T20:00:00Z.
Definition: defines.php:28
testValidParse($format, $original, $expected)
Test parsing of valid timestamps and outputing to MW format.
testConstructWithNoTimestamp()
ConvertibleTimestamp::__construct.
testInvalidParse()
Test an invalid timestamp.
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: defines.php:6
testDiff($timestamp1, $timestamp2, $expected)
provideValidTimestampDifferences ConvertibleTimestamp::diff
testInvalidOutput()
Test requesting an invalid output format.
if($limit) $timestamp
const TS_EXIF
An Exif timestamp (YYYY:MM:DD HH:MM:SS)
Definition: defines.php:37
testOutOfRangeTimestamps($format, $input)
Test an out of range timestamp provideOutOfRangeTimestamps TimestampException ConvertibleTimestamp.
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: defines.php:11
const TS_ISO_8601_BASIC
ISO 8601 basic format with no timezone: 19860209T200000Z.
Definition: defines.php:52
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static provideOutOfRangeTimestamps()
Returns a list of out of range timestamps in the format: [ type, timestamp_of_type ]...
const TS_POSTGRES
Postgres format time.
Definition: defines.php:47
static convert($style=TS_UNIX, $ts)
Convert a timestamp string to a given format.
testToString()
ConvertibleTimestamp::__toString.
testValidOutput($format, $expected, $original)
Test outputting valid timestamps to different formats.
const TS_ORACLE
Oracle format time.
Definition: defines.php:42
const TS_DB
MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
Definition: defines.php:16