This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Plan;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
@@ -29,6 +30,14 @@ class UserFactory extends Factory
|
||||
*/
|
||||
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.
|
||||
*
|
||||
@@ -49,10 +58,10 @@ class UserFactory extends Factory
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the model and, unless opted out, give it an active subscription on
|
||||
* the personal organization the model's created hook just made.
|
||||
* Create the model and give its personal organization a plan. Unless opted
|
||||
* out, the model also gets an active subscription on that organization.
|
||||
*
|
||||
* The flag is read here rather than in an afterCreating callback because
|
||||
* 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.
|
||||
*
|
||||
@@ -63,13 +72,15 @@ class UserFactory extends Factory
|
||||
{
|
||||
$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));
|
||||
$users->each(function (User $user): void {
|
||||
if ($this->subscribed) {
|
||||
$this->subscribe($user);
|
||||
}
|
||||
|
||||
$this->assignPlan($user, $this->planKey);
|
||||
});
|
||||
|
||||
return $result;
|
||||
}
|
||||
@@ -85,6 +96,24 @@ class UserFactory extends Factory
|
||||
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.
|
||||
*/
|
||||
@@ -140,4 +169,28 @@ class UserFactory extends Factory
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user