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_from_three_nights_ten_percent(): void { $this->offer(['name' => 'Ab 3 Nächten', 'min_nights' => 3, 'discount_value' => 10]); $this->assertSame(0, $this->calculator->calculate($this->property, $this->stay('2026-02-10', '2026-02-12'))->discount()); $this->assertSame(1500, $this->calculator->calculate($this->property, $this->stay('2026-02-10', '2026-02-13'))->discount()); } public function test_fixed_dates_package_applies_only_to_exactly_these_dates(): void { $this->offer([ 'name' => 'Ostsee-Woche', 'kind' => OfferKind::FixedDates, 'stay_from' => '2026-02-07', 'stay_until' => '2026-02-14', 'discount_type' => DiscountType::FixedStayPrice, 'discount_value' => 29900, ]); $exact = $this->calculator->calculate($this->property, $this->stay('2026-02-07', '2026-02-14')); $shifted = $this->calculator->calculate($this->property, $this->stay('2026-02-08', '2026-02-14')); $this->assertSame(7 * 5000 - 29900, $exact->discount()); $this->assertSame(29900 + 10000, $exact->total()); $this->assertSame(0, $shifted->discount()); } public function test_gap_filler_applies_inside_a_short_gap_and_may_waive_the_minimum_stay(): void { $this->property->seasons()->update(['min_nights' => 5]); BlockedPeriod::factory()->for($this->property)->create(['starts_on' => '2026-02-01', 'ends_on' => '2026-02-10']); BlockedPeriod::factory()->for($this->property)->create(['starts_on' => '2026-02-13', 'ends_on' => '2026-02-20']); $this->offer(['name' => 'Lückenfüller', 'kind' => OfferKind::GapFiller, 'max_gap_nights' => 4, 'waives_min_stay' => true, 'discount_value' => 20]); $inGap = $this->calculator->calculate($this->property, $this->stay('2026-02-10', '2026-02-13')); $this->assertSame(3000, $inGap->discount()); $this->expectException(QuoteException::class); $this->calculator->calculate($this->property, $this->stay('2026-02-21', '2026-02-24')); } public function test_gap_filler_ignores_gaps_longer_than_allowed_and_open_ended_free_periods(): void { BlockedPeriod::factory()->for($this->property)->create(['starts_on' => '2026-02-01', 'ends_on' => '2026-02-10']); BlockedPeriod::factory()->for($this->property)->create(['starts_on' => '2026-02-20', 'ends_on' => '2026-02-25']); $this->offer(['name' => 'Lückenfüller', 'kind' => OfferKind::GapFiller, 'max_gap_nights' => 4, 'discount_value' => 20]); $longGap = $this->calculator->calculate($this->property, $this->stay('2026-02-10', '2026-02-13')); $openEnded = $this->calculator->calculate($this->property, $this->stay('2026-02-25', '2026-02-28')); $this->assertSame([0, 0], [$longGap->discount(), $openEnded->discount()]); } 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 $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); } }