Skip to content

Commit

Permalink
[10.x] Ensure captured time is in configured timezone (#47567)
Browse files Browse the repository at this point in the history
* Ensure captured time is in configured timezone

* Update Kernel.php
  • Loading branch information
timacdonald authored Jun 26, 2023
1 parent 3558da8 commit be745b8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
21 changes: 21 additions & 0 deletions tests/Integration/Console/CommandDurationThresholdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
20 changes: 20 additions & 0 deletions tests/Integration/Http/RequestDurationThresholdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit be745b8

Please sign in to comment.