trait Test
Properties
staticprotectedClosure|Test|null | $testNow | A test Carbon instance to be returned when now instances are created. | |
staticprotectedstring|null | $testDefaultTimezone | The timezone to resto to when clearing the time mock. |
Methods
Set a Carbon instance (real or mock) to be returned when a "now" instance is created. The provided instance will be returned specifically under the following conditions: - A call to the static now() method, ex. Carbon::now() - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') - When a string containing the desired time is passed to Carbon::parse().
Set a Carbon instance (real or mock) to be returned when a "now" instance is created. The provided instance will be returned specifically under the following conditions: - A call to the static now() method, ex. Carbon::now() - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') - When a string containing the desired time is passed to Carbon::parse().
Temporarily sets a static date to be used within the callback.
Get the Carbon instance (real or mock) to be returned when a "now" instance is created.
Determine if there is a valid test instance set. A valid test instance is anything that is not null.
Get the mocked date passed in setTestNow() and if it's a Closure, execute it.
No description
Details
at line64
static
setTestNow(DateTimeInterface|Closure|Test|string|false|null$testNow =null)
Set a Carbon instance (real or mock) to be returned when a "now" instance is created. The provided instance will be returned specifically under the following conditions: - A call to the static now() method, ex. Carbon::now() - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') - When a string containing the desired time is passed to Carbon::parse().
Note the timezone parameter was left out of the examples above and has no affect as the mock value will be returned regardless of its value.
Only the moment is mocked with setTestNow(), the timezone will still be the one passed as parameter of date_default_timezone_get() as a fallback (see setTestNowAndTimezone()).
To clear the test instance call this method using the default parameter of null.
/!\ Use this method for unit tests only.
at line90
static
setTestNowAndTimezone($testNow =null,$tz =null)
Set a Carbon instance (real or mock) to be returned when a "now" instance is created. The provided instance will be returned specifically under the following conditions: - A call to the static now() method, ex. Carbon::now() - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') - When a string containing the desired time is passed to Carbon::parse().
It will also align default timezone (e.g. call date_default_timezone_set()) with the second argument or if null, with the timezone of the given date object.
To clear the test instance call this method using the default parameter of null.
/!\ Use this method for unit tests only.
at line127
staticmixed
withTestNow(DateTimeInterface|Closure|Test|string|false|null$testNow =null,Closure|null$callback =null)
Temporarily sets a static date to be used within the callback.
Using setTestNow to set the date, executing the callback, then clearing the test instance.
/!\ Use this method for unit tests only.
at line146
staticClosure|Test
getTestNow()
Get the Carbon instance (real or mock) to be returned when a "now" instance is created.
at line157
staticbool
hasTestNow()
Determine if there is a valid test instance set. A valid test instance is anything that is not null.
at line169
staticprotectedCarbonImmutable|Carbon|null
getMockedTestNow(string|DateTimeZone$tz)
Get the mocked date passed in setTestNow() and if it's a Closure, execute it.