addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->getConnection('default')->getSchemaBuilder(); $schema->create('ticket', static function (Blueprint $table): void { $table->increments('id'); $table->text('title'); $table->text('content'); $table->integer('rootid')->default(0); $table->integer('userid'); $table->integer('datetime'); $table->integer('status')->default(1); $table->string('ticket_type')->default('technical'); $table->string('priority')->default('medium'); $table->integer('instance_id')->nullable(); $table->integer('last_activity_at')->default(0); $table->integer('last_message_id')->default(0); $table->integer('last_actor_id')->default(0); $table->boolean('last_actor_is_admin')->default(false); }); $schema->create('ticket_attachments', static function (Blueprint $table): void { $table->increments('id'); $table->integer('ticket_id'); $table->integer('message_id'); $table->integer('user_id'); $table->string('storage_key'); $table->string('original_name'); $table->string('mime_type'); $table->integer('bytes'); $table->string('sha256'); $table->integer('created_at'); }); $schema->create('ticket_write_requests', static function (Blueprint $table): void { $table->increments('id'); $table->integer('actor_user_id'); $table->string('request_key'); $table->string('request_hash'); $table->string('operation'); $table->integer('ticket_id')->nullable(); $table->integer('message_id')->nullable(); $table->integer('created_at'); $table->unique(['actor_user_id', 'request_key']); }); $schema->create('ticket_user_guards', static function (Blueprint $table): void { $table->integer('user_id')->primary(); $table->integer('updated_at'); }); $schema->create('ticket_read_states', static function (Blueprint $table): void { $table->integer('ticket_id'); $table->integer('reader_id'); $table->integer('last_read_message_id'); $table->integer('read_at'); $table->primary(['ticket_id', 'reader_id']); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('user_id'); $table->string('status'); }); $service = new TicketThreadService(); $key = 'ticket-open-1234567890'; $command = new OpenTicketCommand( 7, 'Network issue', 'Cannot connect', 'technical', 'high', null, $request(), $key ); $first = $service->open($command); $again = $service->open($command); $assert($first->wasCreated && !$again->wasCreated, 'An idempotent replay is not distinguished from the original write.'); $assert($again->root->id === $first->root->id && $again->message->id === $first->message->id, 'An idempotent replay did not return the original result.'); $assert(Capsule::table('ticket')->count() === 1 && Capsule::table('ticket_write_requests')->count() === 1, 'An idempotent replay duplicated the ticket or intent.'); try { $service->open(new OpenTicketCommand( 7, 'Changed title', 'Cannot connect', 'technical', 'high', null, $request(), $key )); throw new RuntimeException('A changed request reused an existing idempotency key.'); } catch (TicketIdempotencyConflict $exception) { $assert($exception->getCode() === 409, 'Idempotency conflict is not an HTTP 409 domain error.'); } echo "Ticket idempotency checks passed.\n";