Minimum stay exceptions (gap filled, ends on changeover day, offers, absolute minimum); copy seasons aligned to a weekday

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lars StommerandClaude Opus 5.5 committed 2026-10-22 22:38:27 +02:00
1 parent 20235b9139
commit 70a8c5072b
9 files changed
+280 -15

No files matched your search

+127
View File
@@ -0,0 +1,127 @@
<?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_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'));
}
}