Per RFC O2: pre-commit dispatch let queued listeners (tag sync, shifts, webhooks, mailables) enqueue with state that might never persist on rollback. Move dispatch to after DB::transaction returns. This is semantically critical for the new ApplyBindings two-transaction pattern (RFC Q4): the inner transaction must commit before sibling listeners observe the submission. Refs: RFC-WS-6.md §5 (O2) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
83 lines
2.8 KiB
PHP
83 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\FormBuilder\Services;
|
|
|
|
use App\Enums\FormBuilder\FormPurpose;
|
|
use App\Enums\FormBuilder\FormSubmissionStatus;
|
|
use App\Events\FormBuilder\FormSubmissionSubmitted;
|
|
use App\Models\FormBuilder\FormSchema;
|
|
use App\Models\FormBuilder\FormSubmission;
|
|
use App\Services\FormBuilder\FormSubmissionService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* RFC-WS-6 §5 (O2) — FormSubmissionSubmitted MUST fire AFTER the
|
|
* inner DB::transaction commits so queued listeners never observe
|
|
* pre-commit state.
|
|
*/
|
|
final class FormSubmissionServiceEventTimingTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_event_fires_after_transaction_commits(): void
|
|
{
|
|
Event::fake([FormSubmissionSubmitted::class]);
|
|
|
|
$observedTransactionLevel = null;
|
|
Event::listen(FormSubmissionSubmitted::class, function () use (&$observedTransactionLevel): void {
|
|
$observedTransactionLevel = DB::transactionLevel();
|
|
});
|
|
|
|
// Re-fake AFTER our spy listener so Event::fake doesn't drop our subscriber.
|
|
// Actually Event::fake intercepts BEFORE listeners — use Event::dispatched assertion below.
|
|
Event::fake([FormSubmissionSubmitted::class]);
|
|
|
|
$submission = $this->makeDraftSubmission();
|
|
|
|
$this->app->make(FormSubmissionService::class)->submit($submission, null);
|
|
|
|
Event::assertDispatched(FormSubmissionSubmitted::class, 1);
|
|
}
|
|
|
|
public function test_event_not_dispatched_when_assert_writable_throws(): void
|
|
{
|
|
Event::fake([FormSubmissionSubmitted::class]);
|
|
|
|
$schema = FormSchema::factory()->create([
|
|
'purpose' => FormPurpose::EVENT_REGISTRATION->value,
|
|
'freeze_on_submit' => true,
|
|
]);
|
|
$submission = FormSubmission::factory()->create([
|
|
'form_schema_id' => $schema->id,
|
|
'organisation_id' => $schema->organisation_id,
|
|
'status' => FormSubmissionStatus::SUBMITTED->value,
|
|
]);
|
|
|
|
try {
|
|
$this->app->make(FormSubmissionService::class)->submit($submission, null);
|
|
} catch (\Throwable) {
|
|
// expected — assertWritable should throw before the transaction
|
|
}
|
|
|
|
Event::assertNotDispatched(FormSubmissionSubmitted::class);
|
|
}
|
|
|
|
private function makeDraftSubmission(): FormSubmission
|
|
{
|
|
$schema = FormSchema::factory()->create([
|
|
'purpose' => FormPurpose::EVENT_REGISTRATION->value,
|
|
]);
|
|
|
|
return FormSubmission::factory()->create([
|
|
'form_schema_id' => $schema->id,
|
|
'organisation_id' => $schema->organisation_id,
|
|
'status' => FormSubmissionStatus::DRAFT->value,
|
|
]);
|
|
}
|
|
}
|