123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace fphammerle\helpers\tests;
- use fphammerle\helpers\DateTimeHelper;
- class DateTimeHelperTest extends \PHPUnit_Framework_TestCase
- {
- public function timestampToDateTimeProvider()
- {
- return [
- [null, null],
- [0, new \DateTime('1970-01-01 00:00:00', new \DateTimeZone('UTC'))],
- [0, new \DateTime('1970-01-01 01:00:00', new \DateTimeZone('Europe/Vienna'))],
- [1234567890, new \DateTime('2009-02-13 23:31:30', new \DateTimeZone('UTC'))],
- [1234567890, new \DateTime('2009-02-14 00:31:30', new \DateTimeZone('Europe/Vienna'))],
- [-3600, new \DateTime('1970-01-01 00:00:00', new \DateTimeZone('Europe/Vienna'))],
- ];
- }
- /**
- * @dataProvider timestampToDateTimeProvider
- */
- public function testTimestampToDateTime($timestamp, $expected_datetime)
- {
- $generated_datetime = DateTimeHelper::timestampToDateTime($timestamp);
- $this->assertEquals($expected_datetime, $generated_datetime);
- }
- public function timestampToDateTimeDefaultTimezoneProvider()
- {
- return [
- ['UTC', 100],
- ['Europe/Vienna', 0],
- ['Europe/Vienna', -100],
- ['Europe/Vienna', 100],
- ['Europe/London', 3600],
- ['US/Pacific', 3600],
- ];
- }
- /**
- * @dataProvider timestampToDateTimeDefaultTimezoneProvider
- */
- public function testTimestampToDateTimeDefaultTimezone($timezone, $timestamp)
- {
- date_default_timezone_set($timezone);
- $generated_datetime = DateTimeHelper::timestampToDateTime($timestamp);
- $this->assertSame($timestamp, $generated_datetime->getTimestamp());
- }
- public function parseProvider()
- {
- return [
- [null, 'UTC', null],
- [null, 'US/Pacific', null],
- ['2016-08-02', 'UTC', new \DatePeriod(
- new \DateTime('2016-08-02T00:00:00Z'),
- new \DateInterval('P1D'),
- new \DateTime('2016-08-03T00:00:00Z')
- )],
- ['2016-08-02', 'Europe/Vienna', new \DatePeriod(
- new \DateTime('2016-08-02T00:00:00+02:00'),
- new \DateInterval('P1D'),
- new \DateTime('2016-08-03T00:00:00+02:00')
- )],
- ['2016-08-02', 'Europe/Vienna', new \DatePeriod(
- new \DateTime('2016-08-01T22:00:00Z'),
- new \DateInterval('P1D'),
- new \DateTime('2016-08-02T22:00:00Z')
- )],
- ['2016-08-02 15:52:13', 'UTC', new \DatePeriod(
- new \DateTime('2016-08-02T15:52:13Z'),
- new \DateInterval('PT1S'),
- new \DateTime('2016-08-02T15:52:14Z')
- )],
- ['2016-08-02 15:52:13', 'Europe/Vienna', new \DatePeriod(
- new \DateTime('2016-08-02T15:52:13+02:00'),
- new \DateInterval('PT1S'),
- new \DateTime('2016-08-02T15:52:14+02:00')
- )],
- ['2016-08-02 15:52:13', 'Europe/Vienna', new \DatePeriod(
- new \DateTime('2016-08-02T13:52:13Z'),
- new \DateInterval('PT1S'),
- new \DateTime('2016-08-02T13:52:14Z')
- )],
- ['2016-08-02T15:52:13', 'US/Pacific', new \DatePeriod(
- new \DateTime('2016-08-02T15:52:13-07:00'),
- new \DateInterval('PT1S'),
- new \DateTime('2016-08-02T15:52:14-07:00')
- )],
- ];
- }
- /**
- * @dataProvider parseProvider
- */
- public function testParse($text, $timezone, $expected)
- {
- date_default_timezone_set($timezone);
- $this->assertEquals($expected, DateTimeHelper::parse($text));
- }
- public function parseInvalidArgumentProvider()
- {
- return [
- [' '],
- [''],
- ['2016--12'],
- ['2016-10-12 08:20#01'],
- [1],
- [false],
- ];
- }
- /**
- * @dataProvider parseInvalidArgumentProvider
- * @expectedException \InvalidArgumentException
- */
- public function testParseInvalidArgument($text)
- {
- DateTimeHelper::parse($text);
- }
- public function parseGetStartProvider()
- {
- return [
- [null, 'UTC', null],
- [null, 'US/Pacific', null],
- ['2016-08-02', 'UTC', new \DateTime('2016-08-02T00:00:00Z')],
- ['2016-08-02', 'Europe/Vienna', new \DateTime('2016-08-02T00:00:00+02:00')],
- ['2016-08-02', 'Europe/Vienna', new \DateTime('2016-08-01T22:00:00Z')],
- ['2016-08-02 15:52:13', 'UTC', new \DateTime('2016-08-02T15:52:13Z')],
- ['2016-08-02 15:52:13', 'Europe/Vienna', new \DateTime('2016-08-02T15:52:13+02:00')],
- ['2016-08-02 15:52:13', 'Europe/Vienna', new \DateTime('2016-08-02T13:52:13Z')],
- ['2016-08-02T15:52:13', 'US/Pacific', new \DateTime('2016-08-02T15:52:13-07:00')],
- ];
- }
- /**
- * @dataProvider parseGetStartProvider
- */
- public function testParseGetStart($text, $timezone, $expected)
- {
- date_default_timezone_set($timezone);
- $this->assertEquals($expected, DateTimeHelper::parseGetStart($text));
- }
- public function parseGetStartInvalidArgumentProvider()
- {
- return [
- [' '],
- [''],
- ['2016--12'],
- ['2016-10-12 08:20#01'],
- [1],
- [false],
- ];
- }
- /**
- * @dataProvider parseGetStartInvalidArgumentProvider
- * @expectedException \InvalidArgumentException
- */
- public function testParseGetStartInvalidArgument($text)
- {
- DateTimeHelper::parseGetStart($text);
- }
- }
|