Files
fewo-stommer/tests/Feature/Process/ChannelEventContractTest.php
T

182 lines
9.7 KiB
PHP

<?php
namespace Tests\Feature\Process;
use App\Actions\ConfigureChannelProcess;
use App\Actions\ProcessPortalMail;
use App\Channels\ChannelRegistry;
use App\Channels\Events\BookingCancelledByChannel;
use App\Channels\Events\BookingChangedByChannel;
use App\Channels\Events\ChannelEventHandler;
use App\Channels\Events\IncomingBooking;
use App\Channels\Events\PaymentReported;
use App\Channels\Events\PayoutReported;
use App\Channels\Events\Provenance;
use App\Channels\Intake\IntakeDecision;
use App\Channels\Mail\PortalMail;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\IntakeKind;
use App\Enums\IntakeOutcome;
use App\Enums\PaymentKind;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Models\Channel;
use App\Models\Property;
use App\Process\OutwardSuspension;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Process\FakeApiConnector;
use Tests\TestCase;
/**
* Package U-E (K4, K10, N7): normalised channel events of a future portal interface are mapped onto the existing intake.
* A report is never a business action: no cancellation, change, refund or payment on our account by itself; and one
* booking stays one case whether it arrives by API, mail or calendar.
*/
#[Group('channel-events')]
class ChannelEventContractTest extends TestCase
{
use RefreshDatabase;
private Channel $fewo;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed(PropertySeeder::class);
Mail::fake();
$this->fewo = Channel::factory()->for(Property::sole())->create(['name' => 'FeWo-direkt', 'booking_model' => 'mixed', 'case_intake' => 'create', 'import_enabled' => false,
'settings' => ['import_url' => 'https://www.fewo-direkt.de/icalendar/x.ics', 'email_senders' => 'fewo-direkt.de']]);
app(ConfigureChannelProcess::class)->handle($this->fewo, IntakeKind::Booking, ['model' => 'a4.portal_booking']);
$this->fewo->refresh();
}
public function test_an_api_booking_becomes_one_case_whatever_the_transport_and_order(): void
{
$first = $this->handle(new IncomingBooking($this->provenance('HA-API1'), CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10')));
$this->assertSame(IntakeOutcome::Create, $first->outcome, implode(' ', $first->reasons));
$case = Booking::query()->sole();
$this->assertSame(['a4.portal_booking', BookingStatus::Confirmed, 'HA-API1'], [$case->profile['plan']['model'], $case->status, $case->portal_reference]);
// The same event again, then the booking mail of the same booking: still one case.
$this->assertSame(IntakeOutcome::Link, $this->handle(new IncomingBooking($this->provenance('HA-API1'), CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10')))->outcome);
app(ProcessPortalMail::class)->handle(new PortalMail('<m1@fewo-direkt.de>', 'noreply@fewo-direkt.de', 'Neue Buchung', "Anreise: 07.11.2026\nAbreise: 10.11.2026\nBuchungsnummer: HA-API1", CarbonImmutable::now()));
$this->assertSame(1, Booking::query()->count());
}
public function test_a_mail_first_and_the_api_event_afterwards_stay_one_case(): void
{
app(ProcessPortalMail::class)->handle(new PortalMail('<m2@fewo-direkt.de>', 'noreply@fewo-direkt.de', 'Neue Buchung', "Anreise: 14.11.2026\nAbreise: 18.11.2026\nBuchungsnummer: HA-API2", CarbonImmutable::now()));
$this->assertSame(1, Booking::query()->count());
$decision = $this->handle(new IncomingBooking($this->provenance('HA-API2'), CarbonImmutable::parse('2026-11-14'), CarbonImmutable::parse('2026-11-18')));
$this->assertSame(IntakeOutcome::Link, $decision->outcome);
$this->assertSame(1, Booking::query()->count());
}
public function test_an_api_booking_without_reference_or_with_another_reference_on_the_same_nights_is_clarified(): void
{
$this->assertSame(IntakeOutcome::Clarify, $this->handle(new IncomingBooking($this->provenance(null), CarbonImmutable::parse('2026-12-01'), CarbonImmutable::parse('2026-12-05')))->outcome);
$this->assertSame(0, Booking::query()->count());
$this->handle(new IncomingBooking($this->provenance('HA-A'), CarbonImmutable::parse('2026-12-10'), CarbonImmutable::parse('2026-12-14')));
$this->assertSame(IntakeOutcome::Clarify, $this->handle(new IncomingBooking($this->provenance('HA-B'), CarbonImmutable::parse('2026-12-10'), CarbonImmutable::parse('2026-12-14')))->outcome);
$this->assertSame(1, Booking::query()->count());
}
public function test_a_cancellation_report_is_a_task_and_never_a_cancellation(): void
{
$case = $this->booking('HA-C');
$decision = $this->handle(new BookingCancelledByChannel($this->provenance('HA-C', 'evt-1'), 'vom Gast im Portal'));
$this->handle(new BookingCancelledByChannel($this->provenance('HA-C', 'evt-1'), 'vom Gast im Portal'));
$case->refresh();
$this->assertSame(IntakeOutcome::CancellationSignal, $decision->outcome);
$this->assertSame(BookingStatus::Confirmed, $case->status, 'the booking stays binding');
$this->assertNull($case->cancelled_at);
$this->assertSame(1, BookingTask::query()->where('booking_id', $case->id)->where('kind', BookingTaskKind::PortalCancellationReported)->count(), 'one task, also when repeated');
$this->assertFalse($case->payments()->where('kind', PaymentKind::Refund)->exists());
$this->assertNotNull(OutwardSuspension::reason($case), 'O4: automatic messages wait');
$this->assertTrue(Booking::query()->occupying()->whereKey($case->id)->exists(), 'the calendar stays blocked');
$this->assertSame(IntakeOutcome::Clarify, $this->handle(new BookingCancelledByChannel($this->provenance('UNBEKANNT', 'evt-2')))->outcome, 'an unassigned report stops nothing');
}
public function test_a_change_report_leaves_the_booking_as_it_is(): void
{
$case = $this->booking('HA-D');
$this->handle(new BookingChangedByChannel($this->provenance('HA-D', 'evt-3'), CarbonImmutable::parse('2026-11-08'), CarbonImmutable::parse('2026-11-12'), 3));
$case->refresh();
$this->assertSame(['2026-11-07', '2026-11-10'], [$case->arrival->toDateString(), $case->departure->toDateString()]);
$this->assertTrue(BookingTask::query()->where('booking_id', $case->id)->where('kind', BookingTaskKind::PortalChangeReported)->exists());
}
public function test_portal_payments_and_payouts_are_only_stated_by_the_portal(): void
{
$case = $this->booking('HA-E');
$this->handle(new PaymentReported($this->provenance('HA-E', 'p1'), 39061, CarbonImmutable::parse('2026-10-22')));
$this->handle(new PaymentReported($this->provenance('HA-E', 'p1'), 39061, CarbonImmutable::parse('2026-10-22')));
$this->handle(new PayoutReported($this->provenance('HA-E', 'p2'), 89469, CarbonImmutable::parse('2026-11-12'), executed: true));
$case->refresh();
$this->assertSame(1, $case->payments()->where('kind', PaymentKind::GuestViaPortal)->count(), 'recorded once, "laut Portal"');
$this->assertSame(0, $case->payments()->whereIn('kind', [PaymentKind::Guest, PaymentKind::PortalPayout])->count(), 'never money on our account by a report');
$this->assertTrue($case->events()->where('type', 'evidence')->exists(), 'the payout is evidence "laut Portal"');
}
public function test_only_portals_deliver_events(): void
{
$this->expectException(InvalidArgumentException::class);
$this->handle(new PaymentReported(new Provenance(Channel::websiteOf(Property::sole()), 'api', Provenance::AUTHENTICATED, 'X', 'e', CarbonImmutable::now()), 1, CarbonImmutable::now()));
}
/** N7: a new portal interface needs only its adapter and its registry entry. */
public function test_a_new_api_adapter_uses_the_same_intake(): void
{
config(['channels.connectors' => [...config('channels.connectors'), FakeApiConnector::class]]);
Http::fake(['api.portal.example/*' => Http::response(['bookings' => [['reference' => 'HA-N7', 'arrival' => '2026-11-21', 'departure' => '2026-11-24', 'guests' => 2, 'paid' => 10000, 'paid_on' => '2026-10-22']]])]);
$this->assertArrayHasKey('fake_api', app(ChannelRegistry::class)->connectors());
foreach (app(FakeApiConnector::class)->fetchEvents($this->fewo, null) as $event) {
$this->handle($event);
}
foreach (app(FakeApiConnector::class)->fetchEvents($this->fewo, null) as $event) {
$this->handle($event);
}
$case = Booking::query()->sole();
$this->assertSame(['HA-N7', 'a4.portal_booking'], [$case->portal_reference, $case->profile['plan']['model']]);
$this->assertSame(1, $case->payments()->where('kind', PaymentKind::GuestViaPortal)->count());
}
private function booking(string $reference): Booking
{
$this->handle(new IncomingBooking($this->provenance($reference), CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10')));
return Booking::query()->where('portal_reference', $reference)->sole();
}
private function provenance(?string $reference, string $evidence = 'evt'): Provenance
{
return new Provenance($this->fewo, 'api', Provenance::AUTHENTICATED, $reference, $evidence.'-'.($reference ?? 'none'), CarbonImmutable::now());
}
private function handle(object $event): ?IntakeDecision
{
return app(ChannelEventHandler::class)->handle($event);
}
}