From a987a30517c3ab50924bacd2bc55f036f4c7d9c7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 21 Oct 2023 17:09:30 +0200 Subject: [PATCH] Use mockable Carbon time helper --- .../Actions/CreatesNewMarkdownPostFile.php | 5 ++--- .../tests/Unit/CreatesNewMarkdownPostFileTest.php | 15 +++++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php b/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php index c59383eed74..869b6b381d0 100644 --- a/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php +++ b/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php @@ -7,10 +7,9 @@ use Hyde\Framework\Exceptions\FileConflictException; use Hyde\Facades\Filesystem; use Hyde\Pages\MarkdownPost; +use Illuminate\Support\Carbon; use Illuminate\Support\Str; -use function date; - /** * Offloads logic for the make:post command. * @@ -44,7 +43,7 @@ public function __construct(string $title, ?string $description, ?string $catego $this->category = $category ?? 'blog'; $this->author = $author ?? 'default'; - $this->date = date('Y-m-d H:i'); + $this->date = Carbon::now()->format('Y-m-d H:i'); $this->identifier = Str::slug($title); } diff --git a/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php b/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php index 0c72f12c226..6386d7913ad 100644 --- a/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php +++ b/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php @@ -6,6 +6,7 @@ use Hyde\Testing\TestCase; use Illuminate\Support\Str; +use Illuminate\Support\Carbon; use Hyde\Framework\Actions\CreatesNewMarkdownPostFile; /** @@ -17,35 +18,33 @@ class CreatesNewMarkdownPostFileTest extends TestCase { public function testWithDefaultData() { + Carbon::setTestNow(Carbon::create(2024)); + $action = new CreatesNewMarkdownPostFile('Example Title', null, null, null); $array = $action->toArray(); - // Truncate date as it can cause tests to fail when the clock switches over a second - $array['date'] = Str::before($array['date'], ' '); - $this->assertSame([ 'title' => 'Example Title', 'description' => 'A short description used in previews and SEO', 'category' => 'blog', 'author' => 'default', - 'date' => date('Y-m-d'), + 'date' => '2024-01-01 00:00', ], $array); } public function testWithCustomData() { + Carbon::setTestNow(Carbon::create(2024)); + $action = new CreatesNewMarkdownPostFile('foo', 'bar', 'baz', 'qux'); $array = $action->toArray(); - // Truncate date as it can cause tests to fail when the clock switches over a second - $array['date'] = Str::before($array['date'], ' '); - $this->assertSame([ 'title' => 'foo', 'description' => 'bar', 'category' => 'baz', 'author' => 'qux', - 'date' => date('Y-m-d'), + 'date' => '2024-01-01 00:00', ], $array); } }