3 namespace Wikimedia\ParamValidator\TypeDef
;
5 use Wikimedia\Timestamp\ConvertibleTimestamp
;
6 use Wikimedia\ParamValidator\SimpleCallbacks
;
7 use Wikimedia\ParamValidator\ValidationException
;
10 * @covers Wikimedia\ParamValidator\TypeDef\TimestampDef
12 class TimestampDefTest
extends TypeDefTestCase
{
14 protected static $testClass = TimestampDef
::class;
16 protected function getInstance( SimpleCallbacks
$callbacks, array $options ) {
17 if ( static::$testClass === null ) {
18 throw new \
LogicException( 'Either assign static::$testClass or override ' . __METHOD__
);
21 return new static::$testClass( $callbacks, $options );
24 /** @dataProvider provideValidate */
25 public function testValidate(
26 $value, $expect, array $settings = [], array $options = [], array $expectConds = []
28 $reset = ConvertibleTimestamp
::setFakeTime( 1559764242 );
30 parent
::testValidate( $value, $expect, $settings, $options, $expectConds );
32 ConvertibleTimestamp
::setFakeTime( $reset );
36 public function provideValidate() {
37 $specific = new ConvertibleTimestamp( 1517630706 );
38 $specificMs = new ConvertibleTimestamp( 1517630706.999 );
39 $now = new ConvertibleTimestamp( 1559764242 );
41 $formatDT = [ TimestampDef
::PARAM_TIMESTAMP_FORMAT
=> 'DateTime' ];
42 $formatMW = [ TimestampDef
::PARAM_TIMESTAMP_FORMAT
=> TS_MW
];
45 // We don't try to validate all formats supported by ConvertibleTimestamp, just
46 // some of the interesting ones.
47 'ISO format' => [ '2018-02-03T04:05:06Z', $specific ],
48 'ISO format with TZ' => [ '2018-02-03T00:05:06-04:00', $specific ],
49 'ISO format without punctuation' => [ '20180203T040506', $specific ],
50 'ISO format with ms' => [ '2018-02-03T04:05:06.999000Z', $specificMs ],
51 'ISO format with ms without punctuation' => [ '20180203T040506.999', $specificMs ],
52 'MW format' => [ '20180203040506', $specific ],
53 'Generic format' => [ '2018-02-03 04:05:06', $specific ],
54 'Generic format + GMT' => [ '2018-02-03 04:05:06 GMT', $specific ],
55 'Generic format + TZ +0100' => [ '2018-02-03 05:05:06+0100', $specific ],
56 'Generic format + TZ -01' => [ '2018-02-03 03:05:06-01', $specific ],
57 'Seconds-since-epoch format' => [ '1517630706', $specific ],
58 'Now' => [ 'now', $now ],
61 'Empty' => [ '', $now, [], [], [ [ 'unclearnowtimestamp' ] ] ],
62 'Zero' => [ '0', $now, [], [], [ [ 'unclearnowtimestamp' ] ] ],
67 new ValidationException( 'test', 'bogus', [], 'badtimestamp', [] ),
71 '=> DateTime' => [ 'now', $now->timestamp
, $formatDT ],
72 '=> TS_MW' => [ 'now', '20190605195042', $formatMW ],
73 '=> TS_MW as default' => [ 'now', '20190605195042', [], [ 'defaultFormat' => TS_MW
] ],
74 '=> TS_MW overriding default'
75 => [ 'now', '20190605195042', $formatMW, [ 'defaultFormat' => TS_ISO_8601
] ],
79 public function provideStringifyValue() {
80 $specific = new ConvertibleTimestamp( '20180203040506' );
83 [ '20180203040506', '2018-02-03T04:05:06Z' ],
84 [ $specific, '2018-02-03T04:05:06Z' ],
85 [ $specific->timestamp
, '2018-02-03T04:05:06Z' ],
86 [ $specific, '20180203040506', [], [ 'stringifyFormat' => TS_MW
] ],