Phase 2: Kalender, Anfrage, Vorreservierung & Sonderangebote

- Gäste, Buchungen (Status-Workflow), Sperrzeiten, Sonderangebote
- Vorreservierung mit Haltezeit je Wohnung; abgelaufene Holds geben den Kalender sofort frei, Cron setzt Status und benachrichtigt
- Sonderangebote/Sonderkonditionen: %, € pro Aufenthalt, Sonderpreis/Nacht, Gratis-Nächte; Bedingungen u. a. Saison, Vorlauf, Wochentag, Nächte, Aktionscode; bestes Einzel- vs. kombinierte Angebote
- Öffentliche Seite /buchen: Belegungskalender, Live-Preis, Anfrageformular (Honeypot, Rate-Limit)
- E-Mails: Angebot an Gast, Benachrichtigung Vermieter, Bestätigung, Absage, Ablauf
- Filament: Anfragen & Buchungen (bestätigen, ablehnen, verlängern, stornieren, Telefonbuchung), Gäste, Sperrzeiten, Sonderangebote
- Deutsche Validierungstexte

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lars StommerandClaude Opus 5.5 committed 2026-10-22 09:43:12 +02:00
1 parent 2e9a391b37
commit 08b6582fa1
79 files changed
+6213 -76

No files matched your search

