Files
mytodos/database/factories/UserFactory.php
T
ecdown 2ca3860557
tests / ci (push) Canceled after 0s
added pricing, orgs
2026-10-04 20:34:19 -07:00

144 lines
4.0 KiB
PHP

<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as SupportCollection;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Laravel\Cashier\Subscription;
/**
* @extends Factory<User>
*/
class UserFactory extends Factory
{
/**
* The current password being used by the factory.
*/
protected static ?string $password;
/**
* Whether created users are given an active subscription.
*
* A factory user defaults to being entitled so that tests can focus on the
* behaviour they are covering. Call unsubscribed() to opt out.
*/
protected bool $subscribed = true;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'two_factor_secret' => null,
'two_factor_recovery_codes' => null,
'two_factor_confirmed_at' => null,
];
}
/**
* Create the model and, unless opted out, give it an active subscription on
* the personal organization the model's created hook just made.
*
* The flag is read here rather than in an afterCreating callback because
* afterCreating() hands back a new factory instance, so a closure registered
* there would close over a copy that unsubscribed() never reaches.
*
* @param array<string, mixed> $attributes
* @return Collection<int, User>|User
*/
public function create($attributes = [], ?Model $parent = null): Collection|User
{
$result = parent::create($attributes, $parent);
if (! $this->subscribed) {
return $result;
}
$users = $result instanceof SupportCollection ? $result : collect([$result]);
$users->each(fn (User $user) => $this->subscribe($user));
return $result;
}
/**
* Indicate that the model does not have an active subscription, as a newly
* registered account that has not been through checkout.
*/
public function unsubscribed(): static
{
$this->subscribed = false;
return $this;
}
/**
* Indicate that the model's email address should be unverified.
*/
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}
/**
* Indicate that the user is an administrator.
*/
public function asAdmin(): static
{
return $this->state(fn (array $attributes) => [
'is_admin' => true,
]);
}
/**
* Set the maximum length allowed for the user's todo notes.
*/
public function withNotesLimit(int $limit): static
{
return $this->state(fn (array $attributes) => [
'notes_limit' => $limit,
]);
}
/**
* Indicate that the model has two-factor authentication configured.
*/
public function withTwoFactor(): static
{
return $this->state(fn (array $attributes) => [
'two_factor_secret' => encrypt('secret'),
'two_factor_recovery_codes' => encrypt(json_encode(['recovery-code-1'])),
'two_factor_confirmed_at' => now(),
]);
}
/**
* Attach an active subscription to the user's current organization.
*/
protected function subscribe(User $user): void
{
$organization = $user->currentOrganization()->first();
if ($organization === null) {
return;
}
Subscription::factory()->create(['organization_id' => $organization->id]);
}
}