From 2cbf9867cbabe253d8815b54f3e830bc6d852961 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Wed, 24 Jul 2024 10:30:07 +0200 Subject: [PATCH] Fix deprecations from internals for Date Fix deprecations for startOfDay/endOfDay being called by startOfWeek/endOfWeek. Because dates don't have times we don't need to modify any time related components and can just return early. Fixes #453 --- src/Traits/ModifierTrait.php | 6 ++++++ tests/TestCase/Date/AddTest.php | 18 ++++++++++++++++++ tests/TestCase/DateTime/AddTest.php | 4 ++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Traits/ModifierTrait.php b/src/Traits/ModifierTrait.php index 8d1797d5..effe09ab 100644 --- a/src/Traits/ModifierTrait.php +++ b/src/Traits/ModifierTrait.php @@ -1051,6 +1051,9 @@ public function startOfWeek(): ChronosInterface if ($dt->dayOfWeek !== static::$weekStartsAt) { $dt = $dt->previous(static::$weekStartsAt); } + if ($dt instanceof ChronosDate) { + return $dt; + } return $dt->startOfDay(); } @@ -1066,6 +1069,9 @@ public function endOfWeek(): ChronosInterface if ($dt->dayOfWeek !== static::$weekEndsAt) { $dt = $dt->next(static::$weekEndsAt); } + if ($dt instanceof ChronosDate) { + return $dt; + } return $dt->endOfDay(); } diff --git a/tests/TestCase/Date/AddTest.php b/tests/TestCase/Date/AddTest.php index 9da9ccfd..e6389b3d 100644 --- a/tests/TestCase/Date/AddTest.php +++ b/tests/TestCase/Date/AddTest.php @@ -135,4 +135,22 @@ public function testAddWeekdays($class) $this->assertSame(2, $class::create(1975, 5, 31)->addWeekdays(1)->day); $this->assertSame(30, $class::create(1975, 5, 31)->addWeekdays(-1)->day); } + + /** + * @dataProvider dateClassProvider + * @return void + */ + public function testAddWeeksStartOfWeek($class) + { + $this->assertSame(8, (new $class('2024-7-2'))->addWeeks(1)->startOfWeek()->day); + } + + /** + * @dataProvider dateClassProvider + * @return void + */ + public function testAddWeeksEndOfWeek($class) + { + $this->assertSame(14, (new $class('2024-7-2'))->addWeeks(1)->endOfWeek()->day); + } } diff --git a/tests/TestCase/DateTime/AddTest.php b/tests/TestCase/DateTime/AddTest.php index 61fddb39..8e06fed0 100644 --- a/tests/TestCase/DateTime/AddTest.php +++ b/tests/TestCase/DateTime/AddTest.php @@ -276,9 +276,9 @@ public function testAddWeek($class) */ public function testAddWeeksStartOfWeek($class) { - $this->assertSame(5, (new $class(2024, 7, 2,))->addWeeks(1)->startOfWeek()->day); + $this->assertSame(5, (new $class(2024, 7, 2))->addWeeks(1)->startOfWeek()->day); - $this->assertSame(14, $class::createFromDate(2024, 7, 2,)->addWeeks(1)->endOfWeek()->day); + $this->assertSame(14, $class::createFromDate(2024, 7, 2)->addWeeks(1)->endOfWeek()->day); } /**