Skip to content

Commit

Permalink
[10.x] Allow sync with carbon to be set from fake method (#50450)
Browse files Browse the repository at this point in the history
* feat: allow sync with carbon to be set from fake method

* style: add trailing commas
  • Loading branch information
abenerd committed Mar 11, 2024
1 parent 24d7bd2 commit 3c3d6a6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,16 @@ protected function pullPending()
* Stay awake and capture any attempts to sleep.
*
* @param bool $value
* @param bool $syncWithCarbon
* @return void
*/
public static function fake($value = true)
public static function fake($value = true, $syncWithCarbon = false)
{
static::$fake = $value;

static::$sequence = [];
static::$fakeSleepCallbacks = [];
static::$syncWithCarbon = false;
static::$syncWithCarbon = $syncWithCarbon;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/Support/SleepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Sleep;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use RuntimeException;

Expand Down Expand Up @@ -593,4 +594,40 @@ public function testItCanSyncCarbon()
]);
$this->assertSame('2000-01-01 00:05:03', Date::now()->toDateTimeString());
}

#[TestWith([
'syncWithCarbon' => true,
'datetime' => '2000-01-01 00:05:03',
])]
#[TestWith([
'syncWithCarbon' => false,
'datetime' => '2000-01-01 00:00:00',
])]
public function testFakeCanSetSyncWithCarbon(bool $syncWithCarbon, string $datetime)
{
Carbon::setTestNow('2000-01-01 00:00:00');
Sleep::fake(syncWithCarbon: $syncWithCarbon);

Sleep::for(5)->minutes()
->and(3)->seconds();

Sleep::assertSequence([
Sleep::for(303)->seconds(),
]);
$this->assertSame($datetime, Date::now()->toDateTimeString());
}

public function testFakeDoesNotNeedToSyncWithCarbon()
{
Carbon::setTestNow('2000-01-01 00:00:00');
Sleep::fake();

Sleep::for(5)->minutes()
->and(3)->seconds();

Sleep::assertSequence([
Sleep::for(303)->seconds(),
]);
$this->assertSame('2000-01-01 00:00:00', Date::now()->toDateTimeString());
}
}

0 comments on commit 3c3d6a6

Please sign in to comment.