Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor diffForHumansAt to its own class
- Loading branch information
1 parent
ba92625
commit 405b146
Showing
4 changed files
with
74 additions
and
47 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,34 @@ | ||
<?php | ||
|
||
namespace App\Macros; | ||
|
||
use Carbon\Carbon; | ||
|
||
class CarbonMacros | ||
{ | ||
public static function diffForHumansAt(Carbon $carbon) | ||
{ | ||
$diffDays = $carbon->diffInDays(now()->endOfDay()); | ||
switch ($diffDays) { | ||
case 0: | ||
$date = 'Today'; | ||
break; | ||
case 1: | ||
$date = 'Yesterday'; | ||
break; | ||
case 2: | ||
case 3: | ||
case 4: | ||
case 5: | ||
case 6: | ||
$date = "{$diffDays} days ago"; | ||
break; | ||
default: | ||
$date = $carbon->format('Y-m-d'); | ||
} | ||
|
||
$time = $carbon->format('H:i'); | ||
|
||
return $date.' at '.$time; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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 | ||
|
||
namespace Tests\Unit\Macros; | ||
|
||
use Illuminate\Support\Carbon; | ||
use Tests\TestCase; | ||
|
||
class CarbonMacrosTest extends TestCase | ||
{ | ||
const YEAR = 2019, MONTH = 12, DAY = 23, HOUR = 15, MINUTE = 29, SECOND = 1; | ||
|
||
public static $now; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
parent::setUpBeforeClass(); | ||
self::$now = Carbon::create(self::YEAR, self::MONTH, self::DAY, self::HOUR, self::MINUTE, self::SECOND); | ||
Carbon::setTestNow(self::$now); | ||
} | ||
|
||
public static function tearDownAfterClass(): void | ||
{ | ||
parent::tearDownAfterClass(); | ||
Carbon::setTestNow(); | ||
} | ||
|
||
/** @test */ | ||
function diff_time_for_humans_outputs_correctly() | ||
{ | ||
$date7DaysAgo = self::YEAR.'-'.self::MONTH.'-'.(self::DAY - 7); | ||
$time = self::HOUR.':'.self::MINUTE; | ||
|
||
$this->assertEquals("Today at {$time}", self::$now->diffForHumansAt()); | ||
$this->assertEquals("Yesterday at {$time}", self::$now->copy()->subDays(1)->diffForHumansAt()); | ||
$this->assertEquals("2 days ago at {$time}", self::$now->copy()->subDays(2)->diffForHumansAt()); | ||
$this->assertEquals("{$date7DaysAgo} at {$time}", self::$now->copy()->subDays(7)->diffForHumansAt()); | ||
} | ||
} |