Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"barryvdh/laravel-debugbar": "^3.14",
"fakerphp/faker": "^1.23",
"filament/upgrade": "^4.0",
"kylekatarnls/carbonite": "^2.0",
"laravel/boost": "^1.0",
"laravel/pail": "^1.1",
"laravel/pint": "^1.13",
Expand Down
67 changes: 65 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use Carbon\BespinTimeMocking;
use Carbon\Carbonite;

/*
|--------------------------------------------------------------------------
| Test Case
Expand All @@ -11,6 +14,8 @@
|
*/

uses(BespinTimeMocking::class)->in('Feature', 'Unit');

pest()->extend(Tests\TestCase::class)
// ->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
->in('Feature');
Expand Down Expand Up @@ -45,3 +50,25 @@ function something()
{
// ..
}

/**
* Run a callback with time frozen, then restore the real timeline.
*/
function fakeAsync(callable $callback): mixed
{
Carbonite::freeze();

try {
return $callback();
} finally {
Carbonite::release();
}
}

/**
* Advance the fake timeline by the given number of milliseconds.
*/
function tick(int $milliseconds): void
{
Carbonite::elapse("{$milliseconds} milliseconds");
}
49 changes: 49 additions & 0 deletions tests/Unit/CarboniteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Carbon\Carbon;
use Carbon\Carbonite;

it('freezes time at a given moment', function () {
Carbonite::freeze('2024-01-15 10:00:00');

expect(Carbon::now()->toDateTimeString())->toBe('2024-01-15 10:00:00')
->and(Carbonite::speed())->toBe(0.0);
});

it('accelerates the fake timeline', function () {
Carbonite::freeze('2024-01-01 00:00:00');
Carbonite::speed(2);

expect(Carbonite::accelerate(3))->toBe(6.0);
});

it('decelerates the fake timeline', function () {
Carbonite::freeze('2024-01-01 00:00:00');
Carbonite::speed(5);

expect(Carbonite::decelerate(2))->toBe(2.5);
});

it('elapses time on the fake timeline', function () {
Carbonite::freeze('2024-01-01');
Carbonite::elapse('1 month');

expect(Carbon::now()->format('Y-m-d'))->toBe('2024-02-01');
});

it('advances time with tick inside fakeAsync', function () {
fakeAsync(function () {
$now = Carbon::now();

tick(2000);

expect($now->diffForHumans())->toBe('2 seconds ago');
});
});

it('sets timeline speed with speed()', function () {
Carbonite::freeze('2024-06-01 12:00:00');
Carbonite::speed(3);

expect(Carbonite::speed())->toBe(3.0);
});