From ad60e1cc9a452bec187d09a83669e72df803b33e Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 26 Jun 2023 11:43:41 +1000 Subject: [PATCH 1/2] Ensure captured time is in configured timezone --- src/Illuminate/Foundation/Console/Kernel.php | 2 ++ src/Illuminate/Foundation/Http/Kernel.php | 2 ++ .../Console/CommandDurationThresholdTest.php | 21 +++++++++++++++++++ .../Http/RequestDurationThresholdTest.php | 20 ++++++++++++++++++ 4 files changed, 45 insertions(+) diff --git a/src/Illuminate/Foundation/Console/Kernel.php b/src/Illuminate/Foundation/Console/Kernel.php index 334b3a401b43..5585c8767c52 100644 --- a/src/Illuminate/Foundation/Console/Kernel.php +++ b/src/Illuminate/Foundation/Console/Kernel.php @@ -218,6 +218,8 @@ public function terminate($input, $status) { $this->app->terminate(); + $this->commandStartedAt->setTimezone($this->app['config']->get('app.timezone', 'UTC')); + foreach ($this->commandLifecycleDurationHandlers as ['threshold' => $threshold, 'handler' => $handler]) { $end ??= Carbon::now(); diff --git a/src/Illuminate/Foundation/Http/Kernel.php b/src/Illuminate/Foundation/Http/Kernel.php index e91543cd0c77..784c3e98d6ea 100644 --- a/src/Illuminate/Foundation/Http/Kernel.php +++ b/src/Illuminate/Foundation/Http/Kernel.php @@ -214,6 +214,8 @@ public function terminate($request, $response) $this->app->terminate(); + $this->requestStartedAt->setTimezone($this->app['config']->get('app.timezone') ?? 'UTC'); + foreach ($this->requestLifecycleDurationHandlers as ['threshold' => $threshold, 'handler' => $handler]) { $end ??= Carbon::now(); diff --git a/tests/Integration/Console/CommandDurationThresholdTest.php b/tests/Integration/Console/CommandDurationThresholdTest.php index 3c4aaa6b825a..939264e8b40e 100644 --- a/tests/Integration/Console/CommandDurationThresholdTest.php +++ b/tests/Integration/Console/CommandDurationThresholdTest.php @@ -5,6 +5,7 @@ use Carbon\CarbonInterval; use Illuminate\Contracts\Console\Kernel; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Config; use Orchestra\Testbench\TestCase; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\ConsoleOutput; @@ -177,4 +178,24 @@ public function testItClearsStartTimeAfterHandlingCommand() $kernel->terminate($input, 21); $this->assertNull($kernel->commandStartedAt()); } + + public function testUsesTheConfiguredDateTimezone() + { + Config::set('app.timezone', 'UTC'); + $startedAt = null; + $kernel = $this->app[Kernel::class]; + $kernel->command('foo', fn () => null); + $kernel->whenCommandLifecycleIsLongerThan(0, function ($started) use (&$startedAt) { + $startedAt = $started; + }); + + Config::set('app.timezone', 'Australia/Melbourne'); + Carbon::setTestNow(Carbon::now()); + $kernel->handle($input = new StringInput('foo'), new ConsoleOutput); + + Carbon::setTestNow(now()->addMinute()); + $kernel->terminate($input, 21); + + $this->assertSame('Australia/Melbourne', $startedAt->timezone->getName()); + } } diff --git a/tests/Integration/Http/RequestDurationThresholdTest.php b/tests/Integration/Http/RequestDurationThresholdTest.php index 2a9f4fd4ac11..421981dda0eb 100644 --- a/tests/Integration/Http/RequestDurationThresholdTest.php +++ b/tests/Integration/Http/RequestDurationThresholdTest.php @@ -7,6 +7,7 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Route; use Orchestra\Testbench\TestCase; @@ -72,6 +73,25 @@ public function testItProvidesRequestToHandler() $this->assertSame('http://localhost/test-route', $url); } + public function testUsesTheConfiguredDateTimezone() + { + Config::set('app.timezone', 'UTC'); + Route::get('test-route', fn () => 'ok'); + $kernel = $this->app[Kernel::class]; + $startedAt = null; + $kernel->whenRequestLifecycleIsLongerThan(CarbonInterval::seconds(1), function ($started) use (&$startedAt) { + $startedAt = $started; + }); + + Config::set('app.timezone', 'Australia/Melbourne'); + Carbon::setTestNow(now()->startOfDay()); + $kernel->handle($request = Request::create('http://localhost/test-route')); + Carbon::setTestNow(now()->addMinute()); + $kernel->terminate($request, new Response); + + $this->assertSame('Australia/Melbourne', $startedAt->timezone->getName()); + } + public function testItCanExceedThresholdWhenSpecifyingDurationAsMilliseconds() { Route::get('test-route', fn () => 'ok'); From fda986ef9db64d232b2c82ec981dbadcfa5fc347 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 26 Jun 2023 20:57:19 +1000 Subject: [PATCH 2/2] Update Kernel.php --- src/Illuminate/Foundation/Console/Kernel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Console/Kernel.php b/src/Illuminate/Foundation/Console/Kernel.php index 5585c8767c52..8180435b5869 100644 --- a/src/Illuminate/Foundation/Console/Kernel.php +++ b/src/Illuminate/Foundation/Console/Kernel.php @@ -218,7 +218,7 @@ public function terminate($input, $status) { $this->app->terminate(); - $this->commandStartedAt->setTimezone($this->app['config']->get('app.timezone', 'UTC')); + $this->commandStartedAt->setTimezone($this->app['config']->get('app.timezone') ?? 'UTC'); foreach ($this->commandLifecycleDurationHandlers as ['threshold' => $threshold, 'handler' => $handler]) { $end ??= Carbon::now();