Skip to content

Commit 65f7231

Browse files
authored
Fix DateTimeZone not properly converted to a string (#783)
1 parent 3c431dc commit 65f7231

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Sentry/Laravel/Features/ConsoleIntegration.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sentry\Laravel\Features;
44

5+
use DateTimeZone;
56
use Illuminate\Console\Application as ConsoleApplication;
67
use Illuminate\Console\Scheduling\Event as SchedulingEvent;
78
use Illuminate\Contracts\Cache\Factory as Cache;
@@ -90,11 +91,17 @@ private function startCheckIn(?string $slug, SchedulingEvent $scheduled, ?int $c
9091
$checkIn = $this->createCheckIn($checkInSlug, CheckInStatus::inProgress());
9192

9293
if ($updateMonitorConfig || $slug === null) {
94+
$timezone = $scheduled->timezone;
95+
96+
if ($timezone instanceof DateTimeZone) {
97+
$timezone = $timezone->getName();
98+
}
99+
93100
$checkIn->setMonitorConfig(new MonitorConfig(
94101
MonitorSchedule::crontab($scheduled->getExpression()),
95102
$checkInMargin,
96103
$maxRuntime,
97-
$scheduled->timezone
104+
$timezone
98105
));
99106
}
100107

test/Sentry/Features/ConsoleIntegrationTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sentry\Features;
44

5+
use DateTimeZone;
56
use Illuminate\Console\Scheduling\Schedule;
67
use RuntimeException;
78
use Sentry\Laravel\Tests\TestCase;
@@ -27,6 +28,34 @@ public function testScheduleMacro(): void
2728
$this->assertEquals('test-monitor', $finishCheckInEvent->getCheckIn()->getMonitorSlug());
2829
}
2930

31+
/**
32+
* When a timezone was defined on a command this would fail with:
33+
* Sentry\MonitorConfig::__construct(): Argument #4 ($timezone) must be of type ?string, DateTimeZone given
34+
* This test ensures that the timezone is properly converted to a string as expected.
35+
*/
36+
public function testScheduleMacroWithTimeZone(): void
37+
{
38+
$expectedTimezone = 'UTC';
39+
40+
/** @var Event $scheduledEvent */
41+
$scheduledEvent = $this->getScheduler()
42+
->call(function () {})
43+
->timezone(new DateTimeZone($expectedTimezone))
44+
->sentryMonitor('test-timezone-monitor');
45+
46+
$scheduledEvent->run($this->app);
47+
48+
// We expect a total of 2 events to be sent to Sentry:
49+
// 1. The start check-in event
50+
// 2. The finish check-in event
51+
$this->assertEquals(2, $this->getEventsCount());
52+
53+
$finishCheckInEvent = $this->getLastEvent();
54+
55+
$this->assertNotNull($finishCheckInEvent->getCheckIn());
56+
$this->assertEquals($expectedTimezone, $finishCheckInEvent->getCheckIn()->getMonitorConfig()->getTimezone());
57+
}
58+
3059
public function testScheduleMacroAutomaticSlug(): void
3160
{
3261
/** @var Event $scheduledEvent */

0 commit comments

Comments
 (0)