Skip to content

Commit

Permalink
Making sure the testing instance is not modified when changing timezones
Browse files Browse the repository at this point in the history
This also prevents passing "null" down to setTimeZone(), which can
be problematic for certain environments
  • Loading branch information
lorenzo committed Jan 1, 2016
1 parent 1ec01fb commit e45b3c1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Chronos.php
Expand Up @@ -96,7 +96,7 @@ public function __construct($time = null, $tz = null)
}

if ($tz !== $testInstance->getTimezone()) {
$testInstance = $testInstance->setTimezone($tz);
$testInstance = $testInstance->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}

$time = $testInstance->format('Y-m-d H:i:s.u');
Expand Down
2 changes: 1 addition & 1 deletion src/Date.php
Expand Up @@ -73,7 +73,7 @@ public function __construct($time = null, $tz = null)
}

if ($tz !== $testInstance->getTimezone()) {
$testInstance = $testInstance->setTimezone($tz);
$testInstance = $testInstance->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}

$time = $testInstance->format('Y-m-d 00:00:00');
Expand Down
6 changes: 3 additions & 3 deletions src/MutableDate.php
Expand Up @@ -68,14 +68,14 @@ public function __construct($time = null, $tz = null)
return parent::__construct($time, $tz);
}

$testInstance = static::getTestNow();
$testInstance = clone static::getTestNow();
if ($relative) {
$testInstance = clone $testInstance;
$testInstance = $testInstance;
$testInstance = $testInstance->modify($time);
}

if ($tz !== $testInstance->getTimezone()) {
$testInstance = $testInstance->setTimezone($tz);
$testInstance = $testInstance->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}

$time = $testInstance->format('Y-m-d 00:00:00');
Expand Down
6 changes: 3 additions & 3 deletions src/MutableDateTime.php
Expand Up @@ -65,14 +65,14 @@ public function __construct($time = null, $tz = null)
return parent::__construct($time, $tz);
}

$testInstance = static::getTestNow();
$testInstance = clone static::getTestNow();
if ($relative) {
$testInstance = clone $testInstance;
$testInstance = $testInstance;
$testInstance = $testInstance->modify($time);
}

if ($tz !== $testInstance->getTimezone()) {
$testInstance = $testInstance->setTimezone($tz);
$testInstance = $testInstance->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}

$time = $testInstance->format('Y-m-d H:i:s.u');
Expand Down
16 changes: 16 additions & 0 deletions tests/DateTime/TestingAidsTest.php
Expand Up @@ -14,6 +14,7 @@
namespace Cake\Chronos\Test\DateTime;

use TestCase;
use DateTimeZone;

class TestingAidsTest extends TestCase
{
Expand Down Expand Up @@ -158,4 +159,19 @@ public function testTimeZoneWithTestValueSet($class)
$this->assertSame('2013-07-01T11:00:00-05:00', $class::parse('now', 'America/Mexico_City')->toIso8601String());
$this->assertSame('2013-07-01T09:00:00-07:00', $class::parse('now', 'America/Vancouver')->toIso8601String());
}

/**
* @dataProvider classNameProvider
* @return void
*/
public function testNullTimezone($class)
{
$c = new $class('2016-01-01 00:00:00', 'Europe/Copenhagen');
$class::setTestNow($c);

$result = new $class('now', null);
$this->assertEquals(new DateTimeZone('America/Toronto'), $result->tz);
$this->assertEquals('2015-12-31 18:00:00', $result->format('Y-m-d H:i:s'));
$this->assertEquals(new DateTimeZone('Europe/Copenhagen'), $class::getTestNow()->tz);
}
}
4 changes: 4 additions & 0 deletions tests/TestCase.php
Expand Up @@ -33,6 +33,10 @@ protected function setUp()
protected function tearDown()
{
date_default_timezone_set($this->saveTz);
MutableDateTime::setTestNow(null);
Chronos::setTestNow(null);
MutableDate::setTestNow(null);
Date::setTestNow(null);
}

public function classNameProvider()
Expand Down

0 comments on commit e45b3c1

Please sign in to comment.