addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_hosts', static function (Blueprint $table): void { $table->increments('id'); $table->string('name'); $table->string('region'); $table->string('country_code', 2); $table->string('default_network'); $table->boolean('enabled'); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_host_resource_pool_memberships', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('resource_pool_id'); $table->string('scheduler_state'); $table->integer('priority'); $table->integer('weight'); $table->integer('created_at'); $table->integer('updated_at'); $table->unique(['host_id', 'resource_pool_id']); }); $assert(!$schema->hasColumn('cloud_hosts', 'resource_pool_id') && !$schema->hasColumn('cloud_hosts', 'scheduler_state'), 'Host projection regression schema accidentally restored retired columns.'); Capsule::table('cloud_hosts')->insert([ ['id' => 2, 'name' => 'Host B', 'region' => 'Tokyo', 'country_code' => 'JP', 'default_network' => 'public', 'enabled' => 0, 'created_at' => 20, 'updated_at' => 20], ['id' => 1, 'name' => 'Host A', 'region' => 'HongKong', 'country_code' => 'HK', 'default_network' => 'public', 'enabled' => 1, 'created_at' => 10, 'updated_at' => 10], ]); Capsule::table('cloud_host_resource_pool_memberships')->insert([ ['id' => 1, 'host_id' => 1, 'resource_pool_id' => 10, 'scheduler_state' => 'disabled', 'priority' => 0, 'weight' => 100, 'created_at' => 1, 'updated_at' => 300], ['id' => 2, 'host_id' => 1, 'resource_pool_id' => 11, 'scheduler_state' => 'active', 'priority' => 0, 'weight' => 100, 'created_at' => 1, 'updated_at' => 100], ['id' => 3, 'host_id' => 1, 'resource_pool_id' => 12, 'scheduler_state' => 'draining', 'priority' => 0, 'weight' => 100, 'created_at' => 1, 'updated_at' => 400], ]); $request = Request::createFromEnvironment(Environment::mock([ 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/api/v1/admin/hosts', ]))->withAttribute('cloud_actor', [ 'user_id' => 7, 'is_admin' => true, 'is_super_admin' => false, 'can_manage' => false, ]); Capsule::connection('default')->enableQueryLog(); $response = (new CloudAdminController())->hosts($request, new Response()); $queries = count(Capsule::connection('default')->getQueryLog()); $payload = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR); $assert($response->getStatusCode() === 200 && $response->getHeaderLine('Cache-Control') === 'no-store' && isset($payload['hosts']) && count($payload['hosts']) === 2, 'Administrator host list did not return the expected JSON collection.'); [$first, $second] = $payload['hosts']; $assert($first['id'] === 1 && $first['name'] === 'Host A' && $first['enabled'] === true && $first['resource_pool_id'] === 11 && $first['scheduler_state'] === 'active', 'Host list did not project the selected active membership authority.'); $assert($second['id'] === 2 && $second['name'] === 'Host B' && $second['enabled'] === false && $second['resource_pool_id'] === null && $second['scheduler_state'] === 'disabled', 'Host without a membership did not receive an explicit disabled projection.'); $assert($queries === 2, 'Host list introduced an N+1 query pattern: ' . $queries . ' queries.'); $oneRequest = Request::createFromEnvironment(Environment::mock([ 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/api/v1/admin/hosts/1', ]))->withAttribute('cloud_actor', [ 'user_id' => 7, 'is_admin' => true, 'is_super_admin' => false, 'can_manage' => false, ]); $oneResponse = (new CloudAdminController())->hosts( $oneRequest, new Response(), ['id' => '1'] ); $one = json_decode((string) $oneResponse->getBody(), true, 512, JSON_THROW_ON_ERROR); $assert($oneResponse->getStatusCode() === 200 && $oneResponse->getHeaderLine('Cache-Control') === 'no-store' && $one['id'] === 1 && $one['resource_pool_id'] === 11 && $one['scheduler_state'] === 'active', 'Single-host read drifted from the authoritative membership projection.'); echo "Administrator host list membership projection passed.\n";