Files
fewo-stommer/tests/Feature/PaymentWorkflowTest.php
T
Lars StommerandClaude Opus 5.5 0bf302fcf9 Phase 3: Zahlungen, Mietvertrag, Erinnerungen, Anreiseinformationen
- Zahlungen erfassen (inkl. Erstattungen), Status automatisch: Bestätigt → Anzahlung erhalten → Bezahlt → Abgeschlossen
- Anzahlungsfrist bei Bestätigung; kurzfristige Buchungen: Gesamtbetrag sofort
- Mietvertrag als PDF (dompdf, Font-Subsetting), bei Bestätigung gespeichert und angehängt; Texte im Admin (Muster)
- Täglich: Zahlungserinnerungen (Anzahlung/Rest überfällig, Kopie an Vermieter), Zahlungsaufforderung Rest 7 Tage vorher, Anreiseinfos 7 Tage vorher, Abschluss nach Abreise
- Admin: Zahlungen je Buchung, offene Beträge, Vertrag herunterladen/neu erzeugen

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 13:29:41 +02:00

142 lines
5.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\ConfirmBooking;
use App\Actions\CreateReservation;
use App\Actions\RecordPayment;
use App\Actions\SendBookingReminders;
use App\Enums\BookingStatus;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Filament\Resources\Bookings\RelationManagers\PaymentsRelationManager;
use App\Mail\ArrivalInformation;
use App\Mail\BookingConfirmed;
use App\Mail\PaymentReminder;
use App\Models\Booking;
use App\Models\Property;
use App\Models\User;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
class PaymentWorkflowTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
Storage::fake('local');
Mail::fake();
$this->seed(PropertySeeder::class);
$this->property = Property::sole();
}
public function test_confirmation_fixes_due_dates_and_sends_the_contract(): void
{
$booking = $this->confirmed('2026-12-01', '2026-12-05');
$this->assertSame('2026-10-29', $booking->deposit_due_on->toDateString());
$this->assertFalse($booking->requiresFullPayment());
Storage::disk('local')->assertExists($booking->contract_path);
$this->assertStringStartsWith('%PDF', Storage::disk('local')->get($booking->contract_path));
Mail::assertSent(BookingConfirmed::class, fn (BookingConfirmed $mail): bool => count($mail->attachments()) === 1);
}
public function test_short_term_booking_requires_full_payment(): void
{
$booking = $this->confirmed('2026-11-07', '2026-11-10');
$this->assertTrue($booking->requiresFullPayment());
$this->assertStringContainsString('Gesamtbetrag', (new BookingConfirmed($booking))->render());
}
public function test_payments_move_the_booking_through_its_statuses(): void
{
$booking = $this->confirmed('2026-12-01', '2026-12-05');
$record = new RecordPayment;
$record->handle($booking, $booking->deposit, today());
$this->assertSame(BookingStatus::DepositPaid, $booking->fresh()->status);
$record->handle($booking->fresh(), $booking->total - $booking->deposit, today());
$this->assertSame(BookingStatus::Paid, $booking->fresh()->status);
$this->assertSame(0, $booking->fresh()->openAmount());
$record->handle($booking->fresh(), -5000, today(), note: 'Erstattung');
$this->assertSame(BookingStatus::DepositPaid, $booking->fresh()->status);
}
public function test_reminders_are_sent_once_at_the_right_time(): void
{
$booking = $this->confirmed('2026-12-01', '2026-12-05');
$reminders = new SendBookingReminders;
$this->travelTo(CarbonImmutable::parse('2026-10-30 09:00'));
$this->assertSame(1, $reminders->handle()['deposit_overdue']);
$this->assertSame(0, $reminders->handle()['deposit_overdue']);
Mail::assertSent(PaymentReminder::class, fn (PaymentReminder $mail): bool => $mail->kind === PaymentReminder::DEPOSIT_OVERDUE && $mail->hasBcc(config('fewo.notification_email')));
(new RecordPayment)->handle($booking, $booking->deposit, today());
$this->travelTo(CarbonImmutable::parse('2026-10-26 09:00'));
$this->assertSame(0, $reminders->handle()['balance_request'], 'balance is due on 03.11.; request 7 days before');
$this->travelTo(CarbonImmutable::parse('2026-10-27 09:00'));
$this->assertSame(1, $reminders->handle()['balance_request']);
$this->travelTo(CarbonImmutable::parse('2026-11-04 09:00'));
$this->assertSame(1, $reminders->handle()['balance_overdue']);
$this->travelTo(CarbonImmutable::parse('2026-11-24 09:00'));
$this->assertSame(1, $reminders->handle()['arrival_info']);
Mail::assertSent(ArrivalInformation::class);
(new RecordPayment)->handle($booking->fresh(), $booking->fresh()->openAmount(), today());
$this->travelTo(CarbonImmutable::parse('2026-12-06 09:00'));
$this->assertSame(1, $reminders->handle()['completed']);
$this->assertSame(BookingStatus::Completed, $booking->fresh()->status);
}
public function test_payment_is_recorded_and_contract_downloaded_in_the_admin(): void
{
$this->actingAs(User::factory()->create());
$booking = $this->confirmed('2026-12-01', '2026-12-05');
Livewire::test(PaymentsRelationManager::class, ['ownerRecord' => $booking, 'pageClass' => ViewBooking::class])
->callAction(TestAction::make('record')->table(), [
'amount' => number_format($booking->deposit / 100, 2, '.', ''),
'paid_on' => '2026-10-25',
'method' => 'transfer',
])
->assertHasNoFormErrors();
$this->assertSame(BookingStatus::DepositPaid, $booking->fresh()->status);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('contract')
->assertFileDownloaded("Mietvertrag-{$booking->reference}.pdf");
}
private function confirmed(string $arrival, string $departure): Booking
{
$booking = app(CreateReservation::class)->handle(
$this->property,
new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2),
['first_name' => 'Anna', 'last_name' => 'Muster', 'email' => 'anna@example.com', 'city' => 'Köln'],
);
app(ConfirmBooking::class)->handle($booking);
return $booking->fresh();
}
}