11
11
12
12
namespace Symfony \Component \VarDumper \Tests \Caster ;
13
13
14
+ use Doctrine \DBAL \Types \DateImmutableType ;
14
15
use PHPUnit \Framework \TestCase ;
15
16
use Symfony \Component \VarDumper \Caster \Caster ;
16
17
use Symfony \Component \VarDumper \Caster \DateCaster ;
@@ -289,4 +290,94 @@ public function provideTimeZones()
289
290
array ('Pacific/Tahiti ' , 'Pacific/Tahiti (-10:00) ' , $ xRegion ),
290
291
);
291
292
}
293
+
294
+ /**
295
+ * @dataProvider providePeriods
296
+ */
297
+ public function testDumpPeriod ($ start , $ interval , $ end , $ options , $ expected )
298
+ {
299
+ if (defined ('HHVM_VERSION_ID ' ) || \PHP_VERSION_ID < 50605 ) {
300
+ $ this ->markTestSkipped ();
301
+ }
302
+
303
+ $ p = new \DatePeriod (new \DateTime ($ start ), new \DateInterval ($ interval ), is_int ($ end ) ? $ end : new \DateTime ($ end ), $ options );
304
+
305
+ $ xDump = <<<EODUMP
306
+ DatePeriod {
307
+ period: $ expected
308
+ %A}
309
+ EODUMP ;
310
+
311
+ $ this ->assertDumpMatchesFormat ($ xDump , $ p );
312
+ }
313
+
314
+ /**
315
+ * @dataProvider providePeriods
316
+ */
317
+ public function testCastPeriod ($ start , $ interval , $ end , $ options , $ xPeriod , $ xDates )
318
+ {
319
+ if (defined ('HHVM_VERSION_ID ' ) || \PHP_VERSION_ID < 50605 ) {
320
+ $ this ->markTestSkipped ();
321
+ }
322
+
323
+ $ p = new \DatePeriod (new \DateTime ($ start ), new \DateInterval ($ interval ), is_int ($ end ) ? $ end : new \DateTime ($ end ), $ options );
324
+ $ stub = new Stub ();
325
+
326
+ $ cast = DateCaster::castPeriod ($ p , array (), $ stub , false , 0 );
327
+
328
+ $ xDump = <<<EODUMP
329
+ array:1 [
330
+ "\\x00~\\x00period" => $ xPeriod
331
+ ]
332
+ EODUMP ;
333
+
334
+ $ this ->assertDumpMatchesFormat ($ xDump , $ cast );
335
+
336
+ $ xDump = <<<EODUMP
337
+ Symfony\Component\VarDumper\Caster\ConstStub {
338
+ +type: 1
339
+ +class: " $ xPeriod"
340
+ +value: "%A $ xDates%A"
341
+ +cut: 0
342
+ +handle: 0
343
+ +refCount: 0
344
+ +position: 0
345
+ +attr: []
346
+ }
347
+ EODUMP ;
348
+
349
+ $ this ->assertDumpMatchesFormat ($ xDump , $ cast ["\0~ \0period " ]);
350
+ }
351
+
352
+ public function providePeriods ()
353
+ {
354
+ $ i = new \DateInterval ('PT0S ' );
355
+ $ ms = \PHP_VERSION_ID >= 70100 && isset ($ i ->f ) ? '.000000 ' : '' ;
356
+
357
+ $ periods = array (
358
+ array ('2017-01-01 ' , 'P1D ' , '2017-01-03 ' , 0 , 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-03 00:00:00 ' , '1) 2017-01-01%a2) 2017-01-02 ' ),
359
+ array ('2017-01-01 ' , 'P1D ' , 1 , 0 , 'every + 1d, from 2017-01-01 00:00:00 (included) for 2x ' , '1) 2017-01-01%a2) 2017-01-02 ' ),
360
+
361
+ array ('2017-01-01 ' , 'P1D ' , '2017-01-04 ' , 0 , 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-04 00:00:00 ' , '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03 ' ),
362
+ array ('2017-01-01 ' , 'P1D ' , 2 , 0 , 'every + 1d, from 2017-01-01 00:00:00 (included) for 3x ' , '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03 ' ),
363
+
364
+ array ('2017-01-01 ' , 'P1D ' , '2017-01-05 ' , 0 , 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-05 00:00:00 ' , '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03%a4) 2017-01-04 ' ),
365
+ array ('2017-01-01 ' , 'P1D ' , 3 , 0 , 'every + 1d, from 2017-01-01 00:00:00 (included) for 4x ' , '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03%a4) 2017-01-04 ' ),
366
+
367
+ array ('2017-01-01 01:00:00 ' , 'P1D ' , '2017-01-03 01:00:00 ' , 0 , "every + 1d, from 2017-01-01 01:00:00 (included) to 2017-01-03 01:00:00 " , '1) 2017-01-01 01:00:00%a2) 2017-01-02 01:00:00 ' ),
368
+ array ('2017-01-01 01:00:00 ' , 'P1D ' , 1 , 0 , 'every + 1d, from 2017-01-01 01:00:00 (included) for 2x ' , '1) 2017-01-01 01:00:00%a2) 2017-01-02 01:00:00 ' ),
369
+
370
+ array ('2017-01-01 ' , 'P1DT1H ' , '2017-01-03 ' , 0 , "every + 1d 01:00:00 $ ms, from 2017-01-01 00:00:00 (included) to 2017-01-03 00:00:00 " , '1) 2017-01-01 00:00:00%a2) 2017-01-02 01:00:00 ' ),
371
+ array ('2017-01-01 ' , 'P1DT1H ' , 1 , 0 , "every + 1d 01:00:00 $ ms, from 2017-01-01 00:00:00 (included) for 2x " , '1) 2017-01-01 00:00:00%a2) 2017-01-02 01:00:00 ' ),
372
+
373
+ array ('2017-01-01 ' , 'P1D ' , '2017-01-04 ' , \DatePeriod::EXCLUDE_START_DATE , 'every + 1d, from 2017-01-01 00:00:00 (excluded) to 2017-01-04 00:00:00 ' , '1) 2017-01-02%a2) 2017-01-03 ' ),
374
+ array ('2017-01-01 ' , 'P1D ' , 2 , \DatePeriod::EXCLUDE_START_DATE , 'every + 1d, from 2017-01-01 00:00:00 (excluded) for 2x ' , '1) 2017-01-02%a2) 2017-01-03 ' ),
375
+ );
376
+
377
+ if (\PHP_VERSION_ID < 70107 ) {
378
+ array_walk ($ periods , function (&$ i ) { $ i [5 ] = '' ; });
379
+ }
380
+
381
+ return $ periods ;
382
+ }
292
383
}
0 commit comments