Skip to content

Commit

Permalink
fix start/end of year functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairahcaz committed Apr 6, 2023
1 parent c2a035f commit 590ae83
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/CarbonFiscalYearTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace LaracraftTech\CarbonExtensions;

use Carbon\CarbonInterface;

trait CarbonFiscalYearTrait
{
/**
Expand Down Expand Up @@ -36,7 +38,7 @@ private function ensureFiscalYearStartIsSet()
}
}

public function startOfYear(): CarbonFiscalYear
public function startOfYear(): CarbonInterface
{
$year = $this->year;

Expand All @@ -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();
}
Expand Down
32 changes: 19 additions & 13 deletions tests/CarbonFiscalYearTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 590ae83

Please sign in to comment.