Menu

## Test

Test.php : 25

### Table of Contents

#### Methods

getTestNow() : Closure|CarbonInterface|null
  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() : void
  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().
setTestNowAndTimezone() : void
  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().
withTestNow() : T
  Temporarily sets a static date to be used within the callback.
getMockedTestNow() : CarbonInterface|null
  Get the mocked date passed in setTestNow() and if it's a Closure, execute it.
getMockedTestNowClone() : CarbonInterface|self|null
mockConstructorParameters() : void
nowFromClock() : DateTimeImmutable|null

### Methods

#### getTestNow()

Test.php : 107

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

`public static getTestNow() : Closure|CarbonInterface|null`

##### Return values

Closure|CarbonInterface|null —

the current instance used for testing

#### hasTestNow()

Test.php : 118

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

Test.php : 53

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([DateTimeInterface|Closure|static|string|false|null $testNow = null ]) : void`

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

$testNow : DateTimeInterface|Closure|static|string|false|null = null

  real or mock Carbon instance

#### setTestNowAndTimezone()

Test.php : 77

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 setTestNowAndTimezone([DateTimeInterface|Closure|static|string|false|null $testNow = null ][, mixed $timezone = null ]) : void`

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 : DateTimeInterface|Closure|static|string|false|null = null

  real or mock Carbon instance

$timezone : mixed = null

#### withTestNow()

Test.php : 96

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

`public static withTestNow(DateTimeInterface|Closure|static|string|false|null $testNow, callable $callback) : T`

Using setTestNow to set the date, executing the callback, then clearing the test instance.

/!\ Use this method for unit tests only.

##### Parameters

$testNow : DateTimeInterface|Closure|static|string|false|null

  real or mock Carbon instance

$callback : callable

##### Tags

template

  T

##### Return values

T

#### getMockedTestNow()

Test.php : 126

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

`protected static getMockedTestNow(DateTimeZone|string|int|null $timezone) : CarbonInterface|null`

##### Parameters

$timezone : DateTimeZone|string|int|null

##### Return values

CarbonInterface|null

#### getMockedTestNowClone()

Test.php : 172
`private getMockedTestNowClone(mixed $timezone) : CarbonInterface|self|null`

##### Parameters

$timezone : mixed

##### Return values

CarbonInterface|self|null

#### mockConstructorParameters()

Test.php : 139
`private mockConstructorParameters(mixed &$time, CarbonTimeZone|null $timezone) : void`

##### Parameters

$time : mixed
$timezone : CarbonTimeZone|null

#### nowFromClock()

Test.php : 179
`private nowFromClock(CarbonTimeZone|null $timezone) : DateTimeImmutable|null`

##### Parameters

$timezone : CarbonTimeZone|null

##### Return values

DateTimeImmutable|null

## Search results