<?php
namespace Database\Factories;
use App\Modules\User\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
protected $model = User::class;
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'phone' => fake()->unique()->numerify('98########'),
'password' => Hash::make('password'),
// Verified by default — most tests create an already-onboarded
// customer/staff account. Under Model::shouldBeStrict(), leaving
// this (and vendor_id below) entirely unset meant the in-memory
// factory instance (used directly by actingAs()/Livewire::actingAs(),
// which never re-fetches from the DB) threw MissingAttributeException
// the moment anything touched hasVerifiedEmail() or the vendor
// relation — see ->unverified() for tests that need the opposite.
'email_verified_at' => now(),
'vendor_id' => null,
'locale' => 'en',
'currency' => 'NPR',
'timezone' => 'Asia/Kathmandu',
'is_active' => true,
'remember_token' => Str::random(10),
];
}
public function unverified(): static
{
return $this->state(fn () => ['email_verified_at' => null]);
}
public function admin(): static
{
return $this->afterCreating(fn (User $u) => $u->assignRole('admin'));
}
public function customer(): static
{
return $this->afterCreating(fn (User $u) => $u->assignRole('customer'));
}
public function vendor(): static
{
return $this->afterCreating(fn (User $u) => $u->assignRole('vendor'));
}
}