diff --git a/Sleep.php b/Sleep.php index cd32d7f4..f54ec2a4 100644 --- a/Sleep.php +++ b/Sleep.php @@ -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; @@ -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. * @@ -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); } @@ -317,6 +328,7 @@ public static function fake($value = true) static::$sequence = []; static::$fakeSleepCallbacks = []; + static::$syncWithCarbon = false; } /** @@ -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; + } }