From 25918c6509e6b6a1db9e88d190cff46380803cef Mon Sep 17 00:00:00 2001 From: Osvaldo Jiang Date: Wed, 6 May 2020 11:15:43 -0300 Subject: [PATCH] Throw an exception when the date cannot be parsed --- src/DateTime.php | 3 + tests/0-ParsersTest.php | 135 +++++++++++++++++++++------------------- 2 files changed, 74 insertions(+), 64 deletions(-) diff --git a/src/DateTime.php b/src/DateTime.php index 2b7e107..82bdbf1 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -149,6 +149,9 @@ public static function datetimeParse($time='now', $timezone=null) : PhpDateTime $obj = date_create( $time ); } } + if ($obj === false) { + throw new \RuntimeException('Not a valid date'); + } return $obj; } diff --git a/tests/0-ParsersTest.php b/tests/0-ParsersTest.php index 4d896b8..5f14133 100644 --- a/tests/0-ParsersTest.php +++ b/tests/0-ParsersTest.php @@ -1,84 +1,91 @@ assertInstanceOf( DateTimeZone::class, $tz ); - $this->assertEquals( $offset, $tz->getOffset( $date ) ); +class ParsersTest extends TestCase +{ + /** + * Parses timezone parsing + */ + public function testTimezoneParse() + { + $rows = [ + 'America/Argentina/Buenos_Aires', + '-0300', + '-03:00', + -3, + '-3', + ]; + $offset = -10800; + $date = new PhpDateTime; + date_default_timezone_set($rows[0]); + foreach($rows as $row ) { + $tz = DateTime::timezoneParse( $row ); + $this->assertInstanceOf( DateTimeZone::class, $tz ); + $this->assertEquals( $offset, $tz->getOffset( $date ) ); + } } - } - - - /** - * Performs datetime parsing - */ - public function testDatetimeParse() { - $timestamp = 946684800; - $rows = [ - $timestamp, - '2000-01-01T00:00:00+00:00', - '2000-01-01 00:00:00 UTC', - '01/01/2000 UTC', - ]; - $php_obj = new PhpDateTime; - $php_obj->setTimestamp( $timestamp ); - $formatted = $php_obj->format('c'); - $tz = $php_obj->getTimezone(); - foreach($rows as $row) { - $obj = DateTime::datetimeParse( $row , $tz); - $obj->setTimezone($tz); - $this->assertInstanceOf( PhpDateTime::class, $obj ); - $this->assertEquals( $formatted, $obj->format('c') ); + /** + * Performs datetime parsing + */ + public function testDatetimeParse() + { + $timestamp = 946684800; + $rows = [ + $timestamp, + '2000-01-01T00:00:00+00:00', + '2000-01-01 00:00:00 UTC', + '01/01/2000 UTC', + ]; + + $php_obj = new PhpDateTime; + $php_obj->setTimestamp( $timestamp ); + $formatted = $php_obj->format('c'); + + $tz = $php_obj->getTimezone(); + foreach($rows as $row) { + $obj = DateTime::datetimeParse( $row , $tz); + $obj->setTimezone($tz); + $this->assertInstanceOf( PhpDateTime::class, $obj ); + $this->assertEquals( $formatted, $obj->format('c') ); + } } - } - /** - * Performs interval parsing - */ - public function testIntervalParse() { - $rows = [ - 'P10DT1H', - '+10 days, 1 hour', - ]; + /** + * Performs interval parsing + */ + public function testIntervalParse() + { + $rows = [ + 'P10DT1H', + '+10 days, 1 hour', + ]; - foreach($rows as $row) { - $obj = DateTime::intervalParse( $row ); - $this->assertInstanceOf( DateInterval::class, $obj ); - $this->assertEquals( '00-00-10 01:00:00', $obj->format('%Y-%M-%D %H:%I:%S') ); + foreach($rows as $row) { + $obj = DateTime::intervalParse( $row ); + $this->assertInstanceOf( DateInterval::class, $obj ); + $this->assertEquals( '00-00-10 01:00:00', $obj->format('%Y-%M-%D %H:%I:%S') ); + } } - } - + /** + * Test failed parse + */ + public function testFailedParse() + { + $this->expectException( \RuntimeException::class ); + $obj = new DateTime('non valid'); + $out = $obj->format('Y-m-d'); + } } -