TenantAtlas/apps/platform/database/factories/TenantConfigurationResourceFactory.php
ahmido ca0f54614d feat: add generic content-backed coverage capture (#482)
Automated PR provided by Codex via Gitea API.

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #482
2026-06-25 19:55:52 +00:00

85 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\ManagedEnvironment;
use App\Models\ProviderConnection;
use App\Models\TenantConfigurationResource;
use App\Models\TenantConfigurationResourceType;
use App\Models\Workspace;
use App\Support\TenantConfiguration\ClaimState;
use App\Support\TenantConfiguration\EvidenceState;
use App\Support\TenantConfiguration\IdentityState;
use App\Support\TenantConfiguration\SourceClass;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<TenantConfigurationResource>
*/
class TenantConfigurationResourceFactory extends Factory
{
protected $model = TenantConfigurationResource::class;
public function definition(): array
{
return [
'managed_environment_id' => ManagedEnvironment::factory()->for(Workspace::factory()),
'workspace_id' => function (array $attributes): int {
return $this->workspaceIdForTenant((int) ($attributes['managed_environment_id'] ?? 0));
},
'provider_connection_id' => function (array $attributes): int {
$tenantId = (int) ($attributes['managed_environment_id'] ?? 0);
$workspaceId = $this->workspaceIdForTenant($tenantId);
return (int) ProviderConnection::factory()->create([
'workspace_id' => $workspaceId,
'managed_environment_id' => $tenantId,
])->getKey();
},
'resource_type_id' => TenantConfigurationResourceType::factory(),
'latest_evidence_id' => null,
'source_class' => SourceClass::Tcm->value,
'canonical_type' => function (array $attributes): string {
$resourceType = TenantConfigurationResourceType::query()->find((int) ($attributes['resource_type_id'] ?? 0));
return $resourceType instanceof TenantConfigurationResourceType
? (string) $resourceType->canonical_type
: fake()->slug();
},
'canonical_resource_key' => fn (array $attributes): string => sprintf(
'%s:%s',
(string) ($attributes['canonical_type'] ?? 'resource'),
fake()->uuid(),
),
'source_resource_id' => fake()->uuid(),
'source_display_name' => fake()->words(3, true),
'source_metadata' => ['factory' => 'tenant_configuration_resource'],
'latest_evidence_state' => EvidenceState::NotCaptured->value,
'latest_identity_state' => IdentityState::Stable->value,
'latest_claim_state' => ClaimState::InternalOnly->value,
'latest_payload_hash' => null,
'latest_captured_at' => null,
];
}
private function workspaceIdForTenant(int $tenantId): int
{
$tenant = ManagedEnvironment::query()->find($tenantId);
if (! $tenant instanceof ManagedEnvironment) {
return (int) Workspace::factory()->create()->getKey();
}
if ($tenant->workspace_id === null) {
$workspaceId = (int) Workspace::factory()->create()->getKey();
$tenant->forceFill(['workspace_id' => $workspaceId])->save();
return $workspaceId;
}
return (int) $tenant->workspace_id;
}
}