Skip to content

Commit

Permalink
Merge pull request #245 from pableu/master
Browse files Browse the repository at this point in the history
Allow creating Chronos instances using `new` from existing Chronos instances
  • Loading branch information
markstory authored Feb 8, 2020
2 parents 253422d + e1fbdc3 commit e1044d2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public function __construct($time = 'now', $tz = null)
static::$_lastErrors = [];
$testNow = static::getTestNow();
if ($testNow === null) {
if ($time instanceof \DateTimeInterface) {
$time = $time->format(ChronosInterface::DEFAULT_TO_STRING_FORMAT);
}
parent::__construct($time ?? 'now', $tz);

return;
Expand Down
3 changes: 3 additions & 0 deletions src/MutableDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public function __construct($time = 'now', $tz = null)

$testNow = Chronos::getTestNow();
if ($testNow === null) {
if ($time instanceof \DateTimeInterface) {
$time = $time->format(ChronosInterface::DEFAULT_TO_STRING_FORMAT);
}
parent::__construct($time ?? 'now', $tz);

return;
Expand Down
31 changes: 31 additions & 0 deletions tests/Date/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,35 @@ public function testConstructWithLargeTimezoneChange($class)

date_default_timezone_set($savedTz);
}

/**
* @dataProvider dateClassProvider
*/
public function testCreateFromExistingInstance($class)
{
$existingClass = new $class();
self::assertInstanceOf($class, $existingClass);

$newClass = new $class($existingClass);
self::assertInstanceOf($class, $newClass);

self::assertEquals((string)$existingClass, (string)$newClass);
}

/**
* @dataProvider dateClassProvider
* @return void
*/
public function testCreateFromDateTimeInterface($class)
{
$existingClass = new \DateTimeImmutable();
$newClass = new $class($existingClass);
self::assertInstanceOf($class, $newClass);
self::assertEquals($existingClass->format('Y-m-d 00:00:00'), $newClass->format('Y-m-d H:i:s'));

$existingClass = new \DateTime();
$newClass = new $class($existingClass);
self::assertInstanceOf($class, $newClass);
self::assertEquals($existingClass->format('Y-m-d 00:00:00'), $newClass->format('Y-m-d H:i:s'));
}
}
31 changes: 31 additions & 0 deletions tests/DateTime/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,35 @@ public function testParseSettingTimezoneWithString($class)
$this->assertSame($timezone, $c->tzName);
$this->assertSame(9 + $dayLightSavingTimeOffset, $c->offsetHours);
}

/**
* @dataProvider classNameProvider
* @return void
*/
public function testCreateFromExistingInstance($class)
{
$existingClass = new $class();
self::assertInstanceOf($class, $existingClass);

$newClass = new $class($existingClass);
self::assertInstanceOf($class, $newClass);
self::assertEquals((string)$existingClass, (string)$newClass);
}

/**
* @dataProvider classNameProvider
* @return void
*/
public function testCreateFromDateTimeInterface($class)
{
$existingClass = new \DateTimeImmutable();
$newClass = new $class($existingClass);
self::assertInstanceOf($class, $newClass);
self::assertEquals($existingClass->format('Y-m-d H:i:s'), $newClass->format('Y-m-d H:i:s'));

$existingClass = new \DateTime();
$newClass = new $class($existingClass);
self::assertInstanceOf($class, $newClass);
self::assertEquals($existingClass->format('Y-m-d H:i:s'), $newClass->format('Y-m-d H:i:s'));
}
}

0 comments on commit e1044d2

Please sign in to comment.