Skip to content

Commit

Permalink
Merge pull request #285 from sshead/fix-with-use-carbon-immutable
Browse files Browse the repository at this point in the history
Fix error when app is set to use CarbonImmutable
  • Loading branch information
ash-jc-allen authored Jun 14, 2024
2 parents c7d5a5e + a3643a8 commit 48a55af
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Classes/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ private function setOptions(): void
}

if (! $this->activateAt) {
$this->activateAt = now();
$this->activateAt = Carbon::now();
}

$this->setTrackingOptions();
Expand Down
10 changes: 5 additions & 5 deletions src/Models/ShortURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace AshAllenDesign\ShortURL\Models;

use Carbon\Carbon;
use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -27,10 +27,10 @@
* @property bool $track_browser_version
* @property bool $track_referer_url
* @property bool $track_device_type
* @property Carbon $activated_at
* @property Carbon|null $deactivated_at
* @property Carbon $created_at
* @property Carbon $updated_at
* @property CarbonInterface $activated_at
* @property CarbonInterface|null $deactivated_at
* @property CarbonInterface $created_at
* @property CarbonInterface $updated_at
*/
class ShortURL extends Model
{
Expand Down
8 changes: 4 additions & 4 deletions src/Models/ShortURLVisit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace AshAllenDesign\ShortURL\Models;

use Carbon\Carbon;
use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -19,10 +19,10 @@
* @property string $browser
* @property string $browser_version
* @property string $device_type
* @property Carbon $visited_at
* @property CarbonInterface $visited_at
* @property string $referer_url
* @property Carbon $created_at
* @property Carbon $updated_at
* @property CarbonInterface $created_at
* @property CarbonInterface $updated_at
*/
class ShortURLVisit extends Model
{
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Classes/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use AshAllenDesign\ShortURL\Exceptions\ValidationException;
use AshAllenDesign\ShortURL\Models\ShortURL;
use AshAllenDesign\ShortURL\Tests\Unit\TestCase;
use Carbon\CarbonImmutable;
use Hashids\Hashids;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Date;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use ShortURL as ShortURLAlias;
Expand Down Expand Up @@ -593,4 +595,18 @@ public function short_url_can_be_created_using_the_url_key_if_the_key_and_seeder

$this->assertSame('https://short-url.com/short/abc123', $shortUrl->default_short_url);
}

#[Test]
public function builder_works_when_the_date_facade_is_set_to_use_carbon_immutable(): void
{
Date::use(CarbonImmutable::class);

$shortUrl = app(Builder::class)
->destinationUrl('https://domain.com')
->make();

$this->assertInstanceOf(CarbonImmutable::class, $shortUrl->activated_at);

Date::useDefault();
}
}
25 changes: 25 additions & 0 deletions tests/Unit/Models/ShortURL/CastsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use AshAllenDesign\ShortURL\Models\ShortURL;
use AshAllenDesign\ShortURL\Tests\Unit\TestCase;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;
use PHPUnit\Framework\Attributes\Test;

final class CastsTest extends TestCase
Expand All @@ -30,6 +32,29 @@ public function carbon_date_objects_are_returned(): void
$this->assertInstanceOf(Carbon::class, $shortUrl->updated_at);
}

#[Test]
public function carbon_immutable_date_objects_are_returned_when_the_date_facade_is_set_to_use_carbon_immutable(): void
{
Date::use(CarbonImmutable::class);

$shortUrl = ShortURL::factory()
->create([
'activated_at' => now(),
'deactivated_at' => now(),
'created_at' => now(),
'updated_at' => now(),
]);

$shortUrl->refresh();

$this->assertInstanceOf(CarbonImmutable::class, $shortUrl->activated_at);
$this->assertInstanceOf(CarbonImmutable::class, $shortUrl->deactivated_at);
$this->assertInstanceOf(CarbonImmutable::class, $shortUrl->created_at);
$this->assertInstanceOf(CarbonImmutable::class, $shortUrl->updated_at);

Date::useDefault();
}

#[Test]
public function forward_query_params_is_casted_correctly(): void
{
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Models/ShortURLVisit/CastsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use AshAllenDesign\ShortURL\Models\ShortURLVisit;
use AshAllenDesign\ShortURL\Tests\Unit\TestCase;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;
use PHPUnit\Framework\Attributes\Test;

final class CastsTest extends TestCase
Expand All @@ -29,4 +31,26 @@ public function carbon_date_objects_are_returned(): void
$this->assertInstanceOf(Carbon::class, $shortUrlVisit->created_at);
$this->assertInstanceOf(Carbon::class, $shortUrlVisit->updated_at);
}

#[Test]
public function carbon_immutable_date_objects_are_returned_when_the_date_facade_is_set_to_use_carbon_immutable(): void
{
Date::use(CarbonImmutable::class);

$shortUrlVisit = ShortURLVisit::factory()
->for(ShortURL::factory())
->create([
'visited_at' => now(),
'created_at' => now(),
'updated_at' => now(),
]);

$shortUrlVisit->refresh();

$this->assertInstanceOf(CarbonImmutable::class, $shortUrlVisit->visited_at);
$this->assertInstanceOf(CarbonImmutable::class, $shortUrlVisit->created_at);
$this->assertInstanceOf(CarbonImmutable::class, $shortUrlVisit->updated_at);

Date::useDefault();
}
}

0 comments on commit 48a55af

Please sign in to comment.