Files
crewli/api/database/factories/UserProfileFactory.php
bert.hausmans cd7a804024 test(forms): model tests, multi-tenancy, migration rollback (Phase 9)
UserProfileTest: belongs-to user, fillable/non-fillable boundaries,
settings cast, lastSubmittedAt accessor (null + max from user-subject
submissions only, ignoring drafts and is_test rows).

FormSchemaTest: ULID PK, OrganisationScope filtering, polymorphic owner
resolution to Event, purpose enum cast, hasMany fields/submissions, and
logSchemaChange() actually creates an activity-log entry.

FormFieldTest: belongs-to schema, field_type stored as string (not DB
enum), binding/translations array casts, hasMany values, soft-delete
preserves historical values, logFieldChange() creates an entry.

FormSubmissionTest: belongs-to schema, polymorphic subject resolution,
status enum cast, schema_snapshot array cast, hasMany values.

FormValueTest: belongs-to submission/field, value array cast, hasMany
options pivot rebuilt by observer, unique-pair DB constraint enforced.

MultiTenancyTest: OrganisationScope correctly filters FormSchema /
FormTemplate / FormFieldLibrary by route-resolved organisation. Pins
the FormSchemaWebhook un-scoped behaviour explicitly so a future scope
addition is an intentional decision, not an accident.

MigrationRollbackTest (group 'slow'): full migrate:fresh → rollback 14
S1 steps → assert all 13 form-builder tables dropped + legacy tables
intentionally retained → re-migrate and assert table list matches
snapshot. Plus a separate test exercising the populate-user-profiles
migration's down().

Supporting tweaks:
- UserProfile::lastSubmittedAt accessor now returns Carbon|null instead
  of a raw timestamp string — testable, and matches Eloquent convention.
- UserProfileFactory cooperates with UserObserver via newModel override
  (updates the auto-created row instead of inserting a duplicate).
- AppServiceProvider morph map extended with all 12 form-builder model
  keys so logSchemaChange/logFieldChange resolve under enforceMorphMap.

Suite: 945 passed (was 911), 2671 assertions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 16:44:47 +02:00

56 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\User;
use App\Models\UserProfile;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<UserProfile>
*
* Note: UserObserver auto-creates a user_profiles row whenever a User is
* created. To avoid unique-constraint collisions, this factory cooperates
* with the observer: it updates the existing profile row (if any) rather
* than blindly inserting a new one.
*/
final class UserProfileFactory extends Factory
{
/** @return array<string, mixed> */
public function definition(): array
{
return [
'user_id' => User::factory(),
'bio' => fake('nl_NL')->sentence(10),
'photo_url' => null,
'emergency_contact_name' => fake('nl_NL')->name(),
'emergency_contact_phone' => fake('nl_NL')->phoneNumber(),
'reliability_score' => fake()->randomFloat(2, 3.00, 5.00),
'is_ambassador' => false,
'settings' => null,
];
}
public function ambassador(): static
{
return $this->state(fn () => ['is_ambassador' => true]);
}
public function newModel(array $attributes = []): UserProfile
{
$userId = $attributes['user_id'] ?? null;
if ($userId !== null) {
$existing = UserProfile::where('user_id', $userId)->first();
if ($existing !== null) {
$existing->forceFill($attributes)->save();
return $existing;
}
}
return parent::newModel($attributes);
}
}