Files

113 lines
6.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Feature\Process;
use App\Enums\BookingStatus;
use App\Enums\ProcessSetting;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\Property;
use App\Pricing\PriceCalculator;
use App\Pricing\StayRequest;
use App\Process\CaseContext;
use App\Process\Modules\ContributesToPrice;
use App\Process\Modules\ModuleDefinition;
use App\Process\Modules\RunsAutomatically;
use App\Process\ProcessCatalogue;
use App\Process\ProcessPlan;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
/**
* The generic contract test of every registered module (docs/Prozessarchitektur/Architektur/02 §3.2): its definition is
* complete and consistent, and its reading methods (applies, activities, state, due, blockers, price lines) never
* write – for cases of every archetype and status. What a run may cause is checked per module in the runner, module and
* sandbox tests; this test only promises what it checks.
*/
#[Group('contract')]
class ModuleContractTest extends TestCase
{
use RefreshDatabase;
public function test_every_module_has_a_complete_definition(): void
{
$catalogue = app(ProcessCatalogue::class);
$phases = ['P01', 'P02', 'P03', 'P04', 'P05'];
$toggles = [ModuleDefinition::TOGGLE_MANDATORY, ModuleDefinition::TOGGLE_OPTIONAL, ModuleDefinition::TOGGLE_CONDITIONAL, ModuleDefinition::TOGGLE_EVENT_DRIVEN];
$effects = ['case_write', 'guest_mail', 'landlord_mail', 'document', 'calendar_hold', 'channel_task', 'network'];
foreach ($catalogue->modules() as $key => $class) {
$definition = $class::definition();
$this->assertMatchesRegularExpression('/^[a-z0-9_]+(\.[a-z0-9_]+)*$/', $key);
$this->assertNotSame('', trim($definition->label), "{$key}: label");
$this->assertNotSame('', trim($definition->purpose), "{$key}: purpose");
$this->assertContains($definition->phase, $phases, "{$key}: phase");
$this->assertContains($definition->toggle, $toggles, "{$key}: toggle");
$this->assertSame([], array_diff($definition->effects, $effects), "{$key}: unknown effect kinds");
$this->assertGreaterThanOrEqual(1, $class::version());
foreach ($definition->parameters as $parameter) {
$this->assertTrue($parameter instanceof ProcessSetting || (is_string($parameter) && $parameter !== ''), "{$key}: parameter");
}
if (is_subclass_of($class, RunsAutomatically::class)) {
$this->assertNotSame([], $definition->modes, "{$key}: an automatic module names its modes");
$this->assertNotSame([], $definition->triggers, "{$key}: an automatic module names its triggers");
}
if ($definition->toggle === ModuleDefinition::TOGGLE_MANDATORY && $definition->executions !== []) {
$this->assertNotNull($definition->why, "{$key}: a mandatory duty states why (shown instead of a lock)");
}
if ($definition->isTesterOnly()) {
$this->assertNotSame([], $definition->points, "{$key}: a tester-only module sits at an extension point");
}
}
}
public function test_reading_methods_never_write(): void
{
$this->travelTo(CarbonImmutable::parse('2026-10-23 10:00'));
$this->seed(PropertySeeder::class);
$property = Property::sole();
$portal = Channel::factory()->for($property)->create(['booking_model' => 'portal_booking', 'process_overrides' => ['booking' => ['payments' => 'portal']]]);
$cases = [];
foreach ([BookingStatus::Reserved, BookingStatus::OfferSent, BookingStatus::Confirmed, BookingStatus::Completed, BookingStatus::Cancelled] as $index => $status) {
$cases[] = Booking::factory()->for($property)->create(['status' => $status, 'intake' => 'inquiry', 'arrival' => now()->addDays(40 + $index * 10)->toDateString(), 'departure' => now()->addDays(44 + $index * 10)->toDateString(), 'hold_expires_at' => now()->addDay()]);
$cases[] = Booking::factory()->for($property)->create(['status' => $status === BookingStatus::Reserved ? BookingStatus::Confirmed : $status, 'intake' => 'booking', 'import_channel_id' => $portal->id, 'origin_channel_id' => $portal->id, 'arrival' => now()->addDays(100 + $index * 10)->toDateString(), 'departure' => now()->addDays(104 + $index * 10)->toDateString()]);
}
$catalogue = app(ProcessCatalogue::class);
$writes = [];
DB::listen(function (QueryExecuted $query) use (&$writes): void {
if (preg_match('/^\s*(insert|update|delete|replace)\b/i', $query->sql) === 1) {
$writes[] = $query->sql;
}
});
foreach ($cases as $booking) {
$booking = $booking->fresh();
// Every module against every case: also modules of other models must read without side effects.
$plan = new ProcessPlan('a1.offer', 1, 'loyalty', array_map(fn (string $class): int => $class::version(), $catalogue->modules()), [], ProcessPlan::TESTER);
$context = new CaseContext($booking, $plan);
foreach (array_keys($catalogue->modules()) as $key) {
$module = $catalogue->module($key);
$module->applies($context);
$module->activities($context);
$module->state($context);
if ($module instanceof RunsAutomatically) {
$module->due($context);
$module->blockers($context);
$module->isOutward();
}
if ($module instanceof ContributesToPrice && $booking->arrival->lessThan(now()->addDays(55))) {
$module->priceLines($context, app(PriceCalculator::class)->calculate($property, new StayRequest($booking->arrival, $booking->departure, 2)));
}
}
}
$this->assertSame([], $writes, 'reading methods of modules must not write');
}
}