diff --git a/src/I18n/FrozenTime.php b/src/I18n/FrozenTime.php index 9e8cb7ebe8f..387b8a73978 100644 --- a/src/I18n/FrozenTime.php +++ b/src/I18n/FrozenTime.php @@ -17,6 +17,7 @@ use Cake\Chronos\Chronos; use Cake\Chronos\ChronosInterface; use DateTime; +use DateTimeInterface; use DateTimeZone; use IntlDateFormatter; use JsonSerializable; @@ -103,7 +104,7 @@ class FrozenTime extends Chronos implements JsonSerializable */ public function __construct($time = null, $tz = null) { - if ($time instanceof DateTime) { + if ($time instanceof DateTimeInterface) { $tz = $time->getTimeZone(); $time = $time->format('Y-m-d H:i:s'); } diff --git a/src/I18n/Time.php b/src/I18n/Time.php index 2ed8f35c8a1..30c0e7eadc1 100644 --- a/src/I18n/Time.php +++ b/src/I18n/Time.php @@ -17,7 +17,7 @@ use Cake\Chronos\ChronosInterface; use Cake\Chronos\MutableDateTime; use DateTime; -use DateTimeImmutable; +use DateTimeInterface; use DateTimeZone; use IntlDateFormatter; use JsonSerializable; @@ -103,7 +103,7 @@ class Time extends MutableDateTime implements JsonSerializable */ public function __construct($time = null, $tz = null) { - if ($time instanceof DateTime || $time instanceof DateTimeImmutable) { + if ($time instanceof DateTimeInterface) { $tz = $time->getTimeZone(); $time = $time->format('Y-m-d H:i:s'); } @@ -111,7 +111,6 @@ public function __construct($time = null, $tz = null) if (is_numeric($time)) { $time = '@' . $time; } - parent::__construct($time, $tz); } diff --git a/tests/TestCase/I18n/DateTest.php b/tests/TestCase/I18n/DateTest.php index 283c60b8f91..085515942e4 100644 --- a/tests/TestCase/I18n/DateTest.php +++ b/tests/TestCase/I18n/DateTest.php @@ -64,6 +64,24 @@ public static function classNameProvider() return ['mutable' => ['Cake\I18n\Date'], 'immutable' => ['Cake\I18n\FrozenDate']]; } + /** + * Ensure that instances can be built from other objects. + * + * @dataProvider classNameProvider + * @return void + */ + public function testConstructFromAnotherInstance($class) + { + $time = '2015-01-22'; + $frozen = new FrozenDate($time, 'America/Chicago'); + $subject = new $class($frozen); + $this->assertEquals($time, $subject->format('Y-m-d'), 'frozen date construction'); + + $mut = new Date($time, 'America/Chicago'); + $subject = new $class($mut); + $this->assertEquals($time, $subject->format('Y-m-d'), 'mutable date construction'); + } + /** * test formatting dates taking in account preferred i18n locale file * diff --git a/tests/TestCase/I18n/TimeTest.php b/tests/TestCase/I18n/TimeTest.php index 656f0da64ef..e460b25748d 100644 --- a/tests/TestCase/I18n/TimeTest.php +++ b/tests/TestCase/I18n/TimeTest.php @@ -77,6 +77,24 @@ public static function classNameProvider() return ['mutable' => ['Cake\I18n\Time'], 'immutable' => ['Cake\I18n\FrozenTime']]; } + /** + * Ensure that instances can be built from other objects. + * + * @dataProvider classNameProvider + * @return void + */ + public function testConstructFromAnotherInstance($class) + { + $time = '2015-01-22 10:33:44'; + $frozen = new FrozenTime($time, 'America/Chicago'); + $subject = new $class($frozen); + $this->assertEquals($time, $subject->format('Y-m-d H:i:s'), 'frozen time construction'); + + $mut = new Time($time, 'America/Chicago'); + $subject = new $class($mut); + $this->assertEquals($time, $subject->format('Y-m-d H:i:s'), 'mutable time construction'); + } + /** * provider for timeAgoInWords() tests *