Files
fewo-stommer/tests/Feature/ReservationWorkflowTest.php
T
LarsandCursor 8ebc1a3878 Assign the existing site to one landlord so stommer.info can run tenancy.
Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 12:00:11 +02:00

155 lines
6.3 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\ConfirmBooking;
use App\Actions\CreateReservation;
use App\Actions\ExpireReservations;
use App\Availability\AvailabilityChecker;
use App\Enums\BookingStatus;
use App\Exceptions\ReservationException;
use App\Mail\BookingConfirmed;
use App\Mail\NewReservation;
use App\Mail\ReservationExpired;
use App\Mail\ReservationReceived;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Property;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class ReservationWorkflowTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed(PropertySeeder::class);
$this->property = Property::sole();
Mail::fake();
}
public function test_active_reservations_and_blocked_periods_occupy_the_calendar_but_allow_changeover(): void
{
Booking::factory()->for($this->property)->reserved(now()->addHour())->create(['arrival' => '2026-11-10', 'departure' => '2026-11-14']);
BlockedPeriod::factory()->for($this->property)->create(['starts_on' => '2026-11-20', 'ends_on' => '2026-11-22']);
$availability = new AvailabilityChecker;
$this->assertFalse($availability->isAvailable($this->property, $this->date('2026-11-12'), $this->date('2026-11-16')));
$this->assertFalse($availability->isAvailable($this->property, $this->date('2026-11-18'), $this->date('2026-11-21')));
$this->assertTrue($availability->isAvailable($this->property, $this->date('2026-11-14'), $this->date('2026-11-16')), 'arrival on departure day');
$this->assertTrue($availability->isAvailable($this->property, $this->date('2026-11-07'), $this->date('2026-11-10')), 'departure on arrival day');
$this->assertSame(['2026-11-10', '2026-11-11', '2026-11-12', '2026-11-13', '2026-11-20', '2026-11-21'], array_keys(
$availability->occupiedNights($this->property, $this->date('2026-11-01'), $this->date('2026-12-01')),
));
}
public function test_reservation_with_elapsed_hold_no_longer_occupies_the_calendar(): void
{
Booking::factory()->for($this->property)->reserved(now()->subMinute())->create(['arrival' => '2026-11-10', 'departure' => '2026-11-14']);
$this->assertTrue((new AvailabilityChecker)->isAvailable($this->property, $this->date('2026-11-10'), $this->date('2026-11-14')));
}
public function test_request_creates_a_held_reservation_and_sends_quote_and_notification(): void
{
$booking = $this->reserve('2026-11-07', '2026-11-10');
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->assertSame('2026-10-24 10:00', $booking->hold_expires_at->format('Y-m-d H:i'));
$this->assertSame(3 * 6500 + 10800, $booking->total);
$this->assertSame('2026-0001', $booking->reference);
$this->assertSame('anna@example.com', $booking->guest->email);
Mail::assertSent(ReservationReceived::class, fn (ReservationReceived $mail): bool => $mail->hasTo('anna@example.com'));
Mail::assertSent(NewReservation::class, fn (NewReservation $mail): bool => $mail->hasTo(config('fewo.notification_email')));
}
public function test_request_for_occupied_dates_is_rejected(): void
{
$this->reserve('2026-11-07', '2026-11-10');
$this->expectException(ReservationException::class);
$this->reserve('2026-11-09', '2026-11-12');
}
public function test_returning_guest_is_recognised_by_email(): void
{
$first = $this->reserve('2026-11-07', '2026-11-10');
$second = $this->reserve('2026-11-20', '2026-11-23', 'ANNA@example.com');
$this->assertTrue($first->guest->is($second->guest));
}
public function test_expired_holds_are_marked_and_the_landlord_is_notified(): void
{
$booking = $this->reserve('2026-11-07', '2026-11-10');
$this->travel(47)->hours();
$this->assertSame(0, (new ExpireReservations)->handle());
$this->travel(2)->hours();
$this->assertSame(1, (new ExpireReservations)->handle());
$this->assertSame(BookingStatus::Expired, $booking->fresh()->status);
Mail::assertSent(ReservationExpired::class);
}
public function test_confirmation_makes_the_booking_binding_and_informs_the_guest(): void
{
$booking = $this->reserve('2026-11-07', '2026-11-10');
app(ConfirmBooking::class)->handle($booking);
$booking->refresh();
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertNull($booking->hold_expires_at);
$this->assertNotNull($booking->confirmed_at);
Mail::assertSent(BookingConfirmed::class, fn (BookingConfirmed $mail): bool => $mail->hasTo('anna@example.com'));
}
public function test_expired_reservation_cannot_be_confirmed_once_the_dates_are_taken(): void
{
$expired = $this->reserve('2026-11-07', '2026-11-10');
$this->travel(3)->days();
(new ExpireReservations)->handle();
$this->reserve('2026-11-08', '2026-11-12', 'other@example.com');
$this->expectException(ReservationException::class);
app(ConfirmBooking::class)->handle($expired->fresh());
}
public function test_mails_render(): void
{
$booking = $this->reserve('2026-11-07', '2026-11-10');
$this->assertStringContainsString('vorgemerkt', (new ReservationReceived($booking))->render());
$this->assertStringContainsString($booking->reference, (new NewReservation($booking))->render());
$this->assertStringContainsString('bestätigen', (new BookingConfirmed($booking))->render());
}
private function reserve(string $arrival, string $departure, string $email = 'anna@example.com'): Booking
{
return app(CreateReservation::class)->handle(
$this->property,
new StayRequest($this->date($arrival), $this->date($departure), 2),
['first_name' => 'Anna', 'last_name' => 'Muster', 'email' => $email],
);
}
private function date(string $date): CarbonImmutable
{
return CarbonImmutable::parse($date);
}
}