Skip to content
Permalink
Browse files

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.
  • Loading branch information...
markstory committed Feb 1, 2016
1 parent 36ce0d5 commit 9f6fd409682b60741d8e253c8233d9915770edf0
Showing 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
@@ -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');
}
@@ -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,15 +103,14 @@ 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');
}
if (is_numeric($time)) {
$time = '@' . $time;
}
parent::__construct($time, $tz);
}
@@ -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
*
@@ -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
*

0 comments on commit 9f6fd40

Please sign in to comment.
You can’t perform that action at this time.