Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 9f6fd40
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/I18n/FrozenTime.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Chronos\Chronos;
use Cake\Chronos\ChronosInterface;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
use IntlDateFormatter;
use JsonSerializable;
Expand Down Expand Up @@ -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');
}
Expand Down
5 changes: 2 additions & 3 deletions src/I18n/Time.php
Expand Up @@ -17,7 +17,7 @@
use Cake\Chronos\ChronosInterface;
use Cake\Chronos\MutableDateTime;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use IntlDateFormatter;
use JsonSerializable;
Expand Down Expand Up @@ -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);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/I18n/DateTest.php
Expand Up @@ -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
*
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/I18n/TimeTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 9f6fd40

Please sign in to comment.