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'));
}
}