Skip to content

Commit

Permalink
[10.x] Sleep syncing (#50392)
Browse files Browse the repository at this point in the history
* Use support carbon

* Allow syncing of carbon with sleep

* Fix capitalization
  • Loading branch information
timacdonald committed Mar 7, 2024
1 parent e641b02 commit cb29175
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Illuminate\Support;

use Carbon\Carbon;
use Carbon\CarbonInterval;
use DateInterval;
use Illuminate\Support\Carbon;
use Illuminate\Support\Traits\Macroable;
use PHPUnit\Framework\Assert as PHPUnit;
use RuntimeException;
Expand All @@ -20,6 +20,13 @@ class Sleep
*/
public static $fakeSleepCallbacks = [];

/**
* Keep Carbon's "now" in sync when sleeping.
*
* @var bool
*/
protected static $syncWithCarbon = false;

/**
* The total duration to sleep.
*
Expand Down Expand Up @@ -259,6 +266,10 @@ public function __destruct()
if (static::$fake) {
static::$sequence[] = $this->duration;

if (static::$syncWithCarbon) {
Carbon::setTestNow(Carbon::now()->add($this->duration));
}

foreach (static::$fakeSleepCallbacks as $callback) {
$callback($this->duration);
}
Expand Down Expand Up @@ -317,6 +328,7 @@ public static function fake($value = true)

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

/**
Expand Down Expand Up @@ -458,4 +470,14 @@ public static function whenFakingSleep($callback)
{
static::$fakeSleepCallbacks[] = $callback;
}

/**
* Indicate that Carbon's "now" should be kept in sync when sleeping.
*
* @return void
*/
public static function syncWithCarbon($value = true)
{
static::$syncWithCarbon = $value;
}
}

0 comments on commit cb29175

Please sign in to comment.