Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement setTestNowAndTimezone() #2484

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions src/Carbon/Traits/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Carbon\Traits;

use Carbon\CarbonInterface;
use Closure;
use DateTimeImmutable;
use DateTimeInterface;

trait Test
{
Expand Down Expand Up @@ -55,6 +57,21 @@ public static function setTestNow($testNow = null)
static::$testNow = \is_string($testNow) ? static::parse($testNow) : $testNow;
}

public static function setTestNowAndTimezone($testNow = null, $tz = null)
{
$useDateInstanceTimezone = $testNow instanceof DateTimeInterface;

if ($useDateInstanceTimezone) {
date_default_timezone_set($testNow->getTimezone()->getName());
}

static::setTestNow($testNow);

if (!$useDateInstanceTimezone) {
date_default_timezone_set(static::getMockedTestNow(\func_num_args() === 1 ? null : $tz)->timezone);
}
}

/**
* Temporarily sets a static date to be used within the callback.
* Using setTestNow to set the date, executing the callback, then
Expand Down Expand Up @@ -98,27 +115,6 @@ public static function hasTestNow()
return static::getTestNow() !== null;
}

/**
* Return the given timezone and set it to the test instance if not null.
* If null, get the timezone from the test instance and return it.
*
* @param string|\DateTimeZone $tz
* @param \Carbon\CarbonInterface $testInstance
*
* @return string|\DateTimeZone
*/
protected static function handleMockTimezone($tz, &$testInstance)
{
//shift the time according to the given time zone
if ($tz !== null && $tz !== static::getMockedTestNow($tz)->getTimezone()) {
$testInstance = $testInstance->setTimezone($tz);

return $tz;
}

return $testInstance->getTimezone();
}

/**
* Get the mocked date passed in setTestNow() and if it's a Closure, execute it.
*
Expand All @@ -139,20 +135,22 @@ protected static function getMockedTestNow($tz)
}
/* @var \Carbon\CarbonImmutable|\Carbon\Carbon|null $testNow */

return $testNow;
return $testNow instanceof CarbonInterface
? $testNow->avoidMutation()->tz($tz)
: $testNow;
}

protected static function mockConstructorParameters(&$time, &$tz)
protected static function mockConstructorParameters(&$time, $tz)
{
/** @var \Carbon\CarbonImmutable|\Carbon\Carbon $testInstance */
$testInstance = clone static::getMockedTestNow($tz);

$tz = static::handleMockTimezone($tz, $testInstance);

if (static::hasRelativeKeywords($time)) {
$testInstance = $testInstance->modify($time);
}

$time = $testInstance instanceof self ? $testInstance->rawFormat(static::MOCK_DATETIME_FORMAT) : $testInstance->format(static::MOCK_DATETIME_FORMAT);
$time = $testInstance instanceof self
? $testInstance->rawFormat(static::MOCK_DATETIME_FORMAT)
: $testInstance->format(static::MOCK_DATETIME_FORMAT);
}
}
12 changes: 6 additions & 6 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ protected function setUp(): void
? CarbonImmutable::create(2017, 6, 27, 13, 14, 15, 'UTC')
: CarbonImmutable::now();

Carbon::setTestNow($this->now = $now);
CarbonImmutable::setTestNow($this->immutableNow = $immutableNow);
Carbon::setTestNowAndTimezone($this->now = $now);
CarbonImmutable::setTestNowAndTimezone($this->immutableNow = $immutableNow);

Carbon::useStrictMode(true);
CarbonImmutable::useStrictMode(true);
Expand Down Expand Up @@ -227,11 +227,11 @@ public function wrapWithTestNow(Closure $func, CarbonInterface $dt = null)
$test = Carbon::getTestNow();
$immutableTest = CarbonImmutable::getTestNow();
$dt = $dt ?: Carbon::now();
Carbon::setTestNow($dt);
CarbonImmutable::setTestNow($dt);
Carbon::setTestNowAndTimezone($dt);
CarbonImmutable::setTestNowAndTimezone($dt);
$func();
Carbon::setTestNow($test);
CarbonImmutable::setTestNow($immutableTest);
Carbon::setTestNowAndTimezone($test);
CarbonImmutable::setTestNowAndTimezone($immutableTest);
}

public function wrapWithNonDstDate(Closure $func)
Expand Down
2 changes: 1 addition & 1 deletion tests/Carbon/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function testIsBirthday()

