From c7a5117339b18bfc7d45251ea655a180124b87e5 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 7 Mar 2024 11:10:50 +1100 Subject: [PATCH 1/3] Sleep::syncWithCarbon --- helpers.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/helpers.md b/helpers.md index e48d891d14f..7fe8083d10d 100644 --- a/helpers.md +++ b/helpers.md @@ -2414,4 +2414,17 @@ Sleep::whenFakingSleep(function (Duration $duration) { }); ``` +As progressing time is a common requirement, the Sleep helper provides a dedicated `syncWithCarbon` method to keep Carbon in sync when sleeping within a test. + +```php +Sleep::fake(); +Sleep::syncWithCarbon(); + +$start = now(); + +Sleep::for(1)->second(); + +$start->diffForHumans(); // 1 second ago +``` + Laravel uses the `Sleep` class internally whenever it is pausing execution. For example, the [`retry`](#method-retry) helper uses the `Sleep` class when sleeping, allowing for improved testability when using that helper. From a6d854796155b2203f545a27f192de68d1fc0367 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Mar 2024 11:06:54 -0600 Subject: [PATCH 2/3] Update helpers.md --- helpers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers.md b/helpers.md index 7fe8083d10d..d5fadded840 100644 --- a/helpers.md +++ b/helpers.md @@ -2414,7 +2414,7 @@ Sleep::whenFakingSleep(function (Duration $duration) { }); ``` -As progressing time is a common requirement, the Sleep helper provides a dedicated `syncWithCarbon` method to keep Carbon in sync when sleeping within a test. +As progressing time is a common requirement, the Sleep class provides a dedicated `syncWithCarbon` method to keep Carbon in sync when sleeping within a test: ```php Sleep::fake(); From 90d9e17e7b9c3132538d713b5c9461caccbfcf94 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 13 Mar 2024 15:00:40 -0500 Subject: [PATCH 3/3] Update helpers.md --- helpers.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/helpers.md b/helpers.md index d5fadded840..fc0fbfa3922 100644 --- a/helpers.md +++ b/helpers.md @@ -2414,11 +2414,10 @@ Sleep::whenFakingSleep(function (Duration $duration) { }); ``` -As progressing time is a common requirement, the Sleep class provides a dedicated `syncWithCarbon` method to keep Carbon in sync when sleeping within a test: +As progressing time is a common requirement, the `fake` method accepts a `syncWithCarbon` argument to keep Carbon in sync when sleeping within a test: ```php -Sleep::fake(); -Sleep::syncWithCarbon(); +Sleep::fake(syncWithCarbon: true); $start = now();