You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/TicketIdempotencyTest.php

98 lines
3.5 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Ticket\OpenTicketCommand;
use App\Services\Ticket\TicketIdempotencyConflict;
use App\Services\Ticket\TicketThreadService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
if (!defined('BASE_PATH')) {
define('BASE_PATH', sys_get_temp_dir() . '/virt-ticket-idempotency-' . bin2hex(random_bytes(5)));
}
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$request = static function (): object {
return new class {
public function getUploadedFiles(): array { return []; }
};
};
$capsule = new Capsule();
$capsule->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('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('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";