// Birthday test can't work on February 29th
if ($dt->format('m-d') === '02-29') {
Carbon::setTestNow($dt->subDay());
Carbon::setTestNowAndTimezone($dt->subDay());
$dt = Carbon::now();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Carbon/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ public function testParseCreatesAnInstanceDefaultToNow()

public function testWithFancyString()
{
Carbon::setTestNow(Carbon::today());
Carbon::setTestNowAndTimezone(Carbon::today());
$c = new Carbon('first day of January 2008');
$this->assertCarbon($c, 2008, 1, 1, 0, 0, 0);
}

public function testParseWithFancyString()
{
Carbon::setTestNow(Carbon::today());
Carbon::setTestNowAndTimezone(Carbon::today());
$c = Carbon::parse('first day of January 2008');
$this->assertCarbon($c, 2008, 1, 1, 0, 0, 0);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Carbon/TestingAidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function testTimeZoneWithTestValueSet()

public function testCreateFromPartialFormat()
{
Carbon::setTestNow($now = Carbon::parse('2013-09-01 05:10:15', 'America/Vancouver'));
Carbon::setTestNowAndTimezone('2013-09-01 05:10:15 America/Vancouver', 'America/Vancouver');

// Simple partial time.
$this->assertSame('2018-05-06T05:10:15-07:00', Carbon::createFromFormat('Y-m-d', '2018-05-06')->toIso8601String());
Expand Down Expand Up @@ -244,7 +244,7 @@ public function testCreateFromPartialFormat()

public function testCreateFromPartialFormatWithMicroseconds()
{
Carbon::setTestNow($now = Carbon::parse('2013-09-01 05:10:15.123456', 'America/Vancouver'));
Carbon::setTestNowAndTimezone(Carbon::parse('2013-09-01 05:10:15.123456', 'America/Vancouver'));

// Set microseconds to zero to match behavior of DateTime::createFromFormat()
// See https://bugs.php.net/bug.php?id=74332
Expand Down
4 changes: 2 additions & 2 deletions tests/CarbonImmutable/TestingAidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function testTimeZoneWithTestValueSet()

public function testCreateFromPartialFormat()
{
Carbon::setTestNow($now = Carbon::parse('2013-09-01 05:10:15', 'America/Vancouver'));
Carbon::setTestNowAndTimezone(Carbon::parse('2013-09-01 05:10:15', 'America/Vancouver'));

// Simple partial time.
$this->assertSame('2018-05-06T05:10:15-07:00', Carbon::createFromFormat('Y-m-d', '2018-05-06')->toIso8601String());
Expand Down Expand Up @@ -253,7 +253,7 @@ public function testCreateFromPartialFormat()

public function testCreateFromPartialFormatWithMicroseconds()
{
Carbon::setTestNow($now = Carbon::parse('2013-09-01 05:10:15.123456', 'America/Vancouver'));
Carbon::setTestNowAndTimezone(Carbon::parse('2013-09-01 05:10:15.123456', 'America/Vancouver'));

// Set microseconds to zero to match behavior of DateTime::createFromFormat()
// See https://bugs.php.net/bug.php?id=74332
Expand Down
2 changes: 1 addition & 1 deletion tests/CarbonPeriod/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function testSetRelativeDates()

public function testFluentSetters()
{
Carbon::setTestNow(Carbon::now('UTC'));
Carbon::setTestNowAndTimezone(Carbon::now('UTC'));

$period = CarbonInterval::days(3)->toPeriod()->since('2018-04-21')->until('2018-04-27');
$dates = [];
Expand Down
4 changes: 2 additions & 2 deletions tests/CarbonPeriod/ToStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testToString($period, $expected)

public function provideToString(): Generator
{
Carbon::setTestNow(new Carbon('2015-09-01', 'America/Toronto'));
Carbon::setTestNowAndTimezone(new Carbon('2015-09-01', 'America/Toronto'));

yield [
CarbonPeriod::create('R4/2012-07-01T12:00:00/P7D'),
Expand Down Expand Up @@ -101,7 +101,7 @@ public function testToIso8601String($period, $expected)

public function provideToIso8601String(): Generator
{
Carbon::setTestNow(new Carbon('2015-09-01', 'America/Toronto'));
Carbon::setTestNowAndTimezone(new Carbon('2015-09-01', 'America/Toronto'));

yield [
CarbonPeriod::create('R4/2012-07-01T00:00:00-04:00/P7D'),
Expand Down
2 changes: 1 addition & 1 deletion tests/Factory/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testFactoryModification()

public function testFactoryTimezone()
{
Carbon::setTestNow(Carbon::parse('2020-09-04 03:39:04.123456', 'UTC'));
Carbon::setTestNowAndTimezone(Carbon::parse('2020-09-04 03:39:04.123456', 'UTC'));

$factory = new Factory();

Expand Down