diff --git a/src/CarbonFiscalYearTrait.php b/src/CarbonFiscalYearTrait.php index 069c0ca..f0a3fa8 100644 --- a/src/CarbonFiscalYearTrait.php +++ b/src/CarbonFiscalYearTrait.php @@ -2,6 +2,8 @@ namespace LaracraftTech\CarbonExtensions; +use Carbon\CarbonInterface; + trait CarbonFiscalYearTrait { /** @@ -36,7 +38,7 @@ private function ensureFiscalYearStartIsSet() } } - public function startOfYear(): CarbonFiscalYear + public function startOfYear(): CarbonInterface { $year = $this->year; @@ -49,7 +51,7 @@ public function startOfYear(): CarbonFiscalYear return $this->setDate($year, self::$fiscalYearStartMonth, self::$fiscalYearStartDay)->startOfDay(); } - public function endOfYear(): CarbonFiscalYear + public function endOfYear(): CarbonInterface { return $this->startOfYear()->copy()->addYear()->subDay()->endOfDay(); } diff --git a/tests/CarbonFiscalYearTest.php b/tests/CarbonFiscalYearTest.php index 8e6a5fd..9d7e799 100644 --- a/tests/CarbonFiscalYearTest.php +++ b/tests/CarbonFiscalYearTest.php @@ -4,23 +4,29 @@ use LaracraftTech\CarbonExtensions\CarbonFiscalYearImmutable; it('can detect fiscal year start and end', function () { - //set fiscal year - CarbonFiscalYear::setFiscalYearStart(4, 1); + // Let's test both instances... + foreach ([CarbonFiscalYear::class, CarbonFiscalYearImmutable::class] as $dateClass) { + /** @var CarbonFiscalYear|CarbonFiscalYearImmutable $dateClass */ + + //set fiscal year + $dateClass::setFiscalYearStart(4, 1); + - $date = CarbonFiscalYear::parse("2022-03-30"); - expect($date->startOfYear()->format("Y-m-d"))->toBe("2021-04-01") - ->and($date->endOfYear()->format("Y-m-d"))->toBe("2022-03-31"); + $date = $dateClass::parse("2022-03-30"); + expect($date->startOfYear()->format("Y-m-d"))->toBe("2021-04-01") + ->and($date->endOfYear()->format("Y-m-d"))->toBe("2022-03-31"); - $date = CarbonFiscalYear::parse("2022-04-02"); - expect($date->startOfYear()->format("Y-m-d"))->toBe("2022-04-01") - ->and($date->endOfYear()->format("Y-m-d"))->toBe("2023-03-31"); + $date = $dateClass::parse("2022-04-02"); + expect($date->startOfYear()->format("Y-m-d"))->toBe("2022-04-01") + ->and($date->endOfYear()->format("Y-m-d"))->toBe("2023-03-31"); - //another fiscal year - CarbonFiscalYear::setFiscalYearStart(10, 1); + //another fiscal year + $dateClass::setFiscalYearStart(10, 1); - $date = CarbonFiscalYear::parse("2023-05-11"); - expect($date->startOfYear()->format("Y-m-d"))->toBe("2022-10-01") - ->and($date->endOfYear()->format("Y-m-d"))->toBe("2023-09-30"); + $date = $dateClass::parse("2023-05-11"); + expect($date->startOfYear()->format("Y-m-d"))->toBe("2022-10-01") + ->and($date->endOfYear()->format("Y-m-d"))->toBe("2023-09-30"); + } }); it('works with im/mutable', function () {