197 lines
5.3 KiB
PHP
197 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\Plan;
|
|
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;
|
|
|
|
/**
|
|
* 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';
|
|
|
|
/**
|
|
* 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 give its personal organization a plan. Unless opted
|
|
* out, the model also gets an active subscription on that organization.
|
|
*
|
|
* The flags are 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);
|
|
|
|
$users = $result instanceof SupportCollection ? $result : collect([$result]);
|
|
|
|
$users->each(function (User $user): void {
|
|
if ($this->subscribed) {
|
|
$this->subscribe($user);
|
|
}
|
|
|
|
$this->assignPlan($user, $this->planKey);
|
|
});
|
|
|
|
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');
|
|
}
|
|
|
|
/**
|
|
* 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]);
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
}
|