diff --git a/app/Facades/Greeting.php b/app/Facades/Greeting.php new file mode 100644 index 00000000..0fdeda05 --- /dev/null +++ b/app/Facades/Greeting.php @@ -0,0 +1,14 @@ +app->singleton(GreetingService::class, function ($app) { + return new GreetingService; + }); + + $this->app->alias(GreetingService::class, 'Greeting'); } public function boot(): void diff --git a/app/Services/GreetingService.php b/app/Services/GreetingService.php new file mode 100644 index 00000000..fb80e14a --- /dev/null +++ b/app/Services/GreetingService.php @@ -0,0 +1,24 @@ +hour; + + if ($hour < 12) { + return __('Good morning'); + } + + if ($hour < 18) { + return __('Good afternoon'); + } + + return __('Good evening'); + } +} diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index bd72bbe1..d9f32a13 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -17,7 +17,7 @@
{{ trans_choice(':count backup task has|:count backup tasks have', Auth::user()->backupTasklogCountToday(), ['count' => Auth::user()->backupTasklogCountToday()]) }} {{ __('been run today.') }} diff --git a/tests/Unit/Services/GreetingTest.php b/tests/Unit/Services/GreetingTest.php new file mode 100644 index 00000000..f8314b38 --- /dev/null +++ b/tests/Unit/Services/GreetingTest.php @@ -0,0 +1,38 @@ +toBe('Good morning'); + + Carbon::setTestNow(); +}); + +it('returns Good afternoon', function () { + Carbon::setTestNow(Carbon::createFromTime(14, 0, 0, 'UTC')); + + expect(Greeting::auto('UTC'))->toBe('Good afternoon'); + + Carbon::setTestNow(); +}); + +it('returns Good evening', function () { + Carbon::setTestNow(Carbon::createFromTime(20, 0, 0, 'UTC')); + + expect(Greeting::auto('UTC'))->toBe('Good evening'); + + Carbon::setTestNow(); +}); + +it('handles different timezones correctly', function () { + Carbon::setTestNow(Carbon::createFromTime(8, 0, 0, 'UTC')); + + expect(Greeting::auto('Europe/Berlin'))->toBe('Good morning') + ->and(Greeting::auto('Asia/Shanghai'))->toBe('Good afternoon') + ->and(Greeting::auto('America/New_York'))->toBe('Good morning'); + + Carbon::setTestNow(); +});