Files
fewo-stommer/tests/Feature/MinimumStayRulesTest.php
T
LarsandClaude Opus 5.5 2d2ffffa33
Deploy FeWo mit PipelineManager / deploy (push) Successful in 4m34s
Nächster Schritt sofort sichtbar, Saisonkopie ohne Preislücke
- Vorgang: Die Kopfaktionen werden nach jeder Aktion neu aufgebaut, damit der
  große Schalter gleich den nächsten Schritt zeigt (Filament baut sie vor der
  Aktion auf).
- In Folgejahr kopieren: Reicht eine bestehende Saison (z. B. Neujahr über den
  Jahreswechsel) nur über Beginn oder Ende der Kopie, wird die Kopie dort
  gekürzt statt ganz übersprungen; sonst blieb z. B. Januar bis März ohne Preis.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 19:53:13 +01:00

144 lines
6.5 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\CopySeasonsToNextYear;
use App\Filament\Resources\Properties\Pages\EditProperty;
use App\Filament\Resources\Properties\RelationManagers\SeasonsRelationManager;
use App\Models\BlockedPeriod;
use App\Models\Property;
use App\Models\Season;
use App\Models\SpecialOffer;
use App\Models\User;
use App\Pricing\PriceCalculator;
use App\Pricing\PriceQuote;
use App\Pricing\QuoteException;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class MinimumStayRulesTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->property = Property::factory()->create(['min_stay_absolute' => 2]);
Season::factory()->for($this->property)->create(['name' => 'Hauptsaison', 'starts_on' => '2026-06-06', 'ends_on' => '2026-09-05', 'nightly_rate' => 9000, 'min_nights' => 7]);
}
public function test_season_minimum_applies_without_a_rule(): void
{
$this->expectException(QuoteException::class);
$this->quote('2026-07-08', '2026-07-11');
}
public function test_stay_filling_a_calendar_gap_completely_may_be_shorter(): void
{
BlockedPeriod::factory()->for($this->property)->create(['starts_on' => '2026-07-01', 'ends_on' => '2026-07-08']);
BlockedPeriod::factory()->for($this->property)->create(['starts_on' => '2026-07-11', 'ends_on' => '2026-07-18']);
$this->assertSame(3, $this->quote('2026-07-08', '2026-07-11')->nights());
$this->expectException(QuoteException::class);
$this->quote('2026-07-08', '2026-07-10');
}
public function test_gap_rule_can_be_switched_off(): void
{
$this->property->update(['min_stay_fill_gaps' => false]);
BlockedPeriod::factory()->for($this->property)->create(['starts_on' => '2026-07-01', 'ends_on' => '2026-07-08']);
BlockedPeriod::factory()->for($this->property)->create(['starts_on' => '2026-07-11', 'ends_on' => '2026-07-18']);
$this->expectException(QuoteException::class);
$this->quote('2026-07-08', '2026-07-11');
}
public function test_stay_ending_on_the_changeover_day_may_be_shorter_but_not_below_the_absolute_minimum(): void
{
$this->property->update(['changeover_weekday' => 6, 'min_stay_until_changeover' => true]);
$this->assertSame(3, $this->quote('2026-07-08', '2026-07-11')->nights(), 'Wednesday to Saturday');
try {
$this->quote('2026-07-10', '2026-07-11');
$this->fail('one night is below the absolute minimum');
} catch (QuoteException) {
$this->addToAssertionCount(1);
}
$this->expectException(QuoteException::class);
$this->quote('2026-07-08', '2026-07-10');
}
public function test_any_special_offer_can_waive_the_minimum_stay(): void
{
SpecialOffer::factory()->for($this->property)->create(['name' => 'Special Weekend', 'arrival_weekdays' => [5], 'max_nights' => 2, 'waives_min_stay' => true, 'discount_value' => 10]);
$quote = $this->quote('2026-07-10', '2026-07-12');
$this->assertSame(2, $quote->nights());
$this->assertGreaterThan(0, $quote->discount());
}
public function test_copying_seasons_moves_their_start_to_the_weekday_and_keeps_them_adjacent(): void
{
Season::factory()->for($this->property)->create(['name' => 'Vorsaison', 'starts_on' => '2026-04-04', 'ends_on' => '2026-06-06', 'nightly_rate' => 7000, 'min_nights' => 3]);
$result = app(CopySeasonsToNextYear::class)->handle($this->property, 2026, 6);
$copies = $this->property->seasons()->whereYear('starts_on', 2027)->orderBy('starts_on')->get();
$this->assertSame(2, $result['created']);
$this->assertSame(['2027-04-03', '2027-06-05', '2027-06-05', '2027-09-04'], [
$copies[0]->starts_on->toDateString(), $copies[0]->ends_on->toDateString(),
$copies[1]->starts_on->toDateString(), $copies[1]->ends_on->toDateString(),
]);
$this->assertTrue($copies->every(fn (Season $season): bool => $season->starts_on->isSaturday()));
}
public function test_a_season_over_the_turn_of_the_year_shortens_the_copy_instead_of_leaving_a_gap(): void
{
Season::factory()->for($this->property)->create(['name' => 'Winter', 'starts_on' => '2026-01-03', 'ends_on' => '2026-03-07', 'nightly_rate' => 5500, 'min_nights' => 2]);
Season::factory()->for($this->property)->create(['name' => 'Nebensaison', 'starts_on' => '2026-03-07', 'ends_on' => '2026-04-03', 'nightly_rate' => 6500, 'min_nights' => 2]);
Season::factory()->for($this->property)->create(['name' => 'Neujahr', 'starts_on' => '2026-12-26', 'ends_on' => '2027-01-03', 'nightly_rate' => 11500, 'min_nights' => 6]);
$result = app(CopySeasonsToNextYear::class)->handle($this->property, 2026, 6);
$winter = $this->property->seasons()->where('name', 'Winter')->whereYear('starts_on', 2027)->sole();
$this->assertSame(['2027-01-03', '2027-03-06'], [$winter->starts_on->toDateString(), $winter->ends_on->toDateString()]);
$this->assertSame([0, 1], [$result['skipped'], $result['trimmed']]);
$again = app(CopySeasonsToNextYear::class)->handle($this->property, 2026, 6);
$this->assertSame([0, 0], [$again['created'], $again['trimmed']]);
}
public function test_rules_are_edited_next_to_the_seasons(): void
{
$this->actingAs(User::factory()->create());
Livewire::test(SeasonsRelationManager::class, ['ownerRecord' => $this->property, 'pageClass' => EditProperty::class])
->callAction(TestAction::make('minimumStayRules')->table(), [
'changeover_weekday' => 6,
'min_stay_fill_gaps' => true,
'min_stay_until_changeover' => true,
'min_stay_absolute' => 3,
])
->assertHasNoFormErrors();
$this->assertSame([6, true, 3], [$this->property->fresh()->changeover_weekday, $this->property->fresh()->min_stay_until_changeover, $this->property->fresh()->min_stay_absolute]);
}
private function quote(string $arrival, string $departure): PriceQuote
{
return app(PriceCalculator::class)->calculate($this->property->fresh(), new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2), CarbonImmutable::parse('2026-03-01'));
}
}