From a457ac630e19809f07f66dfddf979c32e7a38ba2 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sun, 31 Oct 2021 21:07:43 +0100 Subject: [PATCH 1/4] Implement setTestNowAndTimezone() --- src/Carbon/Traits/Creator.php | 3 +++ src/Carbon/Traits/Test.php | 27 ++++++++++++++++++++--- tests/AbstractTestCase.php | 12 +++++----- tests/Carbon/ComparisonTest.php | 2 +- tests/Carbon/ConstructTest.php | 4 ++-- tests/Carbon/TestingAidsTest.php | 4 ++-- tests/CarbonImmutable/TestingAidsTest.php | 4 ++-- tests/CarbonPeriod/SettersTest.php | 2 +- tests/CarbonPeriod/ToStringTest.php | 4 ++-- tests/Factory/FactoryTest.php | 2 +- 10 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/Carbon/Traits/Creator.php b/src/Carbon/Traits/Creator.php index f90195a018..b7f4827daa 100644 --- a/src/Carbon/Traits/Creator.php +++ b/src/Carbon/Traits/Creator.php @@ -84,6 +84,9 @@ public function __construct($time = null, $tz = null) } try { + if (isset($GLOBALS['debug'])) { + var_dump($tz); + } parent::__construct($time ?: 'now', static::safeCreateDateTimeZone($tz) ?: null); } catch (Exception $exception) { throw new InvalidFormatException($exception->getMessage(), 0, $exception); diff --git a/src/Carbon/Traits/Test.php b/src/Carbon/Traits/Test.php index ba82d89a91..6bdcf5e729 100644 --- a/src/Carbon/Traits/Test.php +++ b/src/Carbon/Traits/Test.php @@ -11,8 +11,10 @@ namespace Carbon\Traits; +use Carbon\CarbonInterface; use Closure; use DateTimeImmutable; +use DateTimeInterface; trait Test { @@ -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 @@ -139,7 +156,9 @@ 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) @@ -147,12 +166,14 @@ protected static function mockConstructorParameters(&$time, &$tz) /** @var \Carbon\CarbonImmutable|\Carbon\Carbon $testInstance */ $testInstance = clone static::getMockedTestNow($tz); - $tz = static::handleMockTimezone($tz, $testInstance); + // 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); } } diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 6372c7971d..552dc92711 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -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); @@ -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) diff --git a/tests/Carbon/ComparisonTest.php b/tests/Carbon/ComparisonTest.php index fb0f2bf927..88579946b3 100644 --- a/tests/Carbon/ComparisonTest.php +++ b/tests/Carbon/ComparisonTest.php @@ -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(); } diff --git a/tests/Carbon/ConstructTest.php b/tests/Carbon/ConstructTest.php index cece7f67a4..390f879ae7 100644 --- a/tests/Carbon/ConstructTest.php +++ b/tests/Carbon/ConstructTest.php @@ -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); } diff --git a/tests/Carbon/TestingAidsTest.php b/tests/Carbon/TestingAidsTest.php index 78d7ac52a8..df6e040e8f 100644 --- a/tests/Carbon/TestingAidsTest.php +++ b/tests/Carbon/TestingAidsTest.php @@ -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(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()); @@ -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 diff --git a/tests/CarbonImmutable/TestingAidsTest.php b/tests/CarbonImmutable/TestingAidsTest.php index cc70b745ed..1a873393c5 100644 --- a/tests/CarbonImmutable/TestingAidsTest.php +++ b/tests/CarbonImmutable/TestingAidsTest.php @@ -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()); @@ -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 diff --git a/tests/CarbonPeriod/SettersTest.php b/tests/CarbonPeriod/SettersTest.php index c11d5da9d4..c84865fc5c 100644 --- a/tests/CarbonPeriod/SettersTest.php +++ b/tests/CarbonPeriod/SettersTest.php @@ -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 = []; diff --git a/tests/CarbonPeriod/ToStringTest.php b/tests/CarbonPeriod/ToStringTest.php index 7b3d630279..85aa9e140f 100644 --- a/tests/CarbonPeriod/ToStringTest.php +++ b/tests/CarbonPeriod/ToStringTest.php @@ -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'), @@ -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'), diff --git a/tests/Factory/FactoryTest.php b/tests/Factory/FactoryTest.php index 54ecc2b095..33b428d909 100644 --- a/tests/Factory/FactoryTest.php +++ b/tests/Factory/FactoryTest.php @@ -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(); From 539ca6e7acb7fa92a4683be6cc0ec6ebeb1bed70 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sun, 31 Oct 2021 22:13:51 +0100 Subject: [PATCH 2/4] Add usage of setTestNowAndTimezone with string --- src/Carbon/Traits/Creator.php | 3 --- src/Carbon/Traits/Test.php | 2 +- tests/Carbon/TestingAidsTest.php | 2 +- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Carbon/Traits/Creator.php b/src/Carbon/Traits/Creator.php index b7f4827daa..f90195a018 100644 --- a/src/Carbon/Traits/Creator.php +++ b/src/Carbon/Traits/Creator.php @@ -84,9 +84,6 @@ public function __construct($time = null, $tz = null) } try { - if (isset($GLOBALS['debug'])) { - var_dump($tz); - } parent::__construct($time ?: 'now', static::safeCreateDateTimeZone($tz) ?: null); } catch (Exception $exception) { throw new InvalidFormatException($exception->getMessage(), 0, $exception); diff --git a/src/Carbon/Traits/Test.php b/src/Carbon/Traits/Test.php index 6bdcf5e729..b4d4e2a7c4 100644 --- a/src/Carbon/Traits/Test.php +++ b/src/Carbon/Traits/Test.php @@ -68,7 +68,7 @@ public static function setTestNowAndTimezone($testNow = null, $tz = null) static::setTestNow($testNow); if (!$useDateInstanceTimezone) { - date_default_timezone_set(static::getMockedTestNow(func_num_args() === 1 ? null : $tz)->timezone); + date_default_timezone_set(static::getMockedTestNow(\func_num_args() === 1 ? null : $tz)->timezone); } } diff --git a/tests/Carbon/TestingAidsTest.php b/tests/Carbon/TestingAidsTest.php index df6e040e8f..a7e19fe6ff 100644 --- a/tests/Carbon/TestingAidsTest.php +++ b/tests/Carbon/TestingAidsTest.php @@ -201,7 +201,7 @@ public function testTimeZoneWithTestValueSet() public function testCreateFromPartialFormat() { - Carbon::setTestNowAndTimezone(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()); From a785c724a44c71b8db984320b246d95b12a25fe4 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Mon, 1 Nov 2021 16:08:03 +0100 Subject: [PATCH 3/4] Remove no longer needed method handleMockTimezone --- src/Carbon/Traits/Test.php | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/Carbon/Traits/Test.php b/src/Carbon/Traits/Test.php index b4d4e2a7c4..57ef3fe890 100644 --- a/src/Carbon/Traits/Test.php +++ b/src/Carbon/Traits/Test.php @@ -115,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. * @@ -166,8 +145,6 @@ protected static function mockConstructorParameters(&$time, &$tz) /** @var \Carbon\CarbonImmutable|\Carbon\Carbon $testInstance */ $testInstance = clone static::getMockedTestNow($tz); - // static::handleMockTimezone($tz, $testInstance); - if (static::hasRelativeKeywords($time)) { $testInstance = $testInstance->modify($time); } From a4ecf41bc3ff18510aa9d1193b30b732dfe767f1 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Mon, 1 Nov 2021 17:00:19 +0100 Subject: [PATCH 4/4] Remove unneeded reference for $tz --- src/Carbon/Traits/Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Carbon/Traits/Test.php b/src/Carbon/Traits/Test.php index 57ef3fe890..1287ed84d8 100644 --- a/src/Carbon/Traits/Test.php +++ b/src/Carbon/Traits/Test.php @@ -140,7 +140,7 @@ protected static function getMockedTestNow($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);