Boundaries.php
4 years ago
Cast.php
4 years ago
Comparison.php
2 years ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
9 months ago
DeprecatedProperties.php
4 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
4 years ago
Localization.php
9 months ago
Macro.php
4 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
4 years ago
Mutability.php
4 years ago
ObjectInitialisation.php
4 years ago
Options.php
2 years ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
9 months ago
Timestamp.php
1 year ago
ToStringFormat.php
3 years ago
Units.php
2 years ago
Week.php
4 years ago
index.php
3 years ago
Test.php
91 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Carbon\Traits; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Carbon\CarbonInterface; |
| 5 | use MailPoetVendor\Carbon\CarbonTimeZone; |
| 6 | use Closure; |
| 7 | use DateTimeImmutable; |
| 8 | use DateTimeInterface; |
| 9 | use InvalidArgumentException; |
| 10 | use Throwable; |
| 11 | trait Test |
| 12 | { |
| 13 | /////////////////////////////////////////////////////////////////// |
| 14 | ///////////////////////// TESTING AIDS //////////////////////////// |
| 15 | /////////////////////////////////////////////////////////////////// |
| 16 | protected static $testNow; |
| 17 | protected static $testDefaultTimezone; |
| 18 | public static function setTestNow($testNow = null) |
| 19 | { |
| 20 | static::$testNow = $testNow instanceof self || $testNow instanceof Closure ? $testNow : static::make($testNow); |
| 21 | } |
| 22 | public static function setTestNowAndTimezone($testNow = null, $tz = null) |
| 23 | { |
| 24 | if ($testNow) { |
| 25 | self::$testDefaultTimezone = self::$testDefaultTimezone ?? \date_default_timezone_get(); |
| 26 | } |
| 27 | $useDateInstanceTimezone = $testNow instanceof DateTimeInterface; |
| 28 | if ($useDateInstanceTimezone) { |
| 29 | self::setDefaultTimezone($testNow->getTimezone()->getName(), $testNow); |
| 30 | } |
| 31 | static::setTestNow($testNow); |
| 32 | if (!$useDateInstanceTimezone) { |
| 33 | $now = static::getMockedTestNow(\func_num_args() === 1 ? null : $tz); |
| 34 | $tzName = $now ? $now->tzName : null; |
| 35 | self::setDefaultTimezone($tzName ?? self::$testDefaultTimezone ?? 'UTC', $now); |
| 36 | } |
| 37 | if (!$testNow) { |
| 38 | self::$testDefaultTimezone = null; |
| 39 | } |
| 40 | } |
| 41 | public static function withTestNow($testNow, $callback) |
| 42 | { |
| 43 | static::setTestNow($testNow); |
| 44 | try { |
| 45 | $result = $callback(); |
| 46 | } finally { |
| 47 | static::setTestNow(); |
| 48 | } |
| 49 | return $result; |
| 50 | } |
| 51 | public static function getTestNow() |
| 52 | { |
| 53 | return static::$testNow; |
| 54 | } |
| 55 | public static function hasTestNow() |
| 56 | { |
| 57 | return static::getTestNow() !== null; |
| 58 | } |
| 59 | protected static function getMockedTestNow($tz) |
| 60 | { |
| 61 | $testNow = static::getTestNow(); |
| 62 | if ($testNow instanceof Closure) { |
| 63 | $realNow = new DateTimeImmutable('now'); |
| 64 | $testNow = $testNow(static::parse($realNow->format('Y-m-d H:i:s.u'), $tz ?: $realNow->getTimezone())); |
| 65 | } |
| 66 | return $testNow instanceof CarbonInterface ? $testNow->avoidMutation()->tz($tz) : $testNow; |
| 67 | } |
| 68 | protected static function mockConstructorParameters(&$time, $tz) |
| 69 | { |
| 70 | $testInstance = clone static::getMockedTestNow($tz); |
| 71 | if (static::hasRelativeKeywords($time)) { |
| 72 | $testInstance = $testInstance->modify($time); |
| 73 | } |
| 74 | $time = $testInstance instanceof self ? $testInstance->rawFormat(static::MOCK_DATETIME_FORMAT) : $testInstance->format(static::MOCK_DATETIME_FORMAT); |
| 75 | } |
| 76 | private static function setDefaultTimezone($timezone,?DateTimeInterface $date = null) |
| 77 | { |
| 78 | $previous = null; |
| 79 | $success = \false; |
| 80 | try { |
| 81 | $success = \date_default_timezone_set($timezone); |
| 82 | } catch (Throwable $exception) { |
| 83 | $previous = $exception; |
| 84 | } |
| 85 | if (!$success) { |
| 86 | $suggestion = @CarbonTimeZone::create($timezone)->toRegionName($date); |
| 87 | throw new InvalidArgumentException("Timezone ID '{$timezone}' is invalid" . ($suggestion && $suggestion !== $timezone ? ", did you mean '{$suggestion}'?" : '.') . "\n" . "It must be one of the IDs from DateTimeZone::listIdentifiers(),\n" . 'For the record, hours/minutes offset are relevant only for a particular moment, ' . 'but not as a default timezone.', 0, $previous); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 |