Files
fewo-stommer/tests/Feature/SpecialOfferPricingTest.php
T
Lars StommerandClaude Opus 5.5 08b6582fa1 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>
2026-10-22 09:43:12 +02:00

141 lines
5.9 KiB
PHP

<?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);
}
}