Files
fewo-stommer/tests/Feature/PriceCalculatorTest.php
T
Lars StommerandClaude Opus 5.5 2e9a391b37 Phase 1: Wohnungen, Saisons & Preisberechnung
- Datenmodell Property/Season/Extra (Beträge in Cent), Seeder mit Preisliste 2026
- PriceCalculator: Saison-Splitting, Mindestaufenthalt, Zusatzperson, Endreinigung, Extras, Anzahlung/Restzahlung
- Filament-Verwaltung: Wohnungen, Saisons (Überschneidungsprüfung, Lückenwarnung, Kopie ins Folgejahr, Preisrechner), Zusatzleistungen
- Tests für Preisberechnung und Saisonverwaltung

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 09:19:49 +02:00

168 lines
6.3 KiB
PHP

<?php
namespace Tests\Feature;
use App\Enums\ExtraPricing;
use App\Enums\QuoteLineType;
use App\Models\Extra;
use App\Models\Property;
use App\Models\Season;
use App\Pricing\PriceCalculator;
use App\Pricing\QuoteException;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;
class PriceCalculatorTest extends TestCase
{
use RefreshDatabase;
private PriceCalculator $calculator;
protected function setUp(): void
{
parent::setUp();
$this->calculator = new PriceCalculator;
}
/**
* The old price list showed "first night incl. cleaning" plus "each further night".
*
* @return array<string, array{string, string, int}>
*/
public static function oldPriceListProvider(): array
{
return [
'Winter, 3 Nächte' => ['2026-01-10', '2026-01-13', 163_00 + 2 * 55_00],
'Hochsaison, 7 Nächte' => ['2026-07-04', '2026-07-11', 223_00 + 6 * 115_00],
'Zwischensaison, 2 Nächte' => ['2026-11-07', '2026-11-09', 173_00 + 65_00],
];
}
#[DataProvider('oldPriceListProvider')]
public function test_totals_match_the_previous_price_list(string $arrival, string $departure, int $expectedTotal): void
{
$this->seed(PropertySeeder::class);
$quote = $this->calculator->calculate(Property::sole(), CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2);
$this->assertSame($expectedTotal, $quote->total());
}
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);
$accommodation = array_values(array_filter($quote->lines, fn ($line) => $line->type === QuoteLineType::Accommodation));
$this->assertCount(2, $accommodation);
$this->assertSame([2, 9500], [$accommodation[0]->quantity, $accommodation[0]->unitPrice]);
$this->assertSame([2, 11500], [$accommodation[1]->quantity, $accommodation[1]->unitPrice]);
$this->assertSame(2 * 9500 + 2 * 11500 + 10800, $quote->total());
}
public function test_deposit_is_a_share_of_the_rent_and_balance_is_due_before_arrival(): void
{
$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);
$rent = 3 * 5500 + 3 * 750;
$this->assertSame($rent, $quote->rent());
$this->assertSame((int) round($rent * 0.35), $quote->deposit());
$this->assertSame($quote->total() - $quote->deposit(), $quote->balance());
$this->assertSame('2026-02-10', $quote->balanceDueOn->toDateString());
}
public function test_extras_are_priced_per_item_or_per_night(): void
{
$property = $this->propertyWithSeason();
$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, [
$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));
}
public function test_minimum_stay_of_the_arrival_season_is_enforced(): void
{
$this->seed(PropertySeeder::class);
$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);
}
public function test_nights_without_a_season_cannot_be_priced(): void
{
$property = $this->propertyWithSeason();
$this->expectException(QuoteException::class);
$this->expectExceptionMessage('01.04.2026');
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-30'), CarbonImmutable::parse('2026-04-02'), 2);
}
public function test_guest_count_is_limited_by_the_property(): void
{
$property = $this->propertyWithSeason();
$this->expectException(QuoteException::class);
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-12'), $property->max_guests + 1);
}
public function test_departure_must_be_after_arrival(): void
{
$property = $this->propertyWithSeason();
$this->expectException(QuoteException::class);
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-10'), 2);
}
public function test_inactive_extras_cannot_be_booked(): void
{
$property = $this->propertyWithSeason();
$extra = Extra::factory()->for($property)->create(['is_active' => false]);
$this->expectException(QuoteException::class);
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-12'), 2, [$extra->id => 1]);
}
/**
* A property with a single season covering March 2026.
*/
private function propertyWithSeason(int $nightlyRate = 5500, int $cleaningFee = 10800, int $depositPercent = 35): Property
{
$property = Property::factory()->create([
'max_guests' => 3,
'included_guests' => 2,
'extra_guest_fee_per_night' => 750,
'cleaning_fee' => $cleaningFee,
'deposit_percent' => $depositPercent,
'balance_due_days_before_arrival' => 28,
]);
Season::factory()->for($property)->create([
'starts_on' => '2026-03-01',
'ends_on' => '2026-04-01',
'nightly_rate' => $nightlyRate,
'min_nights' => 2,
]);
return $property;
}
}