* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Tests\CarbonImmutable; use Carbon\Carbon as MutableCarbon; use Carbon\CarbonImmutable as Carbon; use ErrorException; use Generator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\RequiresPhp; use Tests\AbstractTestCase; #[RequiresPhp('>=8.5.0')] class NoDiscardTest extends AbstractTestCase { public static function dataForDiscardedCalls(): Generator { yield ['modify', ['+1 day']]; yield ['next', []]; yield ['previous', []]; yield ['addDays', [2]]; } #[DataProvider('dataForDiscardedCalls')] public function testDiscardedResultTriggersWarning(string $method, array $arguments): void { $this->expectException(ErrorException::class); $this->expectExceptionMessage( "The return value of method Carbon\\CarbonImmutable::$method() should either be used or intentionally ignored by casting it as (void)", ); $date = Carbon::parse('2026-10-02 12:00'); $this->withErrorAsException(static function () use ($date, $method, $arguments) { $date->$method(...$arguments); }); } public function testMutableResultCanBeDiscarded(): void { $date = MutableCarbon::parse('2026-10-02 12:00'); $this->withErrorAsException(static function () use ($date) { $date->modify('+1 day'); $date->next(); $date->addDays(2); }); $this->assertSame('2026-10-12 00:00:00', $date->format('Y-m-d H:i:s')); } }