TenantAtlas/apps/platform/database/factories/TenantOnboardingSessionFactory.php
ahmido e64bae9cfc feat: cut over tenant core to managed environments (#335)
## Summary
- replace the legacy Tenant and TenantMembership core models with ManagedEnvironment and ManagedEnvironmentMembership
- propagate the managed environment naming and key changes across Filament resources, pages, controllers, jobs, models, and supporting runtime paths
- add feature 279 spec artifacts and focused managed-environment test coverage for model behavior, route binding, panel context, authorization, and legacy guardrails

## Validation
- `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Feature/ManagedEnvironment/LegacyTenantCoreGuardTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentAuthorizationTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentPanelContextTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentRouteBindingTest.php tests/Unit/ManagedEnvironment/ManagedEnvironmentContextResolverTest.php tests/Unit/ManagedEnvironment/ManagedEnvironmentModelTest.php`
- `cd apps/platform && ./vendor/bin/sail bin pint --dirty --format agent`

## Notes
- branch pushed from commit `1123b122`
- browser smoke test file was added but not run in this pass

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #335
2026-05-07 06:38:14 +00:00

159 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\ManagedEnvironment;
use App\Models\TenantOnboardingSession;
use App\Models\User;
use App\Models\Workspace;
use App\Support\Onboarding\OnboardingCheckpoint;
use App\Support\Onboarding\OnboardingLifecycleState;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<TenantOnboardingSession>
*/
class TenantOnboardingSessionFactory extends Factory
{
protected $model = TenantOnboardingSession::class;
public function definition(): array
{
$entraTenantId = fake()->uuid();
$tenantName = fake()->company();
return [
'workspace_id' => Workspace::factory(),
'managed_environment_id' => null,
'entra_tenant_id' => $entraTenantId,
'current_step' => 'identify',
'state' => [
'entra_tenant_id' => $entraTenantId,
'tenant_name' => $tenantName,
'environment' => 'prod',
],
'started_by_user_id' => User::factory(),
'updated_by_user_id' => User::factory(),
'version' => 1,
'lifecycle_state' => OnboardingLifecycleState::Draft->value,
'current_checkpoint' => OnboardingCheckpoint::Identify->value,
'last_completed_checkpoint' => null,
'reason_code' => null,
'blocking_reason_code' => null,
'completed_at' => null,
'cancelled_at' => null,
];
}
public function forWorkspace(Workspace $workspace): static
{
return $this->state(fn (): array => [
'workspace_id' => (int) $workspace->getKey(),
]);
}
public function forTenant(ManagedEnvironment $tenant): static
{
return $this->state(fn (array $attributes): array => [
'workspace_id' => (int) $tenant->workspace_id,
'managed_environment_id' => (int) $tenant->getKey(),
'entra_tenant_id' => (string) $tenant->managed_environment_id,
'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
'managed_environment_id' => (int) $tenant->getKey(),
'entra_tenant_id' => (string) $tenant->managed_environment_id,
'tenant_name' => (string) $tenant->name,
'environment' => (string) ($tenant->environment ?? 'prod'),
]),
]);
}
public function startedBy(User $user): static
{
return $this->state(fn (): array => [
'started_by_user_id' => (int) $user->getKey(),
]);
}
public function updatedBy(User $user): static
{
return $this->state(fn (): array => [
'updated_by_user_id' => (int) $user->getKey(),
]);
}
public function withProviderConnection(int $providerConnectionId): static
{
return $this->state(fn (array $attributes): array => [
'current_step' => 'connection',
'current_checkpoint' => OnboardingCheckpoint::ConnectProvider->value,
'last_completed_checkpoint' => OnboardingCheckpoint::Identify->value,
'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
'provider_connection_id' => $providerConnectionId,
]),
]);
}
public function withVerificationRun(int $operationRunId): static
{
return $this->state(fn (array $attributes): array => [
'current_step' => 'verify',
'current_checkpoint' => OnboardingCheckpoint::VerifyAccess->value,
'last_completed_checkpoint' => OnboardingCheckpoint::ConnectProvider->value,
'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
'verification_operation_run_id' => $operationRunId,
]),
]);
}
public function reviewReady(): static
{
return $this->state(fn (array $attributes): array => [
'current_step' => 'bootstrap',
'lifecycle_state' => OnboardingLifecycleState::ReadyForActivation->value,
'current_checkpoint' => OnboardingCheckpoint::CompleteActivate->value,
'last_completed_checkpoint' => OnboardingCheckpoint::VerifyAccess->value,
'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
'bootstrap_operation_types' => [],
]),
]);
}
public function completed(): static
{
return $this->state(fn (): array => [
'current_step' => 'complete',
'lifecycle_state' => OnboardingLifecycleState::Completed->value,
'current_checkpoint' => OnboardingCheckpoint::CompleteActivate->value,
'last_completed_checkpoint' => OnboardingCheckpoint::CompleteActivate->value,
'reason_code' => null,
'blocking_reason_code' => null,
'completed_at' => now(),
'cancelled_at' => null,
]);
}
public function cancelled(): static
{
return $this->state(fn (): array => [
'current_step' => 'cancelled',
'lifecycle_state' => OnboardingLifecycleState::Cancelled->value,
'current_checkpoint' => OnboardingCheckpoint::Identify->value,
'last_completed_checkpoint' => null,
'reason_code' => null,
'blocking_reason_code' => null,
'completed_at' => null,
'cancelled_at' => now(),
]);
}
public function resumableForTenant(ManagedEnvironment $tenant): static
{
return $this->forTenant($tenant)->state(fn (): array => [
'completed_at' => null,
'cancelled_at' => null,
]);
}
}