186 lines
5.6 KiB
PHP
186 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Schedule\Services;
|
|
|
|
use App\Models\Dish;
|
|
use App\Models\Schedule;
|
|
use App\Models\ScheduledUserDish;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use DishPlanner\Schedule\Services\ScheduleCalendarService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
use Tests\Traits\HasPlanner;
|
|
|
|
class ScheduleCalendarServiceTest extends TestCase
|
|
{
|
|
use HasPlanner;
|
|
use RefreshDatabase;
|
|
|
|
private ScheduleCalendarService $service;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->setUpHasPlanner();
|
|
$this->service = new ScheduleCalendarService();
|
|
}
|
|
|
|
public function test_returns_31_calendar_days(): void
|
|
{
|
|
$planner = $this->planner;
|
|
|
|
$calendarDays = $this->service->getCalendarDays($planner, 1, 2026);
|
|
|
|
$this->assertCount(31, $calendarDays);
|
|
}
|
|
|
|
public function test_includes_correct_day_numbers(): void
|
|
{
|
|
$planner = $this->planner;
|
|
$month = 2;
|
|
$year = 2026;
|
|
$daysInMonth = Carbon::createFromDate($year, $month, 1)->daysInMonth;
|
|
|
|
$calendarDays = $this->service->getCalendarDays($planner, $month, $year);
|
|
|
|
for ($i = 0; $i < $daysInMonth; $i++) {
|
|
$this->assertEquals($i + 1, $calendarDays[$i]['day']);
|
|
}
|
|
|
|
for ($i = $daysInMonth; $i < 31; $i++) {
|
|
$this->assertNull($calendarDays[$i]['day']);
|
|
}
|
|
}
|
|
|
|
public function test_marks_today_correctly(): void
|
|
{
|
|
$planner = $this->planner;
|
|
$today = now();
|
|
|
|
$calendarDays = $this->service->getCalendarDays($planner, $today->month, $today->year);
|
|
|
|
$todayIndex = $today->day - 1;
|
|
$this->assertTrue($calendarDays[$todayIndex]['isToday']);
|
|
|
|
foreach ($calendarDays as $index => $day) {
|
|
if ($index !== $todayIndex && $day['day'] !== null) {
|
|
$this->assertFalse($day['isToday']);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function test_includes_scheduled_dishes(): void
|
|
{
|
|
$planner = $this->planner;
|
|
$user = User::factory()->planner($planner)->create();
|
|
$dish = Dish::factory()->planner($planner)->create();
|
|
$dish->users()->attach($user);
|
|
|
|
$date = Carbon::createFromDate(2026, 3, 15);
|
|
$schedule = Schedule::create([
|
|
'planner_id' => $planner->id,
|
|
'date' => $date->format('Y-m-d'),
|
|
'is_skipped' => false,
|
|
]);
|
|
|
|
$userDish = $user->userDishes->first();
|
|
ScheduledUserDish::create([
|
|
'schedule_id' => $schedule->id,
|
|
'user_id' => $user->id,
|
|
'user_dish_id' => $userDish->id,
|
|
'is_skipped' => false,
|
|
]);
|
|
|
|
$calendarDays = $this->service->getCalendarDays($planner, 3, 2026);
|
|
|
|
$this->assertFalse($calendarDays[14]['isEmpty']);
|
|
$this->assertCount(1, $calendarDays[14]['scheduledDishes']);
|
|
}
|
|
|
|
public function test_empty_days_have_empty_scheduled_dishes(): void
|
|
{
|
|
$planner = $this->planner;
|
|
|
|
$calendarDays = $this->service->getCalendarDays($planner, 4, 2026);
|
|
|
|
foreach ($calendarDays as $day) {
|
|
if ($day['day'] !== null) {
|
|
$this->assertTrue($day['isEmpty']);
|
|
$this->assertCount(0, $day['scheduledDishes']);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function test_only_loads_schedules_for_specified_planner(): void
|
|
{
|
|
$planner1 = $this->planner;
|
|
$planner2 = $this->createPlanner();
|
|
|
|
$user1 = User::factory()->planner($planner1)->create();
|
|
$user2 = User::factory()->planner($planner2)->create();
|
|
|
|
$dish1 = Dish::factory()->planner($planner1)->create();
|
|
$dish2 = Dish::factory()->planner($planner2)->create();
|
|
|
|
$dish1->users()->attach($user1);
|
|
$dish2->users()->attach($user2);
|
|
|
|
$date = Carbon::createFromDate(2026, 5, 10);
|
|
|
|
$schedule1 = Schedule::create([
|
|
'planner_id' => $planner1->id,
|
|
'date' => $date->format('Y-m-d'),
|
|
'is_skipped' => false,
|
|
]);
|
|
|
|
$schedule2 = Schedule::create([
|
|
'planner_id' => $planner2->id,
|
|
'date' => $date->format('Y-m-d'),
|
|
'is_skipped' => false,
|
|
]);
|
|
|
|
ScheduledUserDish::create([
|
|
'schedule_id' => $schedule1->id,
|
|
'user_id' => $user1->id,
|
|
'user_dish_id' => $user1->userDishes->first()->id,
|
|
'is_skipped' => false,
|
|
]);
|
|
|
|
ScheduledUserDish::create([
|
|
'schedule_id' => $schedule2->id,
|
|
'user_id' => $user2->id,
|
|
'user_dish_id' => $user2->userDishes->first()->id,
|
|
'is_skipped' => false,
|
|
]);
|
|
|
|
$calendarDays = $this->service->getCalendarDays($planner1, 5, 2026);
|
|
|
|
$this->assertCount(1, $calendarDays[9]['scheduledDishes']);
|
|
$this->assertEquals($user1->id, $calendarDays[9]['scheduledDishes']->first()->user_id);
|
|
}
|
|
|
|
public function test_get_month_name_returns_correct_format(): void
|
|
{
|
|
$this->assertEquals('January 2026', $this->service->getMonthName(1, 2026));
|
|
$this->assertEquals('December 2025', $this->service->getMonthName(12, 2025));
|
|
$this->assertEquals('February 2027', $this->service->getMonthName(2, 2027));
|
|
}
|
|
|
|
public function test_handles_february_in_leap_year(): void
|
|
{
|
|
$planner = $this->planner;
|
|
|
|
$calendarDays = $this->service->getCalendarDays($planner, 2, 2028);
|
|
|
|
$this->assertCount(31, $calendarDays);
|
|
|
|
for ($i = 0; $i < 29; $i++) {
|
|
$this->assertNotNull($calendarDays[$i]['day']);
|
|
}
|
|
|
|
for ($i = 29; $i < 31; $i++) {
|
|
$this->assertNull($calendarDays[$i]['day']);
|
|
}
|
|
}
|
|
}
|