Skip to content

Commit

Permalink
Use mockable Carbon time helper
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Oct 21, 2023
1 parent 28713cb commit a987a30
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}

Expand Down
15 changes: 7 additions & 8 deletions packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Hyde\Testing\TestCase;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;

/**
Expand All @@ -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);
}
}

0 comments on commit a987a30

Please sign in to comment.