179 lines
5.3 KiB
PHP
Executable File
179 lines
5.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Models\Marker;
|
|
use App\Models\User;
|
|
use App\Models\VacationPeriod;
|
|
use App\Services\PeriodService;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class PeriodServiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private PeriodService $service;
|
|
|
|
private User $user;
|
|
|
|
private Marker $marker;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->service = new PeriodService;
|
|
$this->user = User::factory()->create();
|
|
$this->marker = Marker::factory()->for($this->user)->create();
|
|
}
|
|
|
|
private function day(string $date): CarbonImmutable
|
|
{
|
|
return CarbonImmutable::parse($date);
|
|
}
|
|
|
|
private function addRange(string $start, string $end): VacationPeriod
|
|
{
|
|
return $this->service->addRange($this->user, $this->marker, $this->day($start), $this->day($end));
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array{0: string, 1: string}>
|
|
*/
|
|
private function ranges(): array
|
|
{
|
|
return $this->marker->periods()
|
|
->orderBy('start_date')
|
|
->get(['start_date', 'end_date'])
|
|
->map(fn (VacationPeriod $p) => [$p->start_date->format('Y-m-d'), $p->end_date->format('Y-m-d')])
|
|
->all();
|
|
}
|
|
|
|
public function test_add_range_creates_period(): void
|
|
{
|
|
$this->addRange('2026-07-06', '2026-07-19');
|
|
|
|
$this->assertSame([['2026-07-06', '2026-07-19']], $this->ranges());
|
|
}
|
|
|
|
public function test_add_range_merges_overlapping_periods(): void
|
|
{
|
|
$this->addRange('2026-07-06', '2026-07-12');
|
|
$this->addRange('2026-07-10', '2026-07-19');
|
|
|
|
$this->assertSame([['2026-07-06', '2026-07-19']], $this->ranges());
|
|
}
|
|
|
|
public function test_add_range_merges_adjacent_periods(): void
|
|
{
|
|
$this->addRange('2026-07-06', '2026-07-12');
|
|
$this->addRange('2026-07-13', '2026-07-19');
|
|
|
|
$this->assertSame([['2026-07-06', '2026-07-19']], $this->ranges());
|
|
}
|
|
|
|
public function test_add_range_keeps_distant_periods_separate(): void
|
|
{
|
|
$this->addRange('2026-07-06', '2026-07-12');
|
|
$this->addRange('2026-07-15', '2026-07-19');
|
|
|
|
$this->assertSame([['2026-07-06', '2026-07-12'], ['2026-07-15', '2026-07-19']], $this->ranges());
|
|
}
|
|
|
|
public function test_toggle_marks_unmarked_day(): void
|
|
{
|
|
$this->service->toggleDay($this->user, $this->marker, $this->day('2026-07-06'));
|
|
|
|
$this->assertSame([['2026-07-06', '2026-07-06']], $this->ranges());
|
|
}
|
|
|
|
public function test_toggle_extends_and_merges_with_neighbours(): void
|
|
{
|
|
$this->addRange('2026-07-06', '2026-07-08');
|
|
$this->addRange('2026-07-10', '2026-07-12');
|
|
|
|
$this->service->toggleDay($this->user, $this->marker, $this->day('2026-07-09'));
|
|
|
|
$this->assertSame([['2026-07-06', '2026-07-12']], $this->ranges());
|
|
}
|
|
|
|
public function test_toggle_removes_single_day_period(): void
|
|
{
|
|
$this->addRange('2026-07-06', '2026-07-06');
|
|
|
|
$this->service->toggleDay($this->user, $this->marker, $this->day('2026-07-06'));
|
|
|
|
$this->assertSame([], $this->ranges());
|
|
}
|
|
|
|
public function test_toggle_trims_period_start(): void
|
|
{
|
|
$this->addRange('2026-07-06', '2026-07-12');
|
|
|
|
$this->service->toggleDay($this->user, $this->marker, $this->day('2026-07-06'));
|
|
|
|
$this->assertSame([['2026-07-07', '2026-07-12']], $this->ranges());
|
|
}
|
|
|
|
public function test_toggle_trims_period_end(): void
|
|
{
|
|
$this->addRange('2026-07-06', '2026-07-12');
|
|
|
|
$this->service->toggleDay($this->user, $this->marker, $this->day('2026-07-12'));
|
|
|
|
$this->assertSame([['2026-07-06', '2026-07-11']], $this->ranges());
|
|
}
|
|
|
|
public function test_toggle_splits_period_in_the_middle(): void
|
|
{
|
|
$this->addRange('2026-07-06', '2026-07-12');
|
|
|
|
$this->service->toggleDay($this->user, $this->marker, $this->day('2026-07-09'));
|
|
|
|
$this->assertSame([['2026-07-06', '2026-07-08'], ['2026-07-10', '2026-07-12']], $this->ranges());
|
|
}
|
|
|
|
public function test_other_markers_periods_are_not_merged(): void
|
|
{
|
|
$other = Marker::factory()->for($this->user)->create();
|
|
|
|
$this->addRange('2026-07-06', '2026-07-12');
|
|
$this->service->addRange($this->user, $other, $this->day('2026-07-08'), $this->day('2026-07-10'));
|
|
|
|
$this->assertSame([['2026-07-06', '2026-07-12']], $this->ranges());
|
|
$this->assertCount(1, $other->periods);
|
|
}
|
|
|
|
public function test_leap_year_range(): void
|
|
{
|
|
$this->addRange('2028-02-27', '2028-03-02');
|
|
|
|
$this->assertSame([['2028-02-27', '2028-03-02']], $this->ranges());
|
|
}
|
|
|
|
public function test_cross_year_range(): void
|
|
{
|
|
$this->addRange('2026-12-28', '2027-01-08');
|
|
|
|
$this->assertSame([['2026-12-28', '2027-01-08']], $this->ranges());
|
|
}
|
|
|
|
public function test_reschedule_replaces_range_and_merges(): void
|
|
{
|
|
$period = $this->addRange('2026-07-06', '2026-07-12');
|
|
$this->addRange('2026-07-20', '2026-07-25');
|
|
|
|
$this->service->reschedule(
|
|
$this->user,
|
|
$period,
|
|
$this->marker,
|
|
$this->day('2026-07-18'),
|
|
$this->day('2026-07-22'),
|
|
);
|
|
|
|
$this->assertSame([['2026-07-18', '2026-07-25']], $this->ranges());
|
|
}
|
|
}
|