Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / ParamValidator / TypeDef / TimestampDefTest.php
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 use Wikimedia\Timestamp\ConvertibleTimestamp;
6 use Wikimedia\ParamValidator\SimpleCallbacks;
7 use Wikimedia\ParamValidator\ValidationException;
8
9 /**
10 * @covers Wikimedia\ParamValidator\TypeDef\TimestampDef
11 */
12 class TimestampDefTest extends TypeDefTestCase {
13
14 protected static $testClass = TimestampDef::class;
15
16 protected function getInstance( SimpleCallbacks $callbacks, array $options ) {
17 if ( static::$testClass === null ) {
18 throw new \LogicException( 'Either assign static::$testClass or override ' . __METHOD__ );
19 }
20
21 return new static::$testClass( $callbacks, $options );
22 }
23
24 /** @dataProvider provideValidate */
25 public function testValidate(
26 $value, $expect, array $settings = [], array $options = [], array $expectConds = []
27 ) {
28 $reset = ConvertibleTimestamp::setFakeTime( 1559764242 );
29 try {
30 parent::testValidate( $value, $expect, $settings, $options, $expectConds );
31 } finally {
32 ConvertibleTimestamp::setFakeTime( $reset );
33 }
34 }
35
36 public function provideValidate() {
37 $specific = new ConvertibleTimestamp( 1517630706 );
38 $specificMs = new ConvertibleTimestamp( 1517630706.999 );
39 $now = new ConvertibleTimestamp( 1559764242 );
40
41 $formatDT = [ TimestampDef::PARAM_TIMESTAMP_FORMAT => 'DateTime' ];
42 $formatMW = [ TimestampDef::PARAM_TIMESTAMP_FORMAT => TS_MW ];
43
44 return [
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 ],
59
60 // Warnings
61 'Empty' => [ '', $now, [], [], [ [ 'unclearnowtimestamp' ] ] ],
62 'Zero' => [ '0', $now, [], [], [ [ 'unclearnowtimestamp' ] ] ],
63
64 // Error handling
65 'Bad value' => [
66 'bogus',
67 new ValidationException( 'test', 'bogus', [], 'badtimestamp', [] ),
68 ],
69
70 // Formatting
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 ] ],
76 ];
77 }
78
79 public function provideStringifyValue() {
80 $specific = new ConvertibleTimestamp( '20180203040506' );
81
82 return [
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 ] ],
87 ];
88 }
89
90 }