Test
Table of Contents
- $testNow : static
- A test Carbon instance to be returned when now instances are created.
- getTestNow() : Closure|static
- Get the Carbon instance (real or mock) to be returned when a "now" instance is created.
- hasTestNow() : bool
- Determine if there is a valid test instance set. A valid test instance is anything that is not null.
- setTestNow() : mixed
- 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().
- getMockedTestNow() : CarbonImmutable|Carbon|null
- Get the mocked date passed in setTestNow() and if it's a Closure, execute it.
- handleMockTimezone() : string|DateTimeZone
- Return the given timezone and set it to the test instance if not null.
- mockConstructorParameters() : mixed
Properties
$testNow
A test Carbon instance to be returned when now instances are created.
protected
static static
$testNow
Methods
getTestNow()
Get the Carbon instance (real or mock) to be returned when a "now" instance is created.
public
static getTestNow() : Closure|static
Return values
Closure|static —the current instance used for testing
hasTestNow()
Determine if there is a valid test instance set. A valid test instance is anything that is not null.
public
static hasTestNow() : bool
Return values
bool —true if there is a test instance, otherwise false
setTestNow()
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().
public
static setTestNow([Closure|static|string|null $testNow = null ]) : mixed
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.
To clear the test instance call this method using the default parameter of null.
/!\ Use this method for unit tests only.
Parameters
- $testNow : Closure|static|string|null = null
-
real or mock Carbon instance
Return values
mixed —getMockedTestNow()
Get the mocked date passed in setTestNow() and if it's a Closure, execute it.
protected
static getMockedTestNow(string|DateTimeZone $tz) : CarbonImmutable|Carbon|null
Parameters
- $tz : string|DateTimeZone
Return values
CarbonImmutable|Carbon|null —handleMockTimezone()
Return the given timezone and set it to the test instance if not null.
protected
static handleMockTimezone(string|DateTimeZone $tz, CarbonInterface &$testInstance) : string|DateTimeZone
If null, get the timezone from the test instance and return it.
Parameters
- $tz : string|DateTimeZone
- $testInstance : CarbonInterface
Return values
string|DateTimeZone —mockConstructorParameters()
protected
static mockConstructorParameters(mixed &$time, mixed &$tz) : mixed
Parameters
- $time : mixed
- $tz : mixed