-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50fe510
commit cdc3cd1
Showing
5 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Facades; | ||
|
||
use App\Services\GreetingService; | ||
use Illuminate\Support\Facades\Facade; | ||
|
||
class Greeting extends Facade | ||
{ | ||
protected static function getFacadeAccessor(): string | ||
{ | ||
return GreetingService::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use Carbon\Carbon; | ||
|
||
class GreetingService | ||
{ | ||
public function auto($timezone = 'UTC'): string | ||
{ | ||
$currentTime = Carbon::now($timezone); | ||
$hour = $currentTime->hour; | ||
|
||
if ($hour < 12) { | ||
return __('Good morning'); | ||
} | ||
|
||
if ($hour < 18) { | ||
return __('Good afternoon'); | ||
} | ||
|
||
return __('Good evening'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
use App\Facades\Greeting; | ||
use Carbon\Carbon; | ||
|
||
it('returns Good morning', function () { | ||
Carbon::setTestNow(Carbon::createFromTime(8, 0, 0, 'UTC')); | ||
|
||
expect(Greeting::auto('UTC'))->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(); | ||
}); |