| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of the Carbon package. |
| 7 | * |
| 8 | * (c) Brian Nesbitt <[email protected]> |
| 9 | * |
| 10 | * For the full copyright and license information, please view the LICENSE |
| 11 | * file that was distributed with this source code. |
| 12 | */ |
| 13 | |
| 14 | namespace Carbon\Traits; |
| 15 | |
| 16 | use Carbon\CarbonInterface; |
| 17 | use Carbon\CarbonTimeZone; |
| 18 | use Carbon\Factory; |
| 19 | use Carbon\FactoryImmutable; |
| 20 | use Closure; |
| 21 | use DateTimeImmutable; |
| 22 | use DateTimeInterface; |
| 23 | use DateTimeZone; |
| 24 | |
| 25 | trait Test |
| 26 | { |
| 27 | /////////////////////////////////////////////////////////////////// |
| 28 | ///////////////////////// TESTING AIDS //////////////////////////// |
| 29 | /////////////////////////////////////////////////////////////////// |
| 30 | |
| 31 | /** |
| 32 | * Set a Carbon instance (real or mock) to be returned when a "now" |
| 33 | * instance is created. The provided instance will be returned |
| 34 | * specifically under the following conditions: |
| 35 | * - A call to the static now() method, ex. Carbon::now() |
| 36 | * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) |
| 37 | * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') |
| 38 | * - When a string containing the desired time is passed to Carbon::parse(). |
| 39 | * |
| 40 | * Note the timezone parameter was left out of the examples above and |
| 41 | * has no affect as the mock value will be returned regardless of its value. |
| 42 | * |
| 43 | * Only the moment is mocked with setTestNow(), the timezone will still be the one passed |
| 44 | * as parameter of date_default_timezone_get() as a fallback (see setTestNowAndTimezone()). |
| 45 | * |
| 46 | * To clear the test instance call this method using the default |
| 47 | * parameter of null. |
| 48 | * |
| 49 | * /!\ Use this method for unit tests only. |
| 50 | * |
| 51 | * @param DateTimeInterface|Closure|static|string|false|null $testNow real or mock Carbon instance |
| 52 | */ |
| 53 | public static function setTestNow(mixed $testNow = null): void |
| 54 | { |
| 55 | FactoryImmutable::getDefaultInstance()->setTestNow($testNow); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set a Carbon instance (real or mock) to be returned when a "now" |
| 60 | * instance is created. The provided instance will be returned |
| 61 | * specifically under the following conditions: |
| 62 | * - A call to the static now() method, ex. Carbon::now() |
| 63 | * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) |
| 64 | * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') |
| 65 | * - When a string containing the desired time is passed to Carbon::parse(). |
| 66 | * |
| 67 | * It will also align default timezone (e.g. call date_default_timezone_set()) with |
| 68 | * the second argument or if null, with the timezone of the given date object. |
| 69 | * |
| 70 | * To clear the test instance call this method using the default |
| 71 | * parameter of null. |
| 72 | * |
| 73 | * /!\ Use this method for unit tests only. |
| 74 | * |
| 75 | * @param DateTimeInterface|Closure|static|string|false|null $testNow real or mock Carbon instance |
| 76 | */ |
| 77 | public static function setTestNowAndTimezone($testNow = null, $timezone = null): void |
| 78 | { |
| 79 | FactoryImmutable::getDefaultInstance()->setTestNowAndTimezone($testNow, $timezone); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Temporarily sets a static date to be used within the callback. |
| 84 | * Using setTestNow to set the date, executing the callback, then |
| 85 | * clearing the test instance. |
| 86 | * |
| 87 | * /!\ Use this method for unit tests only. |
| 88 | * |
| 89 | * @template T |
| 90 | * |
| 91 | * @param DateTimeInterface|Closure|static|string|false|null $testNow real or mock Carbon instance |
| 92 | * @param Closure(): T $callback |
| 93 | * |
| 94 | * @return T |
| 95 | */ |
| 96 | public static function withTestNow(mixed $testNow, callable $callback): mixed |
| 97 | { |
| 98 | return FactoryImmutable::getDefaultInstance()->withTestNow($testNow, $callback); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the Carbon instance (real or mock) to be returned when a "now" |
| 103 | * instance is created. |
| 104 | * |
| 105 | * @return Closure|CarbonInterface|null the current instance used for testing |
| 106 | */ |
| 107 | public static function getTestNow(): Closure|CarbonInterface|null |
| 108 | { |
| 109 | return FactoryImmutable::getInstance()->getTestNow(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Determine if there is a valid test instance set. A valid test instance |
| 114 | * is anything that is not null. |
| 115 | * |
| 116 | * @return bool true if there is a test instance, otherwise false |
| 117 | */ |
| 118 | public static function hasTestNow(): bool |
| 119 | { |
| 120 | return FactoryImmutable::getInstance()->hasTestNow(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get the mocked date passed in setTestNow() and if it's a Closure, execute it. |
| 125 | */ |
| 126 | protected static function getMockedTestNow(DateTimeZone|string|int|null $timezone): ?CarbonInterface |
| 127 | { |
| 128 | $testNow = FactoryImmutable::getInstance()->handleTestNowClosure(static::getTestNow(), $timezone); |
| 129 | |
| 130 | if ($testNow === null) { |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | $testNow = $testNow->avoidMutation(); |
| 135 | |
| 136 | return $timezone ? $testNow->setTimezone($timezone) : $testNow; |
| 137 | } |
| 138 | |
| 139 | private function mockConstructorParameters(&$time, ?CarbonTimeZone $timezone): void |
| 140 | { |
| 141 | $clock = $this->clock?->unwrap(); |
| 142 | $now = $clock instanceof Factory |
| 143 | ? $clock->getTestNow() |
| 144 | : $this->nowFromClock($timezone); |
| 145 | $testInstance = $now ?? self::getMockedTestNowClone($timezone); |
| 146 | |
| 147 | if (!$testInstance) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | if ($testInstance instanceof DateTimeInterface) { |
| 152 | $testInstance = $testInstance->setTimezone($timezone ?? date_default_timezone_get()); |
| 153 | } |
| 154 | |
| 155 | if (static::hasRelativeKeywords($time)) { |
| 156 | $testInstance = $testInstance->modify($time); |
| 157 | } |
| 158 | |
| 159 | $factory = $this->getClock()?->unwrap(); |
| 160 | |
| 161 | if (!($factory instanceof Factory)) { |
| 162 | $factory = FactoryImmutable::getInstance(); |
| 163 | } |
| 164 | |
| 165 | $testInstance = $factory->handleTestNowClosure($testInstance, $timezone); |
| 166 | |
| 167 | $time = $testInstance instanceof self |
| 168 | ? $testInstance->rawFormat(static::MOCK_DATETIME_FORMAT) |
| 169 | : $testInstance->format(static::MOCK_DATETIME_FORMAT); |
| 170 | } |
| 171 | |
| 172 | private static function getMockedTestNowClone($timezone): CarbonInterface|self|null |
| 173 | { |
| 174 | $mock = static::getMockedTestNow($timezone); |
| 175 | |
| 176 | return $mock ? clone $mock : null; |
| 177 | } |
| 178 | |
| 179 | private function nowFromClock(?CarbonTimeZone $timezone): ?DateTimeImmutable |
| 180 | { |
| 181 | $now = $this->clock?->now(); |
| 182 | |
| 183 | return $now && $timezone ? $now->setTimezone($timezone) : null; |
| 184 | } |
| 185 | } |