Browse Source

Typehint on the interface instead of concrete classes.

This lets us cover any userland datetime types as well. Also fix #8146
for FrozenTime objects made from other FrozenTime objects.
Mark Story 10 years ago
parent
commit
9f6fd40968
4 changed files with 40 additions and 4 deletions
  1. 2 1
      src/I18n/FrozenTime.php
  2. 2 3
      src/I18n/Time.php
  3. 18 0
      tests/TestCase/I18n/DateTest.php
  4. 18 0
      tests/TestCase/I18n/TimeTest.php

+ 2 - 1
src/I18n/FrozenTime.php

@@ -17,6 +17,7 @@ namespace Cake\I18n;
 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');
         }

+ 2 - 3
src/I18n/Time.php

@@ -17,7 +17,7 @@ namespace Cake\I18n;
 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 @@ class Time extends MutableDateTime implements JsonSerializable
         if (is_numeric($time)) {
             $time = '@' . $time;
         }
-
         parent::__construct($time, $tz);
     }
 

+ 18 - 0
tests/TestCase/I18n/DateTest.php

@@ -65,6 +65,24 @@ class DateTest extends TestCase
     }
 
     /**
+     * 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
      *
      * @dataProvider classNameProvider

+ 18 - 0
tests/TestCase/I18n/TimeTest.php

@@ -78,6 +78,24 @@ class TimeTest extends TestCase
     }
 
     /**
+     * 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
      *
      * @return array