Upstream briannesbitt/Carbon state of the touched files before d8ff648865

This commit is contained in:
upstream committed 2026-10-03 02:24:35 +02:00
1 parent 95c416685a
commit 8dc8cfa977
8 files changed
+5615

No files matched your search

File diff suppressed because it is too large. Load diff
+159
View File
@@ -0,0 +1,159 @@
<?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 Tests\CarbonImmutable;
use BadMethodCallException;
use Carbon\CarbonImmutable as Carbon;
use Tests\AbstractTestCaseWithOldNow;
use Throwable;
class GenericMacroTest extends AbstractTestCaseWithOldNow
{
public function testGenericMacro()
{
Carbon::genericMacro(function ($method) {
$time = preg_replace('/[A-Z]/', ' $0', $method);
try {
return self::this()->modify($time);
} catch (Throwable $exception) {
if (preg_match('(Could not modify with|Failed to parse)', $exception->getMessage())) {
throw new BadMethodCallException('Try next macro', 0, $exception);
}
throw $exception;
}
});
/** @var mixed $now */
$now = Carbon::now();
$this->assertSame('2017-07-02', $now->nextSunday()->format('Y-m-d'));
$this->assertSame('2017-06-26', Carbon::lastMonday()->format('Y-m-d'));
$message = null;
try {
Carbon::fooBar();
} catch (BadMethodCallException $exception) {
$message = $exception->getMessage();
}
$this->assertSame('Method '.Carbon::class.'::fooBar does not exist.', $message);
$message = null;
try {
$now->barBiz();
} catch (BadMethodCallException $exception) {
$message = $exception->getMessage();
}
$this->assertSame('Method barBiz does not exist.', $message);
}
public function testGenericMacroPriority()
{
Carbon::genericMacro(function ($method) {
if (!str_starts_with($method, 'myPrefix')) {
throw new BadMethodCallException('Try next macro', 0);
}
return 'first';
});
Carbon::genericMacro(function ($method) {
if (!str_starts_with($method, 'myPrefix')) {
throw new BadMethodCallException('Try next macro', 0);
}
return 'second';
}, 1);
Carbon::genericMacro(function ($method) {
if (!str_starts_with($method, 'myPrefix')) {
throw new BadMethodCallException('Try next macro', 0);
}
return 'third';
}, -1);
Carbon::macro('myPrefixFooBar', function () {
return 'myPrefixFooBar';
});
/** @var mixed $now */
$now = Carbon::now();
$this->assertSame('second', $now->myPrefixSomething());
$this->assertSame('second', Carbon::myPrefixSomething());
$this->assertSame('myPrefixFooBar', $now->myPrefixFooBar());
$this->assertSame('myPrefixFooBar', Carbon::myPrefixFooBar());
}
public function testLocalGenericMacroPriority()
{
Carbon::genericMacro(function ($method) {
if (!str_starts_with($method, 'mlp')) {
throw new BadMethodCallException('Try next macro', 0);
}
return 'first';
});
Carbon::genericMacro(function ($method) {
if (!str_starts_with($method, 'mlp')) {
throw new BadMethodCallException('Try next macro', 0);
}
return 'second';
}, 1);
Carbon::genericMacro(function ($method) {
if (!str_starts_with($method, 'mlp')) {
throw new BadMethodCallException('Try next macro', 0);
}
return 'third';
}, -1);
Carbon::macro('mlpFooBar', function () {
return 'mlpFooBar';
});
/** @var mixed $date */
$date = Carbon::now()->settings([
'genericMacros' => [
function ($method) {
if (!str_starts_with($method, 'mlp')) {
throw new BadMethodCallException('Try next macro', 0);
}
return 'local-first';
},
function ($method) {
if (!str_starts_with($method, 'mlp')) {
throw new BadMethodCallException('Try next macro', 0);
}
return 'local-second';
},
],
]);
/** @var mixed $now */
$now = Carbon::now();
$this->assertSame('local-first', $date->mlpSomething());
$this->assertSame('second', $now->mlpSomething());
$this->assertSame('second', Carbon::mlpSomething());
$this->assertSame('mlpFooBar', $date->mlpFooBar());
$this->assertSame('mlpFooBar', $now->mlpFooBar());
$this->assertSame('mlpFooBar', Carbon::mlpFooBar());
}
}
File diff suppressed because it is too large. Load diff
+268
View File
@@ -0,0 +1,268 @@
<?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 Tests\CarbonImmutable;
use BadMethodCallException;
use Carbon\CarbonImmutable as Carbon;
use CarbonTimezoneTrait;
use Closure;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use SubCarbonImmutable;
use Tests\AbstractTestCaseWithOldNow;
use Tests\Carbon\Fixtures\FooBar;
use Tests\CarbonImmutable\Fixtures\Mixin;
class MacroTest extends AbstractTestCaseWithOldNow
{
#[RequiresPhpExtension('calendar')]
public function testCarbonIsMacroableWhenNotCalledDynamically()
{
Carbon::macro('easterDays', function ($year = 2019) {
return easter_days($year);
});
/** @var mixed $now */
$now = Carbon::now();
$this->assertSame(22, $now->easterDays(2020));
$this->assertSame(31, $now->easterDays());
Carbon::macro('otherParameterName', function ($other = true) {
return $other;
});
$this->assertTrue($now->otherParameterName());
}
#[RequiresPhpExtension('calendar')]
public function testCarbonIsMacroableWhenNotCalledDynamicallyUsingThis()
{
Carbon::macro('diffFromEaster', function ($year) {
/** @var Carbon $date */
$date = $this;
return $date->toDateTime()->diff(
Carbon::create($year, 3, 21)
->setTimezone($date->getTimezone())
->addDays(easter_days($year))
->endOfDay(),
);
});
/** @var mixed $now */
$now = Carbon::now();
$this->assertSame(1020, $now->diffFromEaster(2020)->days);
}
#[RequiresPhpExtension('calendar')]
public function testCarbonIsMacroableWhenCalledStatically()
{
Carbon::macro('easterDate', function ($year) {
return Carbon::create($year, 3, 21)->addDays(easter_days($year));
});
$this->assertSame('05/04', Carbon::easterDate(2015)->format('d/m'));
}
public function testCarbonIsMacroableWithNonClosureCallables()
{
Carbon::macro('lower2', 'strtolower');
/** @var mixed $now */
$now = Carbon::now();
$this->assertSame('abc', $now->lower2('ABC'));
$this->assertSame('abc', Carbon::lower2('ABC'));
}
public function testCarbonIsMixinable()
{
include_once __DIR__.'/Fixtures/Mixin.php';
$mixin = new Mixin();
Carbon::mixin($mixin);
Carbon::setUserTimezone('America/Belize');
/** @var mixed $date */
$date = Carbon::parse('2000-01-01 12:00:00', 'UTC');
$this->assertSame('06:00 America/Belize', $date->userFormat('H:i e'));
}
public function testCarbonRaisesExceptionWhenStaticMacroIsNotFound()
{
$this->expectExceptionObject(new BadMethodCallException(
'Method Carbon\CarbonImmutable::nonExistingStaticMacro does not exist.',
));
Carbon::nonExistingStaticMacro();
}
public function testCarbonRaisesExceptionWhenMacroIsNotFound()
{
$this->expectExceptionObject(new BadMethodCallException(
'Method nonExistingMacro does not exist.',
));
/** @var mixed $date */
$date = Carbon::now();
$date->nonExistingMacro();
}
public function testTraitMixin()
{
Carbon::mixin(FooBar::class);
Carbon::setTestNow('2019-07-19 00:00:00');
$this->assertSame('supergirl / Friday / immutable', Carbon::super('girl'));
$this->assertSame('superboy / Thursday / immutable', Carbon::parse('2019-07-18')->super('boy'));
$this->assertInstanceOf(Carbon::class, Carbon::me());
}
public function testTraitWithNamedParameters()
{
require_once __DIR__.'/../Fixtures/CarbonTimezoneTrait.php';
Carbon::mixin(CarbonTimezoneTrait::class);
$now = Carbon::now();
$now = eval("return \$now->toAppTz(tz: 'Europe/Paris');");
$this->assertSame('Europe/Paris', $now->format('e'));
}
public function testSerializationAfterTraitChaining()
{
require_once __DIR__.'/../Fixtures/CarbonTimezoneTrait.php';
Carbon::mixin(CarbonTimezoneTrait::class);
Carbon::setTestNow('2023-05-24 14:49');
$date = Carbon::toAppTz(false, 'Europe/Paris');
$this->assertSame('2023-05-24 16:49 Europe/Paris', unserialize(serialize($date))->format('Y-m-d H:i e'));
$date = Carbon::parse('2023-06-12 11:49')->toAppTz(false, 'Europe/Paris');
$this->assertSame('2023-06-12 13:49 Europe/Paris', unserialize(serialize($date))->format('Y-m-d H:i e'));
}
public function testSubClassMacro()
{
require_once __DIR__.'/../Fixtures/SubCarbonImmutable.php';
$subCarbon = new SubCarbonImmutable('2024-01-24 00:00');
SubCarbonImmutable::macro('diffInDecades', function (SubCarbonImmutable|string|null $dt = null, $abs = true) {
return (int) ($this->diffInYears($dt, $abs) / 10);
});
$this->assertSame(2, $subCarbon->diffInDecades(new SubCarbonImmutable('2049-01-24 00:00')));
$this->assertSame(2, $subCarbon->diffInDecades('2049-01-24 00:00'));
SubCarbonImmutable::resetMacros();
}
public function testLazyMixinMethodCall()
{
$test = new class() {
public static array $calledMethods = [];
public function __construct()
{
self::$calledMethods[] = __METHOD__;
}
public function __destruct()
{
self::$calledMethods[] = __METHOD__;
}
public static function noReturnType()
{
self::$calledMethods[] = __METHOD__;
return static fn () => 'foo';
}
public static function returnVoid(): void
{
self::$calledMethods[] = __METHOD__;
}
public static function returnArray(): array
{
self::$calledMethods[] = __METHOD__;
return [];
}
public static function returnObject(): object
{
self::$calledMethods[] = __METHOD__;
return (object) [];
}
public static function returnClosure(): Closure
{
self::$calledMethods[] = __METHOD__;
return static fn () => 'foo';
}
public static function returnMixed(): mixed
{
self::$calledMethods[] = __METHOD__;
return static fn () => 'foo';
}
public static function returnOtherBuiltIn(): bool
{
self::$calledMethods[] = __METHOD__;
return true;
}
public static function returnUnion(): bool|array
{
self::$calledMethods[] = __METHOD__;
return true;
}
public static function getCalledMethods(): array
{
return self::$calledMethods;
}
};
Carbon::mixin($test);
Carbon::resetMacros();
$this->assertSame([
'__construct', // Only happening because of $test = new class()... but none from Carbon::mixin()
'noReturnType',
'returnArray',
'returnObject',
'returnClosure',
'returnMixed',
'returnUnion',
], array_map(
static fn (string $name) => explode('::', $name, 2)[1],
$test::getCalledMethods(),
));
}
}
+254
View File
@@ -0,0 +1,254 @@
<?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 Tests\CarbonImmutable;
use Carbon\CarbonImmutable as Carbon;
use InvalidArgumentException;
use Tests\AbstractTestCase;
class ModifyTest extends AbstractTestCase
{
public function testSimpleModify()
{
$a = new Carbon('2014-03-30 00:00:00');
$b = $a->addHours(24);
$this->assertSame(24.0, $a->diffInHours($b));
}
public function testSimpleModifyWithNamedParameter()
{
$a = new Carbon('2014-03-30 00:00:00');
$b = $a->addHours(value: 24);
$this->assertSame(24.0, $a->diffInHours($b));
}
public function testTimezoneModify()
{
$php81Fix = 1.0;
// For daylight saving time reason 2014-03-30 0h59 is immediately followed by 2h00
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addHours(24);
$this->assertSame(23.0 + $php81Fix, $a->diffInHours($b));
$this->assertSame(23.0 + $php81Fix, $b->diffInHours($a, true));
$this->assertSame(-23.0 - $php81Fix, $b->diffInHours($a));
$this->assertSame(-(23.0 + $php81Fix) * 60, $b->diffInMinutes($a));
$this->assertSame(-(23.0 + $php81Fix) * 60 * 60, $b->diffInSeconds($a));
$this->assertSame(-(23.0 + $php81Fix) * 60 * 60 * 1000, $b->diffInMilliseconds($a));
$this->assertSame(-(23.0 + $php81Fix) * 60 * 60 * 1000000, $b->diffInMicroseconds($a));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealHours(24);
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$b = $b->subRealHours(24);
$this->assertSame(0.0, $b->diffInHours($a, false));
$this->assertSame(0.0, $b->diffInHours($a, false));
$a = new Carbon('2014-03-30 00:59:00', 'Europe/London');
$a = $a->addRealHour();
$this->assertSame('02:59', $a->format('H:i'));
$a = $a->subRealHour();
$this->assertSame('00:59', $a->format('H:i'));
$a = new Carbon('2014-03-30 00:59:00', 'Europe/London');
$a = $a->addRealMinutes(2);
$this->assertSame('02:01', $a->format('H:i'));
$a = $a->subRealMinutes(2);
$this->assertSame('00:59', $a->format('H:i'));
$a = new Carbon('2014-03-30 00:59:30', 'Europe/London');
$a = $a->addRealMinute();
$this->assertSame('02:00:30', $a->format('H:i:s'));
$a = $a->subRealMinute();
$this->assertSame('00:59:30', $a->format('H:i:s'));
$a = new Carbon('2014-03-30 00:59:30', 'Europe/London');
$a = $a->addRealSeconds(40);
$this->assertSame('02:00:10', $a->format('H:i:s'));
$a = $a->subRealSeconds(40);
$this->assertSame('00:59:30', $a->format('H:i:s'));
$a = new Carbon('2014-03-30 00:59:59', 'Europe/London');
$a = $a->addRealSecond();
$this->assertSame('02:00:00', $a->format('H:i:s'));
$a = $a->subRealSecond();
$this->assertSame('00:59:59', $a->format('H:i:s'));
$a = new Carbon('2014-03-30 00:59:59.990000', 'Europe/London');
$a = $a->addRealMilliseconds(20);
$this->assertSame('02:00:00.010000', $a->format('H:i:s.u'));
$a = $a->subRealMilliseconds(20);
$this->assertSame('00:59:59.990000', $a->format('H:i:s.u'));
$a = new Carbon('2014-03-30 00:59:59.999990', 'Europe/London');
$a = $a->addRealMicroseconds(20);
$this->assertSame('02:00:00.000010', $a->format('H:i:s.u'));
$a = $a->subRealMicroseconds(20);
$this->assertSame('00:59:59.999990', $a->format('H:i:s.u'));
$a = new Carbon('2014-03-30 00:59:59.999999', 'Europe/London');
$a = $a->addRealMicrosecond();
$this->assertSame('02:00:00.000000', $a->format('H:i:s.u'));
$a = $a->subRealMicrosecond();
$this->assertSame('00:59:59.999999', $a->format('H:i:s.u'));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealDay();
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealWeeks(1 / 7);
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealMonths(1 / 30);
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealQuarters(1 / 90);
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealYears(1 / 365);
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealDecades(1 / 3650);
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealCenturies(1 / 36500);
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealMillennia(1 / 365000);
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
}
public function testNextAndPrevious()
{
Carbon::setTestNow('2019-06-02 13:27:09.816752');
$this->assertSame('2019-06-02 14:00:00', Carbon::now()->next('2pm')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-01 14:00:00', Carbon::now()->previous('2pm')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-02 14:00:00', Carbon::now()->next('14h')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-01 14:00:00', Carbon::now()->previous('14h')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-03 09:00:00', Carbon::now()->next('9am')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-02 09:00:00', Carbon::now()->previous('9am')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-02 14:00:00', Carbon::parse('next 2pm')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-01 14:00:00', Carbon::parse('previous 2pm')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-02 14:00:00', Carbon::parse('next 14h')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-01 14:00:00', Carbon::parse('previous 14h')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-03 09:00:00', Carbon::parse('next 9am')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-02 09:00:00', Carbon::parse('previous 9am')->format('Y-m-d H:i:s'));
}
public function testAnchorDay(): void
{
$this->assertSame(
'2023-03-31 00:00:00',
Carbon::parse('2023-02-28')->addMonths(1, anchorDay: 31)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2023-04-30 00:00:00',
Carbon::parse('2023-02-28')->addMonths(2, anchorDay: 31)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2023-03-31 00:00:00',
Carbon::parse('2023-02-28')->addMonthsNoOverflow(1, anchorDay: 31)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2023-04-30 00:00:00',
Carbon::parse('2023-02-28')->addMonthsNoOverflow(2, anchorDay: 31)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2024-02-29 00:00:00',
Carbon::parse('2023-02-28')->addYears(1, anchorDay: 29)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2025-02-28 00:00:00',
Carbon::parse('2023-02-28')->addYears(2, anchorDay: 29)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2024-02-29 00:00:00',
Carbon::parse('2023-02-28')->addYearsNoOverflow(1, anchorDay: 29)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2025-02-28 00:00:00',
Carbon::parse('2023-02-28')->addYearsNoOverflow(2, anchorDay: 29)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2024-02-29 00:00:00',
Carbon::parse('2023-02-28')->addYearsWithNoOverflow(1, anchorDay: 29)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2025-02-28 00:00:00',
Carbon::parse('2023-02-28')->addYearsWithNoOverflow(2, anchorDay: 29)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2024-02-29 00:00:00',
Carbon::parse('2023-02-28')->addYearsWithoutOverflow(1, anchorDay: 29)->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2025-02-28 00:00:00',
Carbon::parse('2023-02-28')->addYearsWithoutOverflow(2, anchorDay: 29)->format('Y-m-d H:i:s'),
);
}
public function testAnchorDayIsIncompatibleWithOverflow(): void
{
$this->expectExceptionObject(new InvalidArgumentException(
'$anchorDay is not compatible with overflow',
));
Carbon::parse('2023-02-28')->addYearsWithOverflow(2, anchorDay: 29);
}
}
+435
View File
@@ -0,0 +1,435 @@
<?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 Tests\CarbonImmutable;
use Carbon\CarbonImmutable as Carbon;
use Carbon\Exceptions\ImmutableException;
use Carbon\Exceptions\UnitException;
use Carbon\Month;
use DateTimeZone;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\TestWith;
use RuntimeException;
use Tests\AbstractTestCase;
class SettersTest extends AbstractTestCase
{
public function testYearSetter()
{
$this->expectExceptionObject(new ImmutableException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->year = 1995;
}
public function testMonthSetter()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->month = 3;
}
public function testMonthEnumOnWrongUnit()
{
$this->expectExceptionObject(new UnitException(
'Month enum cannot be used to set hour',
));
$d = Carbon::now();
$d->setHours(Month::February);
}
public function testMonthEnum()
{
$d = Carbon::parse('2023-10-25 21:14:51');
$result = $d->setMonth(Month::February);
$this->assertSame('2023-02-25 21:14:51', $result->format('Y-m-d H:i:s'));
$result = $d->setMonth(Month::July);
$this->assertSame('2023-07-25 21:14:51', $result->format('Y-m-d H:i:s'));
}
public function testMonthFloatFailWithDecimalPart()
{
$this->expectExceptionObject(new InvalidArgumentException(
'month cannot be changed to float value 2.5, integer expected',
));
$d = Carbon::parse('2023-10-25 21:14:51');
$d->setMonth(2.5);
}
public function testMonthFloatPassWithZeroDecimalPart()
{
$d = Carbon::parse('2023-10-25 21:14:51');
$result = $d->setMonth(2.0);
$this->assertSame('2023-02-25 21:14:51', $result->format('Y-m-d H:i:s'));
}
public function testMonthSetterWithWrap()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->month = 13;
}
public function testDaySetter()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->day = 2;
}
public function testDaySetterWithWrap()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::createFromDate(2012, 8, 5);
$d->day = 32;
}
public function testHourSetter()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->hour = 2;
}
public function testHourSetterWithWrap()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->hour = 25;
}
public function testMinuteSetter()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->minute = 2;
}
public function testMinuteSetterWithWrap()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->minute = 65;
}
public function testSecondSetter()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->second = 2;
}
public function testTimeSetter()
{
$d = Carbon::now();
$d = $d->setTime(1, 1, 1);
$this->assertSame(1, $d->second);
$d = $d->setTime(1, 1);
$this->assertSame(0, $d->second);
}
public function testTimeSetterWithChaining()
{
$d = Carbon::now();
$d = $d->setTime(2, 2, 2)->setTime(1, 1, 1);
$this->assertInstanceOfCarbon($d);
$this->assertSame(1, $d->second);
$d = $d->setTime(2, 2, 2)->setTime(1, 1);
$this->assertInstanceOfCarbon($d);
$this->assertSame(0, $d->second);
}
public function testTimeSetterWithZero()
{
$d = Carbon::now();
$d = $d->setTime(1, 1);
$this->assertSame(0, $d->second);
}
public function testDateTimeSetter()
{
$d = Carbon::now();
$d = $d->setDateTime($d->year, $d->month, $d->day, 1, 1, 1);
$this->assertSame(1, $d->second);
}
public function testDateTimeSetterWithZero()
{
$d = Carbon::now();
$d = $d->setDateTime($d->year, $d->month, $d->day, 1, 1);
$this->assertSame(0, $d->second);
}
public function testDateTimeSetterWithChaining()
{
$d = Carbon::now();
$d = $d->setDateTime(2013, 9, 24, 17, 4, 29);
$this->assertInstanceOfCarbon($d);
$d = $d->setDateTime(2014, 10, 25, 18, 5, 30);
$this->assertInstanceOfCarbon($d);
$this->assertCarbon($d, 2014, 10, 25, 18, 5, 30);
}
/**
* @link https://github.com/briannesbitt/Carbon/issues/539
*/
public function testSetDateAfterStringCreation()
{
$d = new Carbon('first day of this month');
$this->assertSame(1, $d->day);
$d = $d->setDate($d->year, $d->month, 12);
$this->assertSame(12, $d->day);
}
public function testSecondSetterWithWrap()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->second = 65;
}
public function testTimestampSetter()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
$d = Carbon::now();
$d->timestamp = 10;
}
public function testSetTimezoneWithInvalidTimezone()
{
$this->expectExceptionObject(new InvalidArgumentException(
'Unknown or bad timezone (sdf)',
));
$d = Carbon::now();
$d->setTimezone('sdf');
}
public function testTimezoneWithInvalidTimezone()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
/** @var mixed $d */
$d = Carbon::now();
$d->timezone = 'sdf';
}
public function testTimezoneWithInvalidTimezoneSetter()
{
$this->expectExceptionObject(new InvalidArgumentException(
'Unknown or bad timezone (sdf)',
));
$d = Carbon::now();
$d->timezone('sdf');
}
public function testTzWithInvalidTimezone()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
/** @var mixed $d */
$d = Carbon::now();
$d->tz = 'sdf';
}
public function testTzWithInvalidTimezoneSetter()
{
$this->expectExceptionObject(new InvalidArgumentException(
'Unknown or bad timezone (sdf)',
));
$d = Carbon::now();
$d->tz('sdf');
}
public function testSetTimezoneUsingString()
{
$d = Carbon::now();
$d = $d->setTimezone('America/Toronto');
$this->assertSame('America/Toronto', $d->tzName);
}
public function testShiftTimezone()
{
$d = Carbon::parse('2018-08-13 10:53:12', 'Europe/Paris');
$d2 = $d->copy()->setTimezone('America/Toronto');
$this->assertSame(0, $d2->getTimestamp() - $d->getTimestamp());
$this->assertSame('04:53:12', $d2->format('H:i:s'));
$d = Carbon::parse('2018-08-13 10:53:12', 'Europe/Paris');
$d2 = $d->copy()->shiftTimezone('America/Toronto');
$this->assertSame(21600, $d2->getTimestamp() - $d->getTimestamp());
$this->assertSame('America/Toronto', $d2->tzName);
$this->assertSame('10:53:12', $d2->format('H:i:s'));
$d = Carbon::parse('2018-03-25 00:53:12.321654 America/Toronto')->shiftTimezone('Europe/Oslo');
$this->assertSame('2018-03-25 00:53:12.321654 Europe/Oslo', $d->format('Y-m-d H:i:s.u e'));
}
public function testTimezoneUsingString()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
/** @var mixed $d */
$d = Carbon::now();
$d->timezone = 'America/Toronto';
}
public function testTzUsingString()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
/** @var mixed $d */
$d = Carbon::now();
$d->tz = 'America/Toronto';
}
public function testSetTimezoneUsingDateTimeZone()
{
$d = Carbon::now();
$d = $d->setTimezone(new DateTimeZone('America/Toronto'));
$this->assertSame('America/Toronto', $d->tzName);
}
public function testTimezoneUsingDateTimeZone()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
/** @var mixed $d */
$d = Carbon::now();
$d->timezone = new DateTimeZone('America/Toronto');
}
public function testTzUsingDateTimeZone()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
/** @var mixed $d */
$d = Carbon::now();
$d->tz = new DateTimeZone('America/Toronto');
}
public function testInvalidSetter()
{
$this->expectExceptionObject(new RuntimeException(
'Carbon\CarbonImmutable class is immutable.',
));
/** @var mixed $d */
$d = Carbon::now();
$d->doesNotExit = 'bb';
}
#[TestWith([9, 15, 30, '09:15:30'])]
#[TestWith([9, 15, 0, '09:15'])]
#[TestWith([9, 0, 0, '09'])]
#[TestWith([9, 5, 3, '9:5:3'])]
#[TestWith([9, 5, 0, '9:5'])]
#[TestWith([9, 0, 0, '9'])]
public function testSetTimeFromTimeString(int $hour, int $minute, int $second, string $time)
{
Carbon::setTestNow(Carbon::create(2016, 2, 12, 1, 2, 3));
$d = Carbon::now()->setTimeFromTimeString($time);
$this->assertCarbon($d, 2016, 2, 12, $hour, $minute, $second);
}
public function testWeekendDaysSetter()
{
$weekendDays = [Carbon::FRIDAY, Carbon::SATURDAY];
$d = Carbon::now();
$d->setWeekendDays($weekendDays);
$this->assertSame($weekendDays, $d->getWeekendDays());
Carbon::setWeekendDays([Carbon::SATURDAY, Carbon::SUNDAY]);
}
public function testMidDayAtSetter()
{
$d = Carbon::now();
$d->setMidDayAt(11);
$this->assertSame(11, $d->getMidDayAt());
$d->setMidDayAt(12);
$this->assertSame(12, $d->getMidDayAt());
}
public function testSetter()
{
$d = Carbon::now();
$d->setMidDayAt(11);
$this->assertSame(11, $d->getMidDayAt());
$d->setMidDayAt(12);
$this->assertSame(12, $d->getMidDayAt());
}
public function testSetUnitNoOverflowFebruary()
{
$d = Carbon::parse('2024-02-29')->setUnitNoOverFlow('day', 31, 'month');
$this->assertInstanceOf(Carbon::class, $d);
$this->assertSame('2024-02-29 23:59:59.999999', $d->format('Y-m-d H:i:s.u'));
}
}
+480
View File
@@ -0,0 +1,480 @@
<?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 Tests\CarbonPeriod;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Carbon\Exceptions\UnknownGetterException;
use Carbon\Exceptions\UnreachableException;
use DateTime;
use Tests\AbstractTestCase;
use Tests\CarbonPeriod\Fixtures\CarbonPeriodFactory;
class GettersTest extends AbstractTestCase
{
public function testGetStartDate()
{
$period = CarbonPeriodFactory::withStartIntervalEnd(static::$periodClass);
$date = $period->getStartDate();
$this->assertInstanceOfCarbon($date);
$this->assertSame('2012-07-01 17:30:00', $date->format('Y-m-d H:i:s'));
}
public function testGetEndDate()
{
$period = CarbonPeriodFactory::withStartIntervalEnd(static::$periodClass);
$date = $period->getEndDate();
$this->assertInstanceOfCarbon($date);
$this->assertSame('2012-07-15 11:15:00', $date->format('Y-m-d H:i:s'));
}
public function testGetDateInterval()
{
$period = CarbonPeriodFactory::withStartIntervalEnd(static::$periodClass);
$interval = $period->getDateInterval();
$this->assertInstanceOfCarbonInterval($interval);
$this->assertSame('P3DT5H', $interval->spec());
}
public function testGetRecurrences()
{
$periodClass = static::$periodClass;
$recurrences = $periodClass::create(new DateTime(), 5)->getRecurrences();
$this->assertSame(5, $recurrences);
$period = $periodClass::create('2024-09-01/3 days/2024-09-30');
$this->assertNull($period->get('recurrences'));
$period = $periodClass::create('R3/2024-09-01/3 days');
$this->assertSame(3, $period->get('recurrences'));
}
public function testGetDefaultDateInterval()
{
$periodClass = static::$periodClass;
$period = $periodClass::create(Carbon::parse('2015-09-30'), Carbon::parse('2015-10-03'));
$this->assertInstanceOfCarbonInterval($period->getDateInterval());
$this->assertSame('P1D', $period->getDateInterval()->spec());
}
public function testModifyStartDate()
{
$period = CarbonPeriodFactory::withStartIntervalEnd(static::$periodClass);
$period->getStartDate()->subDays(3);
$this->assertSame('2012-07-01', $period->getStartDate()->format('Y-m-d'));
}
public function testModifyEndDate()
{
$period = CarbonPeriodFactory::withStartIntervalEnd(static::$periodClass);
$period->getEndDate()->addDays(3);
$this->assertSame('2012-07-15', $period->getEndDate()->format('Y-m-d'));
}
public function testModifyDateInterval()
{
$period = CarbonPeriodFactory::withStartIntervalEnd(static::$periodClass);
$period->getDateInterval()->days(5)->hours(0);
$this->assertSame('P3DT5H', $period->getDateInterval()->spec());
}
public function testGetOptions()
{
$periodClass = static::$periodClass;
$period = new $periodClass();
$this->assertPeriodOptions(0, $period);
$this->assertTrue($period->isStartIncluded());
$this->assertTrue($period->isEndIncluded());
if (PHP_VERSION < 8.2) {
$this->assertTrue($period->include_start_date);
$this->assertTrue($period->include_end_date);
}
$period = new $periodClass(new DateTime(), new DateTime(), $options = $periodClass::EXCLUDE_START_DATE | $periodClass::EXCLUDE_END_DATE);
$this->assertPeriodOptions($options, $period);
$this->assertFalse($period->isStartIncluded());
$this->assertFalse($period->isEndIncluded());
if (PHP_VERSION < 8.2) {
$this->assertFalse($period->include_start_date);
$this->assertFalse($period->include_end_date);
}
}
public function testOverlaps()
{
$periodClass = static::$periodClass;
$range1 = $periodClass::create('2019-01-26', '2019-03-03');
$range2 = $periodClass::create('2019-02-15', '2019-04-01');
$this->assertTrue($range1->overlaps($range2));
$this->assertTrue($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26', '2019-02-13');
$range2 = $periodClass::create('2019-02-15', '2019-04-01');
$this->assertFalse($range1->overlaps($range2));
$this->assertFalse($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26', '2019-02-15');
$range2 = $periodClass::create('2019-02-15', '2019-04-01');
$this->assertFalse($range1->overlaps($range2));
$this->assertFalse($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26', '2019-02-15 00:00:01');
$range2 = $periodClass::create('2019-02-15', '2019-04-01');
$this->assertTrue($range1->overlaps($range2));
$this->assertTrue($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26', '2019-02-15 00:00:01');
$range2 = $periodClass::create('2019-02-15 00:00:01', '2019-04-01');
$this->assertFalse($range1->overlaps($range2));
$this->assertFalse($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', '2019-01-26 13:30:12');
$range2 = $periodClass::create('2019-01-26 10:30:05', '2019-01-26 13:32:12');
$this->assertTrue($range1->overlaps($range2));
$this->assertTrue($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', INF);
$range2 = $periodClass::create('2999-01-26 10:30:05', '2999-01-26 13:32:12');
$this->assertFalse($range1->calculateEnd()->isStartOfTime());
$this->assertTrue($range1->calculateEnd()->isEndOfTime());
$this->assertFalse($range2->calculateEnd()->isStartOfTime());
$this->assertFalse($range2->calculateEnd()->isEndOfTime());
$this->assertTrue($range1->overlaps($range2));
$this->assertTrue($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', CarbonInterval::day(-1), INF);
$range2 = $periodClass::create('2999-01-26 10:30:05', '2999-01-26 13:32:12');
$this->assertTrue($range1->calculateEnd()->isStartOfTime());
$this->assertFalse($range1->calculateEnd()->isEndOfTime());
$this->assertFalse($range2->calculateEnd()->isStartOfTime());
$this->assertFalse($range2->calculateEnd()->isEndOfTime());
$this->assertFalse($range1->overlaps($range2));
$this->assertFalse($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', INF);
$range2 = $periodClass::create('1975-01-26 10:30:05', '1975-01-26 13:32:12');
$this->assertFalse($range1->overlaps($range2));
$this->assertFalse($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', CarbonInterval::day(-1), INF);
$range2 = $periodClass::create('1975-01-26 10:30:05', '1975-01-26 13:32:12');
$this->assertTrue($range1->overlaps($range2));
$this->assertTrue($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', INF);
$range2 = $periodClass::create('2999-01-26 10:30:05', INF);
$this->assertTrue($range1->overlaps($range2));
$this->assertTrue($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', CarbonInterval::day(-1), INF);
$range2 = $periodClass::create('2999-01-26 10:30:05', INF);
$this->assertFalse($range1->overlaps($range2));
$this->assertFalse($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', INF);
$range2 = $periodClass::create('1975-01-26 10:30:05', INF);
$this->assertTrue($range1->overlaps($range2));
$this->assertTrue($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', INF);
$range2 = $periodClass::create('1975-01-26 10:30:05', CarbonInterval::day(-1), INF);
$this->assertFalse($range1->overlaps($range2));
$this->assertFalse($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', CarbonInterval::day(-1), INF);
$range2 = $periodClass::create('1975-01-26 10:30:05', INF);
$this->assertTrue($range1->overlaps($range2));
$this->assertTrue($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', CarbonInterval::day(), 365, $periodClass::EXCLUDE_START_DATE);
$range2 = $periodClass::create('2020-01-26 10:30:05', '2020-01-27 10:30:05');
$this->assertTrue($range1->overlaps($range2));
$this->assertTrue($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', CarbonInterval::day(), 365, $periodClass::EXCLUDE_START_DATE);
$range2 = $periodClass::create('2020-01-26 10:30:20', '2020-01-27 10:30:20');
$this->assertFalse($range1->overlaps($range2));
$this->assertFalse($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', CarbonInterval::day(), 365);
$range2 = $periodClass::create('2020-01-27 10:30:20', '2020-01-28 10:30:20');
$this->assertFalse($range1->overlaps($range2));
$this->assertFalse($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', CarbonInterval::day(), INF);
(function () {
$this->dateInterval->subDays(1);
})->call($range1);
$range2 = $periodClass::create('2999-01-26 10:30:05', '2999-01-26 13:32:12');
$this->assertFalse($range1->overlaps($range2));
$this->assertFalse($range2->overlaps($range1));
$range1 = $periodClass::create('2019-01-26 10:30:12', CarbonInterval::day(), INF);
(function () {
$this->dateInterval->subDays(1);
})->call($range1);
$range2 = $periodClass::create('2018-01-26 10:30:05', '2019-01-26 13:32:12');
$this->assertTrue($range1->overlaps($range2));
$this->assertTrue($range2->overlaps($range1));
}
public function testOverlapsErrorForNullEnd()
{
$periodClass = static::$periodClass;
$this->expectExceptionObject(new UnreachableException(
"Could not calculate period end without either explicit end or recurrences.\n".
"If you're looking for a forever-period, use ->setRecurrences(INF).",
));
$periodClass::create('2019-01-26 10:30:12', null)->overlaps('R2/2019-01-31T10:30:45Z/P2D');
}
public function testOverlapsErrorForMaxAttempts()
{
$periodClass = static::$periodClass;
$this->expectExceptionObject(new UnreachableException(
'Could not calculate period end after iterating 10000 times.',
));
$period = $periodClass::create('2019-01-26 10:30:12', CarbonInterval::minute(), 98282828);
$period = $period->addFilter(function ($date) {
return $date->minute % 2;
});
$period->overlaps('R2/2019-01-31T10:30:45Z/P2D');
}
public function testOverlapsCalculated()
{
$periodClass = static::$periodClass;
$this->assertTrue($periodClass::create('2019-01-27', '2019-02-02')->overlaps('R2/2019-01-31T10:30:45Z/P2D'));
$this->assertTrue($periodClass::create('2019-01-27', '2019-02-02')->overlaps('2018-12-31/2019-02-01'));
$this->assertFalse($periodClass::create('2019-01-27', '2019-02-02')->overlaps('R6/2018-12-31/P3D'));
$this->assertTrue($periodClass::create('2019-01-27', '2019-02-02')->overlaps('R6/2018-12-31/P6D'));
$this->assertFalse($periodClass::create('R6/2018-12-31/P1D')->overlaps('R3/2019-01-05/PT3H'));
$this->assertTrue($periodClass::create('R7/2018-12-31/P1D')->overlaps('R3/2019-01-05/PT3H'));
}
public function testOverlapsWithDatesCouple()
{
$periodClass = static::$periodClass;
$carbonClass = $periodClass === CarbonPeriod::class ? Carbon::class : CarbonImmutable::class;
$this->assertTrue($carbonClass::parse('2019-01-26')->toPeriod('2019-03-03')->overlaps('2019-02-15', '2019-04-01'));
$this->assertTrue($carbonClass::parse('2019-02-15')->toPeriod('2019-04-01')->overlaps('2019-02-15', '2019-04-01'));
$this->assertTrue($periodClass::create('2019-01-26', '2019-03-03')->overlaps('2019-02-15', '2019-04-01'));
$this->assertTrue($periodClass::create('2019-01-26', '2019-03-03')->overlaps($carbonClass::parse('2019-02-15')->toPeriod('2019-04-01')));
$this->assertTrue($carbonClass::parse('2019-01-26')->toPeriod('2019-03-03')->overlaps($carbonClass::parse('2019-02-15'), '2019-04-01'));
$this->assertTrue(Carbon::parse('2019-02-15')->toPeriod('2019-04-01')->overlaps('2019-02-15', CarbonImmutable::parse('2019-04-01')));
$this->assertTrue(CarbonImmutable::parse('2019-02-15')->toPeriod('2019-04-01')->overlaps('2019-02-15', Carbon::parse('2019-04-01')));
$this->assertTrue($periodClass::create('2019-01-26', '2019-03-03')->overlaps(new DateTime('2019-02-15'), new DateTime('2019-04-01')));
$this->assertFalse($carbonClass::parse('2018-01-26')->toPeriod('2018-03-03')->overlaps('2019-02-15', '2019-04-01'));
$this->assertFalse($carbonClass::parse('2018-02-15')->toPeriod('2018-04-01')->overlaps('2019-02-15', '2019-04-01'));
$this->assertFalse($periodClass::create('2018-01-26', '2018-02-13')->overlaps('2019-02-15', '2019-04-01'));
$this->assertFalse($periodClass::create('2018-01-26', '2018-02-13')->overlaps($carbonClass::parse('2019-02-15')->toPeriod('2019-04-01')));
$this->assertFalse($carbonClass::parse('2018-01-26')->toPeriod('2018-03-03')->overlaps($carbonClass::parse('2019-02-15'), '2019-04-01'));
$this->assertFalse(Carbon::parse('2018-02-15')->toPeriod('2018-04-01')->overlaps('2019-02-15', CarbonImmutable::parse('2019-04-01')));
$this->assertFalse($periodClass::create('2018-01-26', '2018-02-13')->overlaps(new DateTime('2019-02-15'), new DateTime('2019-04-01')));
}
public function testIsStarted()
{
$periodClass = static::$periodClass;
Carbon::setTestNow('2019-08-03 11:47:00');
$this->assertFalse($periodClass::create('2019-08-03 11:47:01', '2019-08-03 12:00:00')->isStarted());
$this->assertFalse($periodClass::create('2020-01-01', '2020-07-01')->isStarted());
$this->assertTrue($periodClass::create('2019-08-03 01:00:00', '2019-08-03 09:00:00')->isStarted());
$this->assertTrue($periodClass::create('2019-01-01', '2019-07-01')->isStarted());
$this->assertTrue($periodClass::create('2019-08-01', '2019-08-15')->isStarted());
$this->assertTrue($periodClass::create('2019-08-03 11:47:00', '2019-08-15 11:47:00')->isStarted());
}
public function testIsEnded()
{
$periodClass = static::$periodClass;
Carbon::setTestNow('2019-08-03 11:47:00');
$this->assertFalse($periodClass::create('2019-08-03 11:47:01', '2019-08-03 12:00:00')->isEnded());
$this->assertFalse($periodClass::create('2020-01-01', '2020-07-01')->isEnded());
$this->assertFalse($periodClass::create('2019-08-01', '2019-08-15')->isEnded());
$this->assertFalse($periodClass::create('2019-08-03 11:47:00', '2019-08-15 11:47:00')->isEnded());
$this->assertTrue($periodClass::create('2019-08-03 01:00:00', '2019-08-03 09:00:00')->isEnded());
$this->assertTrue($periodClass::create('2019-01-01', '2019-07-01')->isEnded());
$this->assertTrue($periodClass::create('2019-08-02 11:47:00', '2019-08-03 11:47:00')->isEnded());
}
public function testIsInProgress()
{
$periodClass = static::$periodClass;
Carbon::setTestNow('2019-08-03 11:47:00');
$this->assertFalse($periodClass::create('2019-08-03 11:47:01', '2019-08-03 12:00:00')->isInProgress());
$this->assertFalse($periodClass::create('2020-01-01', '2020-07-01')->isInProgress());
$this->assertFalse($periodClass::create('2019-08-03 01:00:00', '2019-08-03 09:00:00')->isInProgress());
$this->assertFalse($periodClass::create('2019-01-01', '2019-07-01')->isInProgress());
$this->assertFalse($periodClass::create('2019-08-02 11:47:00', '2019-08-03 11:47:00')->isInProgress());
$this->assertTrue($periodClass::create('2019-08-03 11:47:00', '2019-08-15 11:47:00')->isInProgress());
$this->assertTrue($periodClass::create('2019-08-01', '2019-08-15')->isInProgress());
}
public function testIsset()
{
$periodClass = static::$periodClass;
$this->assertTrue(isset($periodClass::create('2019-08-01', '2019-08-15')->startDate));
$this->assertFalse(isset($periodClass::create('2019-08-01', '2019-08-15')->middleDate));
}
public function testMagicGet()
{
$periodClass = static::$periodClass;
$this->assertSame(
'2019-08-01',
$periodClass::create('2019-08-01', '2019-08-15')->startDate->format('Y-m-d'),
);
$this->assertSame(
'en',
$periodClass::create('2019-08-01', '2019-08-15')->locale,
);
$this->assertSame(
'fi',
$periodClass::create('2019-08-01', '2019-08-15')->locale('fi')->locale,
);
}
public function testGet()
{
$periodClass = static::$periodClass;
$this->assertSame(
'2019-08-01',
$periodClass::create('2019-08-01', '2019-08-15')->get('start')->format('Y-m-d'),
);
}
public function testUnknownGetter()
{
$periodClass = static::$periodClass;
$this->expectExceptionObject(new UnknownGetterException('middle'));
$periodClass::create('2019-08-01', '2019-08-15')->get('middle');
}
public function testGetEnd()
{
$periodClass = static::$periodClass;
$period = $periodClass::create('2024-09-01/3 days/2024-09-30');
$this->assertSame('2024-09-30 00:00:00', $period->end->format('Y-m-d H:i:s'));
$this->assertSame('2024-09-30 00:00:00', $period->endDate->format('Y-m-d H:i:s'));
}
public function testEndPropertySyncsAfterSetEndDate()
{
$periodClass = static::$periodClass;
$period = $periodClass::create('2024-09-01/3 days/2024-09-30');
$period = $period->setEndDate('2025-06-15');
$this->assertSame('2025-06-15 00:00:00', $period->end->format('Y-m-d H:i:s'));
$this->assertSame('2025-06-15 00:00:00', $period->getEndDate()->format('Y-m-d H:i:s'));
$period2 = $periodClass::between('2024-01-01', '2024-12-31');
$period2 = $period2->setEndDate('2026-03-01');
$this->assertSame('2026-03-01 00:00:00', $period2->end->format('Y-m-d H:i:s'));
}
public function testGetCurrent()
{
$periodClass = static::$periodClass;
$period = $periodClass::create('2024-09-01/3 days/2024-09-30');
$this->assertSame('2024-09-01 00:00:00', $period->get('current')->format('Y-m-d H:i:s'));
$period->next();
$this->assertSame('2024-09-04 00:00:00', $period->get('current')->format('Y-m-d H:i:s'));
}
public function testGetInclude()
{
$periodClass = static::$periodClass;
$period = $periodClass::create('2024-09-01/3 days/2024-09-30');
$this->assertTrue($period->includeStartDate);
$this->assertTrue($period->includeEndDate);
$period = $periodClass::create('2024-09-01/3 days/2024-09-30')->excludeStartDate()->excludeEndDate();
$this->assertFalse($period->includeStartDate);
$this->assertFalse($period->includeEndDate);
}
}