PHP 8.3.31
Preview: Test.php Size: 6.44 KB
/home/getspomw/royalsquad.us/vendor/nesbot/carbon/src/Carbon/Traits/Test.php

<?php

declare(strict_types=1);

/**
 * This file is part of the Carbon package.
 *
 * (c) Brian Nesbitt <brian@nesbot.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Carbon\Traits;

use Carbon\CarbonInterface;
use Carbon\CarbonTimeZone;
use Carbon\Factory;
use Carbon\FactoryImmutable;
use Closure;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;

trait Test
{
    ///////////////////////////////////////////////////////////////////
    ///////////////////////// TESTING AIDS ////////////////////////////
    ///////////////////////////////////////////////////////////////////

    /**
     * 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.
     *
     * @param DateTimeInterface|Closure|static|string|false|null $testNow real or mock Carbon instance
     */
    public static function setTestNow(mixed $testNow = null): void
    {
        FactoryImmutable::getDefaultInstance()->setTestNow($testNow);
    }

    /**
     * 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.
     *
     * @param DateTimeInterface|Closure|static|string|false|null $testNow real or mock Carbon instance
     */
    public static function setTestNowAndTimezone($testNow = null, $timezone = null): void
    {
        FactoryImmutable::getDefaultInstance()->setTestNowAndTimezone($testNow, $timezone);
    }

    /**
     * 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.
     *
     * @template T
     *
     * @param DateTimeInterface|Closure|static|string|false|null $testNow  real or mock Carbon instance
     * @param Closure(): T                                       $callback
     *
     * @return T
     */
    public static function withTestNow(mixed $testNow, callable $callback): mixed
    {
        return FactoryImmutable::getDefaultInstance()->withTestNow($testNow, $callback);
    }

    /**
     * Get the Carbon instance (real or mock) to be returned when a "now"
     * instance is created.
     *
     * @return Closure|CarbonInterface|null the current instance used for testing
     */
    public static function getTestNow(): Closure|CarbonInterface|null
    {
        return FactoryImmutable::getInstance()->getTestNow();
    }

    /**
     * Determine if there is a valid test instance set. A valid test instance
     * is anything that is not null.
     *
     * @return bool true if there is a test instance, otherwise false
     */
    public static function hasTestNow(): bool
    {
        return FactoryImmutable::getInstance()->hasTestNow();
    }

    /**
     * Get the mocked date passed in setTestNow() and if it's a Closure, execute it.
     */
    protected static function getMockedTestNow(DateTimeZone|string|int|null $timezone): ?CarbonInterface
    {
        $testNow = FactoryImmutable::getInstance()->handleTestNowClosure(static::getTestNow(), $timezone);

        if ($testNow === null) {
            return null;
        }

        $testNow = $testNow->avoidMutation();

        return $timezone ? $testNow->setTimezone($timezone) : $testNow;
    }

    private function mockConstructorParameters(&$time, ?CarbonTimeZone $timezone): void
    {
        $clock = $this->clock?->unwrap();
        $now = $clock instanceof Factory
            ? $clock->getTestNow()
            : $this->nowFromClock($timezone);
        $testInstance = $now ?? self::getMockedTestNowClone($timezone);

        if (!$testInstance) {
            return;
        }

        if ($testInstance instanceof DateTimeInterface) {
            $testInstance = $testInstance->setTimezone($timezone ?? date_default_timezone_get());
        }

        if (static::hasRelativeKeywords($time)) {
            $testInstance = $testInstance->modify($time);
        }

        $factory = $this->getClock()?->unwrap();

        if (!($factory instanceof Factory)) {
            $factory = FactoryImmutable::getInstance();
        }

        $testInstance = $factory->handleTestNowClosure($testInstance, $timezone);

        $time = $testInstance instanceof self
            ? $testInstance->rawFormat(static::MOCK_DATETIME_FORMAT)
            : $testInstance->format(static::MOCK_DATETIME_FORMAT);
    }

    private function getMockedTestNowClone($timezone): CarbonInterface|self|null
    {
        $mock = static::getMockedTestNow($timezone);

        return $mock ? clone $mock : null;
    }

    private function nowFromClock(?CarbonTimeZone $timezone): ?DateTimeImmutable
    {
        $now = $this->clock?->now();

        return $now && $timezone ? $now->setTimezone($timezone) : null;
    }
}

Directory Contents

Dirs: 0 × Files: 28

Name Size Perms Modified Actions
12.15 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
1.16 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
45.26 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
14.12 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
31.37 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
228.11 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
1.89 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
42.63 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
1.65 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
2.36 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
1.59 KB lrw-rw-rw- 2025-09-17 06:53:17
Edit Download
25.46 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
2.66 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
747 B lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
6.18 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
13.59 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
1.29 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
448 B lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
5.85 KB lrw-rw-rw- 2025-09-17 06:53:17
Edit Download
6.93 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
8.38 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
2.33 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
5.28 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
6.44 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
6.42 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
1.42 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
14.17 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download
7.54 KB lrw-rw-rw- 2025-09-17 06:53:16
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).