From 95f9772ddb23dea99124815dc81b27628b5b72a4 Mon Sep 17 00:00:00 2001 From: Anand Capur <551002+arcdigital@users.noreply.github.com> Date: Wed, 24 Jan 2024 07:19:23 -0800 Subject: [PATCH] Add US Holidays (#26) --- src/Countries/UnitedStates.php | 49 +++++++++++++++++++ ...ate_united_states_holidays_after_2021.snap | 46 +++++++++++++++++ ...te_united_states_holidays_before_2021.snap | 42 ++++++++++++++++ tests/Countries/UnitedStatesTest.php | 32 ++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 src/Countries/UnitedStates.php create mode 100644 tests/.pest/snapshots/Countries/UnitedStatesTest/it_can_calculate_united_states_holidays_after_2021.snap create mode 100644 tests/.pest/snapshots/Countries/UnitedStatesTest/it_can_calculate_united_states_holidays_before_2021.snap create mode 100644 tests/Countries/UnitedStatesTest.php diff --git a/src/Countries/UnitedStates.php b/src/Countries/UnitedStates.php new file mode 100644 index 000000000..695103dca --- /dev/null +++ b/src/Countries/UnitedStates.php @@ -0,0 +1,49 @@ + '01-01', + 'Independence Day' => '07-04', + 'Veterans Day' => '11-11', + 'Christmas' => '12-25', + ], $this->variableHolidays($year)); + + if ($year >= 2021) { + $holidays['Juneteenth National Independence Day'] = '06-19'; + } + + return $holidays; + } + + /** @return array */ + protected function variableHolidays(int $year): array + { + $martinLutherKingDay = new CarbonImmutable("third monday of January $year", 'America/Los_Angeles'); + $presidentsDay = new CarbonImmutable("third monday of February $year", 'America/Los_Angeles'); + $memorialDay = new CarbonImmutable("last monday of May $year", 'America/Los_Angeles'); + $laborDay = new CarbonImmutable("first monday of September $year", 'America/Los_Angeles'); + $columbusDay = new CarbonImmutable("second monday of October $year", 'America/Los_Angeles'); + $thanksgiving = new CarbonImmutable("fourth thursday of November $year", 'America/Los_Angeles'); + + return [ + 'Martin Luther King Day' => $martinLutherKingDay, + 'Presidents\' Day' => $presidentsDay, + 'Memorial Day' => $memorialDay, + 'Labor Day' => $laborDay, + 'Columbus Day' => $columbusDay, + 'Thanksgiving' => $thanksgiving, + ]; + } +} diff --git a/tests/.pest/snapshots/Countries/UnitedStatesTest/it_can_calculate_united_states_holidays_after_2021.snap b/tests/.pest/snapshots/Countries/UnitedStatesTest/it_can_calculate_united_states_holidays_after_2021.snap new file mode 100644 index 000000000..41227b5c4 --- /dev/null +++ b/tests/.pest/snapshots/Countries/UnitedStatesTest/it_can_calculate_united_states_holidays_after_2021.snap @@ -0,0 +1,46 @@ +[ + { + "name": "New Year's Day", + "date": "2024-01-01" + }, + { + "name": "Martin Luther King Day", + "date": "2024-01-15" + }, + { + "name": "Presidents' Day", + "date": "2024-02-19" + }, + { + "name": "Memorial Day", + "date": "2024-05-27" + }, + { + "name": "Juneteenth National Independence Day", + "date": "2024-06-19" + }, + { + "name": "Independence Day", + "date": "2024-07-04" + }, + { + "name": "Labor Day", + "date": "2024-09-02" + }, + { + "name": "Columbus Day", + "date": "2024-10-14" + }, + { + "name": "Veterans Day", + "date": "2024-11-11" + }, + { + "name": "Thanksgiving", + "date": "2024-11-28" + }, + { + "name": "Christmas", + "date": "2024-12-25" + } +] \ No newline at end of file diff --git a/tests/.pest/snapshots/Countries/UnitedStatesTest/it_can_calculate_united_states_holidays_before_2021.snap b/tests/.pest/snapshots/Countries/UnitedStatesTest/it_can_calculate_united_states_holidays_before_2021.snap new file mode 100644 index 000000000..5d92494f7 --- /dev/null +++ b/tests/.pest/snapshots/Countries/UnitedStatesTest/it_can_calculate_united_states_holidays_before_2021.snap @@ -0,0 +1,42 @@ +[ + { + "name": "New Year's Day", + "date": "2020-01-01" + }, + { + "name": "Martin Luther King Day", + "date": "2020-01-20" + }, + { + "name": "Presidents' Day", + "date": "2020-02-17" + }, + { + "name": "Memorial Day", + "date": "2020-05-25" + }, + { + "name": "Independence Day", + "date": "2020-07-04" + }, + { + "name": "Labor Day", + "date": "2020-09-07" + }, + { + "name": "Columbus Day", + "date": "2020-10-12" + }, + { + "name": "Veterans Day", + "date": "2020-11-11" + }, + { + "name": "Thanksgiving", + "date": "2020-11-26" + }, + { + "name": "Christmas", + "date": "2020-12-25" + } +] \ No newline at end of file diff --git a/tests/Countries/UnitedStatesTest.php b/tests/Countries/UnitedStatesTest.php new file mode 100644 index 000000000..bdf277d2f --- /dev/null +++ b/tests/Countries/UnitedStatesTest.php @@ -0,0 +1,32 @@ +get(); + + expect($holidays) + ->toBeArray() + ->not()->toBeEmpty(); + + expect(formatDates($holidays))->toMatchSnapshot(); + +}); + +it('can calculate united states holidays before 2021', function () { + CarbonImmutable::setTestNowAndTimezone('2020-01-01'); + + $holidays = Holidays::for(country: 'us')->get(); + + expect($holidays) + ->toBeArray() + ->not()->toBeEmpty(); + + expect(formatDates($holidays))->toMatchSnapshot(); + +});