d2d.emegrendeles.hu/tests/Feature/DeliveryCalendarServiceTest.php
2026-04-15 06:42:55 +02:00

167 lines
5.3 KiB
PHP

<?php
use App\Models\DeliveryCalendarOverride;
use App\Models\DeliverySchedule;
use App\Models\ProfitCenter;
use App\Models\ProfitCenterSupplierSchedule;
use App\Models\Supplier;
use App\Models\WorkCalendar;
use App\Enums\WorkDayType;
use App\Enums\DataSource;
use App\Services\DeliveryCalendarService;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
uses(TestCase::class, RefreshDatabase::class);
it('returns true for a scheduled delivery day', function () {
$pc = ProfitCenter::factory()->create();
$supplier = Supplier::factory()->create();
$schedule = DeliverySchedule::factory()->create([
'monday' => true,
]);
ProfitCenterSupplierSchedule::factory()->create([
'profit_center_id' => $pc->id,
'supplier_id' => $supplier->id,
'delivery_schedule_id' => $schedule->id,
]);
$service = app(DeliveryCalendarService::class);
$monday = Carbon::parse('2026-04-13'); // Monday
expect($service->isDeliveryDay($pc, $supplier, $monday))->toBeTrue();
});
it('returns false for a non-scheduled delivery day', function () {
$pc = ProfitCenter::factory()->create();
$supplier = Supplier::factory()->create();
$schedule = DeliverySchedule::factory()->create([
'monday' => true,
'tuesday' => false,
]);
ProfitCenterSupplierSchedule::factory()->create([
'profit_center_id' => $pc->id,
'supplier_id' => $supplier->id,
'delivery_schedule_id' => $schedule->id,
]);
$service = app(DeliveryCalendarService::class);
$tuesday = Carbon::parse('2026-04-14'); // Tuesday
expect($service->isDeliveryDay($pc, $supplier, $tuesday))->toBeFalse();
});
it('respects official holidays (WorkCalendar)', function () {
$pc = ProfitCenter::factory()->create();
$supplier = Supplier::factory()->create();
$schedule = DeliverySchedule::factory()->create(['monday' => true]);
ProfitCenterSupplierSchedule::factory()->create([
'profit_center_id' => $pc->id,
'supplier_id' => $supplier->id,
'delivery_schedule_id' => $schedule->id,
]);
$monday = Carbon::parse('2026-04-13'); // Monday
// Mark this Monday as a holiday
WorkCalendar::create([
'date' => '2026-04-13',
'type' => WorkDayType::Holiday,
'data_source' => DataSource::Manual,
]);
$service = app(DeliveryCalendarService::class);
expect($service->isDeliveryDay($pc, $supplier, $monday))->toBeFalse();
});
it('respects supplier overrides over holidays', function () {
$pc = ProfitCenter::factory()->create();
$supplier = Supplier::factory()->create();
$schedule = DeliverySchedule::factory()->create(['monday' => true]);
ProfitCenterSupplierSchedule::factory()->create([
'profit_center_id' => $pc->id,
'supplier_id' => $supplier->id,
'delivery_schedule_id' => $schedule->id,
]);
$monday = Carbon::parse('2026-04-13'); // Monday
// Mark this Monday as a holiday
WorkCalendar::create([
'date' => '2026-04-13',
'type' => WorkDayType::Holiday,
'data_source' => DataSource::Manual,
]);
// Create an override to allow delivery despite the holiday
DeliveryCalendarOverride::create([
'supplier_id' => $supplier->id,
'date' => '2026-04-13',
'is_delivery_day' => true,
]);
$service = app(DeliveryCalendarService::class);
expect($service->isDeliveryDay($pc, $supplier, $monday))->toBeTrue();
});
it('respects supplier overrides to block delivery on a normal day', function () {
$pc = ProfitCenter::factory()->create();
$supplier = Supplier::factory()->create();
$schedule = DeliverySchedule::factory()->create(['monday' => true]);
ProfitCenterSupplierSchedule::factory()->create([
'profit_center_id' => $pc->id,
'supplier_id' => $supplier->id,
'delivery_schedule_id' => $schedule->id,
]);
$monday = Carbon::parse('2026-04-13'); // Monday
// Create an override to block delivery
DeliveryCalendarOverride::create([
'supplier_id' => $supplier->id,
'date' => '2026-04-13',
'is_delivery_day' => false,
]);
$service = app(DeliveryCalendarService::class);
expect($service->isDeliveryDay($pc, $supplier, $monday))->toBeFalse();
});
it('calculates available delivery dates in a range', function () {
$pc = ProfitCenter::factory()->create();
$supplier = Supplier::factory()->create();
$schedule = DeliverySchedule::factory()->create([
'monday' => true,
'wednesday' => true,
'friday' => true,
]);
ProfitCenterSupplierSchedule::factory()->create([
'profit_center_id' => $pc->id,
'supplier_id' => $supplier->id,
'delivery_schedule_id' => $schedule->id,
]);
$start = Carbon::parse('2026-04-13'); // Monday
$end = Carbon::parse('2026-04-19'); // Sunday
$service = app(DeliveryCalendarService::class);
$dates = $service->getAvailableDeliveryDates($pc, $supplier, $start, $end);
// Should be Mon, Wed, Fri
expect($dates)->toHaveCount(3);
$dateStrings = $dates->map(fn($d) => $d->toDateString());
expect($dateStrings)->toContain('2026-04-13');
expect($dateStrings)->toContain('2026-04-15');
expect($dateStrings)->toContain('2026-04-17');
});