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

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().

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().

static mixed
withTestNow(DateTimeInterface|Closure|Test|string|false|null$testNow =null,Closure|null$callback =null)

Temporarily sets a static date to be used within the callback.

static Closure|Test
getTestNow()

Get the Carbon instance (real or mock) to be returned when a "now" instance is created.

static bool
hasTestNow()

Determine if there is a valid test instance set. A valid test instance is anything that is not null.

static CarbonImmutable|Carbon|null
getMockedTestNow(string|DateTimeZone$tz)

Get the mocked date passed in setTestNow() and if it's a Closure, execute it.

static 
mockConstructorParameters($time,$tz)

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.

Parameters

DateTimeInterface|Closure|Test|string|false|null $testNow real or mock Carbon instance

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.

Parameters

$testNow
$tz

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.

Parameters

DateTimeInterface|Closure|Test|string|false|null $testNow real or mock Carbon instance
Closure|null $callback

Return Value

mixed

at line146
staticClosure|Test getTestNow()

Get the Carbon instance (real or mock) to be returned when a "now" instance is created.

Return Value

Closure|Test the current instance used for testing

at line157
staticbool hasTestNow()

Determine if there is a valid test instance set. A valid test instance is anything that is not null.

Return Value

bool true if there is a test instance, otherwise false

at line169
staticprotectedCarbonImmutable|Carbon|null getMockedTestNow(string|DateTimeZone$tz)

Get the mocked date passed in setTestNow() and if it's a Closure, execute it.

Parameters

string|DateTimeZone $tz

Return Value

CarbonImmutable|Carbon|null

at line187
staticprotected mockConstructorParameters($time,$tz)

Parameters

$time
$tz