+158
View File
@@ -0,0 +1,158 @@
<?php
namespace Tests\Feature;
use App\Enums\BookingStatus;
use App\Filament\Resources\Bookings\BookingResource;
use App\Filament\Resources\Bookings\Pages\CreateBooking;
use App\Filament\Resources\Bookings\Pages\ListBookings;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Filament\Resources\Guests\GuestResource;
use App\Filament\Resources\Properties\Pages\EditProperty;
use App\Filament\Resources\Properties\RelationManagers\BlockedPeriodsRelationManager;
use App\Filament\Resources\Properties\RelationManagers\SpecialOffersRelationManager;
use App\Mail\BookingConfirmed;
use App\Mail\BookingDeclined;
use App\Models\Booking;
use App\Models\Property;
use App\Models\User;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Filament\Actions\CreateAction;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Tests\TestCase;
class BookingAdminTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed(PropertySeeder::class);
$this->property = Property::sole();
$this->actingAs(User::factory()->create());
Mail::fake();
}
public function test_booking_pages_render(): void
{
$booking = Booking::factory()->for($this->property)->reserved()->create();
$this->get(BookingResource::getUrl('index'))->assertOk()->assertSee($booking->reference);
$this->get(BookingResource::getUrl('view', ['record' => $booking]))->assertOk()->assertSee($booking->guest->email);
$this->get(GuestResource::getUrl('index'))->assertOk()->assertSee($booking->guest->email);
Livewire::test(ListBookings::class)->assertCanSeeTableRecords([$booking]);
}
public function test_landlord_confirms_a_reservation(): void
{
$booking = Booking::factory()->for($this->property)->reserved()->create();
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('confirm', ['notify' => true])
->assertNotified('Buchung bestätigt');
$this->assertSame(BookingStatus::Confirmed, $booking->fresh()->status);
Mail::assertSent(BookingConfirmed::class);
}
public function test_landlord_declines_without_notifying_the_guest(): void
{
$booking = Booking::factory()->for($this->property)->reserved()->create();
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('decline', ['notify' => false]);
$this->assertSame(BookingStatus::Declined, $booking->fresh()->status);
Mail::assertNotSent(BookingDeclined::class);
}
public function test_hold_can_be_extended(): void
{
$booking = Booking::factory()->for($this->property)->reserved(now()->addHours(2))->create();
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('extendHold', ['hours' => 24]);
$this->assertSame('2026-10-23 12:00', $booking->fresh()->hold_expires_at->format('Y-m-d H:i'));
}
public function test_phone_booking_is_entered_and_confirmed_with_calculated_price(): void
{
Livewire::test(CreateBooking::class)
->fillForm([
'property_id' => $this->property->id,
'guests' => 2,
'arrival' => '2026-11-07',
'departure' => '2026-11-10',
'first_name' => 'Peter',
'last_name' => 'Telefon',
'email' => 'peter@example.com',
'confirm_immediately' => true,
'send_mails' => false,
])
->call('create')
->assertHasNoFormErrors();
$booking = Booking::sole();
$this->assertSame([BookingStatus::Confirmed, 'manual', 3 * 6500 + 10800], [$booking->status, $booking->source, $booking->total]);
Mail::assertNothingSent();
}
public function test_phone_booking_for_occupied_dates_is_refused(): void
{
Booking::factory()->for($this->property)->create(['arrival' => '2026-11-08', 'departure' => '2026-11-12']);
Livewire::test(CreateBooking::class)
->fillForm([
'property_id' => $this->property->id,
'guests' => 2,
'arrival' => '2026-11-07',
'departure' => '2026-11-10',
'first_name' => 'Peter',
'last_name' => 'Telefon',
'email' => 'peter@example.com',
])
->call('create')
->assertNotified('Buchung nicht möglich');
$this->assertSame(1, Booking::count());
}
public function test_special_offer_with_euro_value_is_stored_in_cents(): void
{
Livewire::test(SpecialOffersRelationManager::class, ['ownerRecord' => $this->property, 'pageClass' => EditProperty::class])
->callAction(TestAction::make(CreateAction::class)->table(), [
'name' => 'Special Weekend',
'discount_type' => 'nightly_rate',
'discount_value' => '59.50',
'arrival_weekdays' => [5],
'season_names' => ['Winter'],
])
->assertHasNoFormErrors();
$offer = $this->property->specialOffers()->sole();
$this->assertSame([5950, ['5'], ['Winter']], [$offer->discount_value, array_map('strval', $offer->arrival_weekdays), $offer->season_names]);
}
public function test_blocked_period_is_created(): void
{
Livewire::test(BlockedPeriodsRelationManager::class, ['ownerRecord' => $this->property, 'pageClass' => EditProperty::class])
->callAction(TestAction::make(CreateAction::class)->table(), [
'starts_on' => '2026-12-01',
'ends_on' => '2026-12-05',
'reason' => 'Eigennutzung',
])
->assertHasNoFormErrors();
$this->assertSame(1, $this->property->blockedPeriods()->count());
}
}
+113
View File
@@ -0,0 +1,113 @@
<?php
namespace Tests\Feature;
use App\Enums\BookingStatus;
use App\Livewire\BookingRequest;
use App\Models\Booking;
use App\Models\Property;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Tests\TestCase;
class BookingRequestPageTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed(PropertySeeder::class);
Mail::fake();
}
public function test_booking_page_renders(): void
{
$this->withoutVite()
->get(route('booking.create'))
->assertOk()
->assertSee('Preise &amp; Buchung', false)
->assertSeeLivewire(BookingRequest::class);
}
public function test_selecting_dates_shows_the_live_price(): void
{
Livewire::test(BookingRequest::class)
->call('selectDate', '2026-11-07')
->call('selectDate', '2026-11-10')
->assertSet('arrival', '2026-11-07')
->assertSet('departure', '2026-11-10')
->assertSee('Gesamtpreis')
->assertSee('303,00');
}
public function test_occupied_nights_cannot_be_selected_as_arrival_and_ranges_over_them_are_refused(): void
{
Booking::factory()->for(Property::sole())->create(['arrival' => '2026-11-10', 'departure' => '2026-11-14']);
Livewire::test(BookingRequest::class)
->call('selectDate', '2026-11-11')
->assertSet('arrival', null)
->call('selectDate', '2026-11-08')
->call('selectDate', '2026-11-12')
->assertSet('departure', null)
->assertSet('arrival', '2026-11-08')
->call('selectDate', '2026-11-10')
->assertSet('departure', '2026-11-10');
}
public function test_minimum_stay_is_explained_to_the_guest(): void
{
Livewire::test(BookingRequest::class)
->set('arrival', '2026-12-26')
->set('departure', '2026-12-28')
->assertSee('Mindestaufenthalt 6 Nächte');
}
public function test_guest_submits_a_request(): void
{
Livewire::test(BookingRequest::class)
->set('arrival', '2026-11-07')
->set('departure', '2026-11-10')
->set('firstName', 'Anna')
->set('lastName', 'Muster')
->set('email', 'anna@example.com')
->set('privacyAccepted', true)
->call('submit')
->assertHasNoErrors()
->assertSee('Vielen Dank, Anna!');
$this->assertSame(BookingStatus::Reserved, Booking::sole()->status);
}
public function test_request_requires_contact_data_and_consent(): void
{
Livewire::test(BookingRequest::class)
->set('arrival', '2026-11-07')
->set('departure', '2026-11-10')
->call('submit')
->assertHasErrors(['firstName', 'lastName', 'email', 'privacyAccepted']);
$this->assertSame(0, Booking::count());
}
public function test_honeypot_silently_drops_spam(): void
{
Livewire::test(BookingRequest::class)
->set('arrival', '2026-11-07')
->set('departure', '2026-11-10')
->set('firstName', 'Spam')
->set('lastName', 'Bot')
->set('email', 'bot@example.com')
->set('privacyAccepted', true)
->set('website', 'http://spam.example')
->call('submit');
$this->assertSame(0, Booking::count());
}
}
+11 -10
View File
@@ -9,6 +9,7 @@
use App\Models\Season;
use App\Pricing\PriceCalculator;
use App\Pricing\QuoteException;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -47,7 +48,7 @@ public function test_totals_match_the_previous_price_list(string $arrival, strin
{
$this->seed(PropertySeeder::class);
$quote = $this->calculator->calculate(Property::sole(), CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2);
$quote = $this->calculator->calculate(Property::sole(), new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2));
$this->assertSame($expectedTotal, $quote->total());
}
@@ -56,7 +57,7 @@ public function test_stay_across_two_seasons_is_split_per_season(): void
{
$this->seed(PropertySeeder::class);
$quote = $this->calculator->calculate(Property::sole(), CarbonImmutable::parse('2026-06-18'), CarbonImmutable::parse('2026-06-22'), 2);
$quote = $this->calculator->calculate(Property::sole(), new StayRequest(CarbonImmutable::parse('2026-06-18'), CarbonImmutable::parse('2026-06-22'), 2));
$accommodation = array_values(array_filter($quote->lines, fn ($line) => $line->type === QuoteLineType::Accommodation));
$this->assertCount(2, $accommodation);
@@ -69,7 +70,7 @@ public function test_deposit_is_a_share_of_the_rent_and_balance_is_due_before_ar
{
$property = $this->propertyWithSeason(nightlyRate: 5500, cleaningFee: 10800, depositPercent: 35);
$quote = $this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-13'), 3);
$quote = $this->calculator->calculate($property, new StayRequest(CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-13'), 3));
$rent = 3 * 5500 + 3 * 750;
$this->assertSame($rent, $quote->rent());
@@ -84,10 +85,10 @@ public function test_extras_are_priced_per_item_or_per_night(): void
$linen = Extra::factory()->for($property)->create(['price' => 1400, 'pricing' => ExtraPricing::PerItem]);
$babyBed = Extra::factory()->for($property)->create(['price' => 300, 'pricing' => ExtraPricing::PerNight]);
$quote = $this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-14'), 2, [
$quote = $this->calculator->calculate($property, new StayRequest(CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-14'), 2, [
$linen->id => 2,
$babyBed->id => 1,
]);
]));
$extras = array_values(array_filter($quote->lines, fn ($line) => $line->type === QuoteLineType::Extra));
$this->assertSame([2 * 1400, 4 * 300], array_map(fn ($line) => $line->amount, $extras));
@@ -100,7 +101,7 @@ public function test_minimum_stay_of_the_arrival_season_is_enforced(): void
$this->expectException(QuoteException::class);
$this->expectExceptionMessage('Mindestaufenthalt 3 Nächte');
$this->calculator->calculate(Property::sole(), CarbonImmutable::parse('2026-08-29'), CarbonImmutable::parse('2026-08-31'), 2);
$this->calculator->calculate(Property::sole(), new StayRequest(CarbonImmutable::parse('2026-08-29'), CarbonImmutable::parse('2026-08-31'), 2));
}
public function test_nights_without_a_season_cannot_be_priced(): void
@@ -110,7 +111,7 @@ public function test_nights_without_a_season_cannot_be_priced(): void
$this->expectException(QuoteException::class);
$this->expectExceptionMessage('01.04.2026');
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-30'), CarbonImmutable::parse('2026-04-02'), 2);
$this->calculator->calculate($property, new StayRequest(CarbonImmutable::parse('2026-03-30'), CarbonImmutable::parse('2026-04-02'), 2));
}
public function test_guest_count_is_limited_by_the_property(): void
@@ -119,7 +120,7 @@ public function test_guest_count_is_limited_by_the_property(): void
$this->expectException(QuoteException::class);
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-12'), $property->max_guests + 1);
$this->calculator->calculate($property, new StayRequest(CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-12'), $property->max_guests + 1));
}
public function test_departure_must_be_after_arrival(): void
@@ -128,7 +129,7 @@ public function test_departure_must_be_after_arrival(): void
$this->expectException(QuoteException::class);
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-10'), 2);
$this->calculator->calculate($property, new StayRequest(CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-10'), 2));
}
public function test_inactive_extras_cannot_be_booked(): void
@@ -138,7 +139,7 @@ public function test_inactive_extras_cannot_be_booked(): void
$this->expectException(QuoteException::class);
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-12'), 2, [$extra->id => 1]);
$this->calculator->calculate($property, new StayRequest(CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-12'), 2, [$extra->id => 1]));
}
/**
+154
View File
@@ -0,0 +1,154 @@
<?php
namespace Tests\Feature;
use App\Actions\ConfirmBooking;
use App\Actions\CreateReservation;
use App\Actions\ExpireReservations;
use App\Availability\AvailabilityChecker;
use App\Enums\BookingStatus;
use App\Exceptions\ReservationException;
use App\Mail\BookingConfirmed;
use App\Mail\NewReservation;
use App\Mail\ReservationExpired;
use App\Mail\ReservationReceived;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Property;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class ReservationWorkflowTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed(PropertySeeder::class);
$this->property = Property::sole();
Mail::fake();
}
public function test_active_reservations_and_blocked_periods_occupy_the_calendar_but_allow_changeover(): void
{
Booking::factory()->for($this->property)->reserved(now()->addHour())->create(['arrival' => '2026-11-10', 'departure' => '2026-11-14']);
BlockedPeriod::factory()->for($this->property)->create(['starts_on' => '2026-11-20', 'ends_on' => '2026-11-22']);
$availability = new AvailabilityChecker;
$this->assertFalse($availability->isAvailable($this->property, $this->date('2026-11-12'), $this->date('2026-11-16')));
$this->assertFalse($availability->isAvailable($this->property, $this->date('2026-11-18'), $this->date('2026-11-21')));
$this->assertTrue($availability->isAvailable($this->property, $this->date('2026-11-14'), $this->date('2026-11-16')), 'arrival on departure day');
$this->assertTrue($availability->isAvailable($this->property, $this->date('2026-11-07'), $this->date('2026-11-10')), 'departure on arrival day');
$this->assertSame(['2026-11-10', '2026-11-11', '2026-11-12', '2026-11-13', '2026-11-20', '2026-11-21'], array_keys(
$availability->occupiedNights($this->property, $this->date('2026-11-01'), $this->date('2026-12-01')),
));
}
public function test_reservation_with_elapsed_hold_no_longer_occupies_the_calendar(): void
{
Booking::factory()->for($this->property)->reserved(now()->subMinute())->create(['arrival' => '2026-11-10', 'departure' => '2026-11-14']);
$this->assertTrue((new AvailabilityChecker)->isAvailable($this->property, $this->date('2026-11-10'), $this->date('2026-11-14')));
}
public function test_request_creates_a_held_reservation_and_sends_quote_and_notification(): void
{
$booking = $this->reserve('2026-11-07', '2026-11-10');
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->assertSame('2026-10-24 10:00', $booking->hold_expires_at->format('Y-m-d H:i'));
$this->assertSame(3 * 6500 + 10800, $booking->total);
$this->assertSame('2026-'.str_pad((string) $booking->id, 4, '0', STR_PAD_LEFT), $booking->reference);
$this->assertSame('anna@example.com', $booking->guest->email);
Mail::assertSent(ReservationReceived::class, fn (ReservationReceived $mail): bool => $mail->hasTo('anna@example.com'));
Mail::assertSent(NewReservation::class, fn (NewReservation $mail): bool => $mail->hasTo(config('fewo.notification_email')));
}
public function test_request_for_occupied_dates_is_rejected(): void
{
$this->reserve('2026-11-07', '2026-11-10');
$this->expectException(ReservationException::class);
$this->reserve('2026-11-09', '2026-11-12');
}
public function test_returning_guest_is_recognised_by_email(): void
{
$first = $this->reserve('2026-11-07', '2026-11-10');
$second = $this->reserve('2026-11-20', '2026-11-23', 'ANNA@example.com');
$this->assertTrue($first->guest->is($second->guest));
}
public function test_expired_holds_are_marked_and_the_landlord_is_notified(): void
{
$booking = $this->reserve('2026-11-07', '2026-11-10');
$this->travel(47)->hours();
$this->assertSame(0, (new ExpireReservations)->handle());
$this->travel(2)->hours();
$this->assertSame(1, (new ExpireReservations)->handle());
$this->assertSame(BookingStatus::Expired, $booking->fresh()->status);
Mail::assertSent(ReservationExpired::class);
}
public function test_confirmation_makes_the_booking_binding_and_informs_the_guest(): void
{
$booking = $this->reserve('2026-11-07', '2026-11-10');
app(ConfirmBooking::class)->handle($booking);
$booking->refresh();
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertNull($booking->hold_expires_at);
$this->assertNotNull($booking->confirmed_at);
Mail::assertSent(BookingConfirmed::class, fn (BookingConfirmed $mail): bool => $mail->hasTo('anna@example.com'));
}
public function test_expired_reservation_cannot_be_confirmed_once_the_dates_are_taken(): void
{
$expired = $this->reserve('2026-11-07', '2026-11-10');
$this->travel(3)->days();
(new ExpireReservations)->handle();
$this->reserve('2026-11-08', '2026-11-12', 'other@example.com');
$this->expectException(ReservationException::class);
app(ConfirmBooking::class)->handle($expired->fresh());
}
public function test_mails_render(): void
{
$booking = $this->reserve('2026-11-07', '2026-11-10');
$this->assertStringContainsString('vorgemerkt', (new ReservationReceived($booking))->render());
$this->assertStringContainsString($booking->reference, (new NewReservation($booking))->render());
$this->assertStringContainsString('bestätigen', (new BookingConfirmed($booking))->render());
}
private function reserve(string $arrival, string $departure, string $email = 'anna@example.com'): Booking
{
return app(CreateReservation::class)->handle(
$this->property,
new StayRequest($this->date($arrival), $this->date($departure), 2),
['first_name' => 'Anna', 'last_name' => 'Muster', 'email' => $email],
);
}
private function date(string $date): CarbonImmutable
{
return CarbonImmutable::parse($date);
}
}
+140
View File
@@ -0,0 +1,140 @@
<?php
namespace Tests\Feature;
use App\Enums\DiscountType;
use App\Models\Property;
use App\Models\Season;
use App\Models\SpecialOffer;
use App\Pricing\PriceCalculator;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SpecialOfferPricingTest extends TestCase
{
use RefreshDatabase;
private Property $property;
private PriceCalculator $calculator;
protected function setUp(): void
{
parent::setUp();
$this->calculator = new PriceCalculator;
$this->property = Property::factory()->create(['cleaning_fee' => 10000, 'deposit_percent' => 50]);
Season::factory()->for($this->property)->create(['name' => 'Winter', 'starts_on' => '2026-01-03', 'ends_on' => '2026-03-07', 'nightly_rate' => 5000, 'min_nights' => 2]);
Season::factory()->for($this->property)->create(['name' => 'Nebensaison', 'starts_on' => '2026-03-07', 'ends_on' => '2026-04-03', 'nightly_rate' => 7000, 'min_nights' => 2]);
}
public function test_last_minute_applies_only_within_the_booking_window(): void
{
$this->offer(['name' => 'Last Minute', 'max_days_before_arrival' => 14, 'discount_value' => 20]);
$stay = $this->stay('2026-02-10', '2026-02-14');
$lastMinute = $this->calculator->calculate($this->property, $stay, CarbonImmutable::parse('2026-02-01'));
$early = $this->calculator->calculate($this->property, $stay, CarbonImmutable::parse('2026-01-10'));
$this->assertSame(4000, $lastMinute->discount());
$this->assertSame(0, $early->discount());
}
public function test_offer_can_be_restricted_to_seasons_of_the_arrival(): void
{
$this->offer(['season_names' => ['Winter'], 'discount_value' => 10]);
$winter = $this->calculator->calculate($this->property, $this->stay('2026-02-10', '2026-02-12'));
$spring = $this->calculator->calculate($this->property, $this->stay('2026-03-10', '2026-03-12'));
$this->assertSame(1000, $winter->discount());
$this->assertSame(0, $spring->discount());
}
public function test_special_weekend_with_fixed_nightly_rate_on_friday_arrival(): void
{
$this->offer([
'name' => 'Special Weekend',
'discount_type' => DiscountType::NightlyRate,
'discount_value' => 3900,
'arrival_weekdays' => [5],
'max_nights' => 2,
]);
$friday = $this->calculator->calculate($this->property, $this->stay('2026-02-13', '2026-02-15'));
$saturday = $this->calculator->calculate($this->property, $this->stay('2026-02-14', '2026-02-16'));
$this->assertSame(2 * (5000 - 3900), $friday->discount());
$this->assertSame(0, $saturday->discount());
}
public function test_free_nights_discount_the_cheapest_nights(): void
{
$this->offer(['name' => '7 = 6', 'discount_type' => DiscountType::FreeNights, 'discount_value' => 1, 'min_nights' => 7]);
$quote = $this->calculator->calculate($this->property, $this->stay('2026-03-03', '2026-03-10'));
$this->assertSame(5000, $quote->discount());
}
public function test_offer_with_code_needs_the_code(): void
{
$this->offer(['name' => 'Stammgast', 'code' => 'OSTSEE26', 'discount_type' => DiscountType::AmountPerStay, 'discount_value' => 2500]);
$withCode = $this->calculator->calculate($this->property, $this->stay('2026-02-10', '2026-02-12', 'ostsee26'));
$wrongCode = $this->calculator->calculate($this->property, $this->stay('2026-02-10', '2026-02-12', 'FALSCH'));
$withoutCode = $this->calculator->calculate($this->property, $this->stay('2026-02-10', '2026-02-12'));
$this->assertSame([2500, true], [$withCode->discount(), $withCode->promoCodeApplied]);
$this->assertSame([0, false], [$wrongCode->discount(), $wrongCode->promoCodeApplied]);
$this->assertSame([0, null], [$withoutCode->discount(), $withoutCode->promoCodeApplied]);
}
public function test_guest_gets_the_better_of_best_exclusive_offer_and_all_combinable_offers(): void
{
$this->offer(['name' => 'Kombi A', 'discount_value' => 5, 'is_combinable' => true]);
$this->offer(['name' => 'Kombi B', 'discount_value' => 5, 'is_combinable' => true]);
$exclusive = $this->offer(['name' => 'Exklusiv', 'discount_value' => 8]);
$stay = $this->stay('2026-02-10', '2026-02-20');
$this->assertSame(5000, $this->calculator->calculate($this->property, $stay)->discount());
$exclusive->update(['discount_value' => 15]);
$quote = $this->calculator->calculate($this->property, $stay);
$this->assertSame(7500, $quote->discount());
$this->assertSame(['Exklusiv'], array_values(array_map(fn ($line) => $line->label, array_filter($quote->lines, fn ($line) => $line->amount < 0))));
}
public function test_deposit_is_calculated_on_the_discounted_rent(): void
{
$this->offer(['discount_value' => 10]);
$quote = $this->calculator->calculate($this->property, $this->stay('2026-02-10', '2026-02-14'));
$this->assertSame((20000 - 2000) / 2, $quote->deposit());
$this->assertSame(20000 - 2000 + 10000, $quote->total());
}
public function test_inactive_offers_are_ignored(): void
{
$this->offer(['is_active' => false]);
$this->assertSame(0, $this->calculator->calculate($this->property, $this->stay('2026-02-10', '2026-02-14'))->discount());
}
/**
* @param array<string, mixed> $attributes
*/
private function offer(array $attributes): SpecialOffer
{
return SpecialOffer::factory()->for($this->property)->create($attributes);
}
private function stay(string $arrival, string $departure, ?string $promoCode = null): StayRequest
{
return new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2, promoCode: $promoCode);
}
}