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(); }); $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('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('user_id'); $table->string('status'); }); $service = new TicketThreadService(); Capsule::table('ticket')->insert([ 'id' => 1, 'title' => 'Root ticket', 'content' => 'Initial message', 'rootid' => 0, 'userid' => 7, 'datetime' => time(), 'status' => 1, 'ticket_type' => 'technical', 'priority' => 'medium', ]); $png = base64_decode( 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk' . 'YAAAAAYAAjCB0C8AAAAASUVORK5CYII=', true ); $uploaded = static function (string $name, string $bytes): array { $path = tempnam(sys_get_temp_dir(), 'ticket-upload-'); if ($path === false || file_put_contents($path, $bytes) === false) { throw new RuntimeException('Could not create uploaded fixture.'); } return [ 'name' => $name, 'tmp_name' => $path, 'error' => UPLOAD_ERR_OK, 'size' => filesize($path), 'type' => 'image/png', ]; }; $request = static function (array $files): object { return new class($files) { public function __construct(private array $files) {} public function getUploadedFiles(): array { return ['attachments' => $this->files]; } }; }; // A user may only mutate a thread that they own. The missing result is the // public behavior used by the HTTP adapter to return "ticket not found" // without revealing another user's ticket. $foreignResult = $service->reply(new ReplyTicketCommand( 1, 8, 'must not be saved', 0, $request([]), 8 )); $assert($foreignResult === null, 'A foreign user was able to reply to the ticket.'); $assert(Capsule::table('ticket')->where('rootid', 1)->count() === 0, 'The rejected foreign reply wrote a message row.'); // A later invalid attachment must roll back the root status and reply and // remove the first file that was already moved into private storage. $first = $uploaded('first.png', $png); $secondPath = tempnam(sys_get_temp_dir(), 'ticket-upload-invalid-'); if ($secondPath === false || file_put_contents($secondPath, 'not an image') === false) { throw new RuntimeException('Could not create invalid upload fixture.'); } $second = [ 'name' => 'second.png', 'tmp_name' => $secondPath, 'error' => UPLOAD_ERR_OK, 'size' => filesize($secondPath), 'type' => 'image/png', ]; try { $service->reply(new ReplyTicketCommand( 1, 7, 'reply that must roll back', 0, $request([$first, $second]), 7 )); throw new RuntimeException('Invalid attachment unexpectedly succeeded.'); } catch (InvalidArgumentException $exception) { // Expected validation failure; the assertions below verify the rollback. } $assert((int) Capsule::table('ticket')->where('id', 1)->value('status') === 1, 'Root status changed after attachment validation failure.'); $assert(Capsule::table('ticket')->where('rootid', 1)->count() === 0, 'Reply row survived attachment validation failure.'); $assert(Capsule::table('ticket_attachments')->count() === 0, 'Attachment row survived transaction rollback.'); $assert(glob(BASE_PATH . '/storage/ticket-attachments/*') === [], 'A partially stored attachment file survived rollback.'); @unlink($secondPath); $result = $service->reply(new ReplyTicketCommand( 1, 7, 'successful reply', 0, $request([$uploaded('ok.png', $png)]), 7 )); $assert($result !== null && $result->message->rootid == 1, 'Successful reply was not created.'); $assert((int) Capsule::table('ticket')->where('id', 1)->value('status') === 0, 'Root status was not updated by a successful reply.'); $assert(Capsule::table('ticket')->where('rootid', 1)->count() === 1, 'Successful reply row is missing.'); $assert(Capsule::table('ticket_attachments')->count() === 1, 'Successful attachment row is missing.'); $attachment = Capsule::table('ticket_attachments')->first(); $assert((string) $attachment->original_name === 'ok.png', 'Successful attachment did not preserve its sanitized original name.'); $assert(is_file(BASE_PATH . '/storage/ticket-attachments/' . $attachment->storage_key), 'Successful attachment file is missing.'); // Closing and reopening are observable thread behaviors. Reopening a closed // thread has its own event so notification adapters can explain the change. $reopened = $service->reply(new ReplyTicketCommand(1, 7, 'reopen', 1, $request([]), 7)); $assert($reopened !== null && $reopened->event === '工单重新开启', 'A closed ticket did not emit the reopen event.'); $assert((int) Capsule::table('ticket')->where('id', 1)->value('status') === 1, 'The reopened ticket root is not open.'); // The lock must be acquired in the transaction, not by a preflight query. $serviceSource = file_get_contents(dirname(__DIR__) . '/app/Services/Ticket/TicketThreadService.php'); $assert(str_contains($serviceSource, 'transaction(function') && str_contains($serviceSource, 'lockForUpdate()->first()') && str_contains($serviceSource, 'TicketAttachmentService::store'), 'Reply service does not keep root locking and attachment persistence in one transaction.'); $attachmentSource = file_get_contents(dirname(__DIR__) . '/app/Services/TicketAttachmentService.php'); $assert(str_contains($attachmentSource, '$paths[] = $path') && str_contains($attachmentSource, 'self::discardPaths($paths)'), 'Attachment writes do not register and clean up every newly-created path.'); foreach ([ dirname(__DIR__) . '/app/Controllers/UserController.php', dirname(__DIR__) . '/app/Controllers/Admin/TicketController.php', ] as $controllerPath) { $controllerSource = file_get_contents($controllerPath); $assert(str_contains($controllerSource, 'TicketThreadService') && !str_contains($controllerSource, '$previousStatus'), 'A ticket controller still uses the stale-status compensation flow.'); } Capsule::table('cloud_instances')->insert(['id' => 31, 'user_id' => 7, 'status' => 'running']); $opened = $service->open(new OpenTicketCommand( 7, 'Provisioning issue', 'Cannot boot', 'technical', 'high', 31, $request([]) )); $assert($opened->root->rootid == 0 && $opened->message->id === $opened->root->id, 'Opening a ticket did not create the root thread message.'); $assert((int) $opened->root->instance_id === 31, 'Opening a ticket lost the owned instance association.'); $assert(!str_contains((string) $opened->root->content, '