feat(services): add database storage for public holidays

This commit is contained in:
2024-08-07 12:55:57 +02:00
parent 24f36c16d0
commit 9b907cf25d
4 changed files with 216 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Services\WorkingDays\PublicHolidays\Storage;
use App\Models\Country;
use App\Models\NonWorkingDays;
use App\Services\PublicHolidays\Storage\DatabaseStoreTrait;
use App\Services\PublicHolidays\Storage\NWDStore;
use App\Services\WorkingDays\PublicHolidays\Storage\DatabaseStorage;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class DatabaseStorageTest extends TestCase
{
use DatabaseTransactions;
public function testStoreWillStoreDate(): void
{
$country = Country::create(['country_code' => 'XX', 'name' => 'Czech Republic']);
$databaseStorage = new DatabaseStorage();
$item1 = $databaseStorage->storePublicHoliday('XX', new \DateTimeImmutable('2022-01-01'));
$item2 = $databaseStorage->storePublicHoliday('XX', new \DateTimeImmutable('2022-01-02'));
$this->assertEquals(2, NonWorkingDays::where('country_id', $country->id)->count());
$this->assertEquals('2022-01-01', NonWorkingDays::where('id', $item1->id)->first()->non_working_date); // @phpstan-ignore property.nonObject
$this->assertEquals('2022-01-02', NonWorkingDays::where('id', $item2->id)->first()->non_working_date); // @phpstan-ignore property.nonObject
}
public function testStoreWillIgnoreDuplicateDates(): void
{
$country = Country::create(['country_code' => 'XX', 'name' => 'Czech Republic']);
$databaseStorage = new DatabaseStorage();
$databaseStorage->storePublicHoliday('XX', new \DateTimeImmutable('2022-01-01'));
$databaseStorage->storePublicHoliday('XX', new \DateTimeImmutable('2022-01-01'));
$this->assertEquals(1, NonWorkingDays::where('country_id', $country->id)->count());
}
public function testStoreWillStoreDateIfUsedAnotherCountry(): void
{
$country = Country::create(['country_code' => 'XX', 'name' => 'Czech Republic']);
$country2 = Country::create(['country_code' => 'YY', 'name' => 'second Czech republic']);
$databaseStorage = new DatabaseStorage();
$item1 = $databaseStorage->storePublicHoliday('XX', new \DateTimeImmutable('2022-01-01'));
$item2 = $databaseStorage->storePublicHoliday('YY', new \DateTimeImmutable('2022-01-01'));
$this->assertEquals(1, NonWorkingDays::where('country_id', $country->id)->count());
$this->assertEquals(1, NonWorkingDays::where('country_id', $country2->id)->count());
$this->assertEquals('2022-01-01', NonWorkingDays::where('id', $item1->id)->first()->non_working_date); // @phpstan-ignore property.nonObject
$this->assertEquals('2022-01-01', NonWorkingDays::where('id', $item2->id)->first()->non_working_date); // @phpstan-ignore property.nonObject
}
public function testCountPublicHolidaysInIntervalWillReturnCorrectCount(): void
{
Country::create(['country_code' => 'XX', 'name' => 'Czech Republic']);
$databaseStorage = new DatabaseStorage();
$databaseStorage->storePublicHoliday('XX', new \DateTimeImmutable('2022-01-01'));
$databaseStorage->storePublicHoliday('XX', new \DateTimeImmutable('2022-01-02'));
$this->assertEquals(2, $databaseStorage->countPublicHolidaysInInterval('XX', new \DateTimeImmutable('2022-01-01'), new \DateTimeImmutable('2022-01-07')));
}
public function testCountPublicHolidaysInIntervalWillReturnCorrectPublicHolidays(): void
{
Country::create(['country_code' => 'XX', 'name' => 'Czech Republic']);
$databaseStorage = new DatabaseStorage();
$item1 = $databaseStorage->storePublicHoliday('XX', new \DateTimeImmutable('2022-01-01'));
$item2 = $databaseStorage->storePublicHoliday('XX', new \DateTimeImmutable('2022-01-02'));
$holidays = $databaseStorage->getPublicHolidaysInInterval('XX', new \DateTimeImmutable('2022-01-01'), new \DateTimeImmutable('2022-01-07'));
$this->assertCount(2, $holidays);
$this->assertEquals($item1->id, $holidays[0]->id); // @phpstan-ignore property.nonObject
$this->assertEquals($item2->id, $holidays[1]->id); // @phpstan-ignore property.nonObject
}
public function testIsPublicHolidayWillReturnTrueIfDateIsPublicHoliday(): void
{
Country::create(['country_code' => 'XX', 'name' => 'Czech Republic']);
$databaseStorage = new DatabaseStorage();
$databaseStorage->storePublicHoliday('XX', new \DateTimeImmutable('2022-01-01'));
$this->assertTrue($databaseStorage->isPublicHoliday('XX', new \DateTimeImmutable('2022-01-01')));
$this->assertFalse($databaseStorage->isPublicHoliday('XX', new \DateTimeImmutable('2022-01-02')));
}
public function testStoreWillRaiseExceptionIfCountryCodeDoesNotExist(): void
{
$databaseStorage = new DatabaseStorage();
$this->expectException(\InvalidArgumentException::class);
$databaseStorage->storePublicHoliday('ZZ', new \DateTimeImmutable('2022-01-01'));
}
}