Files
mytodos/database/factories/UserFactory.php
T

197 lines
5.3 KiB
PHP
Raw Normal View History

2026-09-17 23:01:26 -07:00
<?php
namespace Database\Factories;
use App\Models\Plan;
2026-09-17 23:01:26 -07:00
use App\Models\User;
2026-10-04 20:34:19 -07:00
use Illuminate\Database\Eloquent\Collection;
2026-09-17 23:01:26 -07:00
use Illuminate\Database\Eloquent\Factories\Factory;
2026-10-04 20:34:19 -07:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as SupportCollection;
2026-09-17 23:01:26 -07:00
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
2026-10-04 20:34:19 -07:00
use Laravel\Cashier\Subscription;
2026-09-17 23:01:26 -07:00
/**
* @extends Factory<User>
*/
class UserFactory extends Factory
{
/**
* The current password being used by the factory.
*/
protected static ?string $password;
2026-10-04 20:34:19 -07:00
/**
* 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;
/**
* The plan given to the user's personal organization. Following the plan
* of record, a factory user starts on the unlimited plan so tests are not
* constrained by folder or note limits. Call withPlan() or withFreePlan()
* to opt into a specific tier.
*/
protected ?string $planKey = 'unlimited';
2026-09-17 23:01:26 -07:00
/**
* 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,
];
}
2026-10-04 20:34:19 -07:00
/**
* Create the model and give its personal organization a plan. Unless opted
* out, the model also gets an active subscription on that organization.
2026-10-04 20:34:19 -07:00
*
* The flags are read here rather than in an afterCreating callback because
2026-10-04 20:34:19 -07:00
* 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);
$users = $result instanceof SupportCollection ? $result : collect([$result]);
$users->each(function (User $user): void {
if ($this->subscribed) {
$this->subscribe($user);
}
$this->assignPlan($user, $this->planKey);
});
2026-10-04 20:34:19 -07:00
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;
}
/**
* Give the user's personal organization the plan with the given key.
*/
public function withPlan(string $key): static
{
$this->planKey = $key;
return $this;
}
/**
* Give the user's personal organization the free plan.
*/
public function withFreePlan(): static
{
return $this->withPlan('free');
}
2026-09-17 23:01:26 -07:00
/**
* 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(),
]);
}
2026-10-04 20:34:19 -07:00
/**
* 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]);
}
/**
* Assign a plan to the user's current organization.
*/
protected function assignPlan(User $user, ?string $planKey): void
{
if ($planKey === null) {
return;
}
$organization = $user->currentOrganization()->first();
if ($organization === null) {
return;
}
$planId = Plan::query()->where('key', $planKey)->value('id');
if ($planId === null) {
return;
}
$organization->forceFill(['plan_id' => $planId])->save();
}
2026-09-17 23:01:26 -07:00
}