authentikate/database/factories/InvitationFactory.php
Javier Feliz de8277a303
Some checks failed
linter / quality (push) Successful in 6m48s
tests / ci (push) Has been cancelled
Add some more factories
2025-08-02 20:04:57 -04:00

167 lines
4.1 KiB
PHP

<?php
namespace Database\Factories;
use App\Models\Invitation;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Invitation>
*/
class InvitationFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'code' => fake()->unique()->regexify('[A-Za-z0-9]{50}'),
'email' => fake()->unique()->safeEmail(),
'invited_by' => User::factory(),
'expires_at' => now()->addDays(7),
'email_sent' => fake()->boolean(30),
'accepted_at' => null,
'user_id' => null,
];
}
/**
* Indicate that the invitation is expired.
*/
public function expired(): static
{
return $this->state(fn(array $attributes) => [
'expires_at' => now()->subDay(),
]);
}
/**
* Indicate that the invitation expires soon.
*/
public function expiringSoon(): static
{
return $this->state(fn(array $attributes) => [
'expires_at' => now()->addHours(fake()->numberBetween(1, 24)),
]);
}
/**
* Indicate that the invitation has been accepted.
*/
public function accepted(): static
{
return $this->state(fn(array $attributes) => [
'accepted_at' => now()->subDays(fake()->numberBetween(0, 5)),
'user_id' => User::factory(),
]);
}
/**
* Indicate that the invitation was accepted by a specific user.
*/
public function acceptedBy(User $user): static
{
return $this->state(fn(array $attributes) => [
'accepted_at' => now()->subDays(fake()->numberBetween(0, 5)),
'user_id' => $user->id,
]);
}
/**
* Indicate that the email was sent.
*/
public function emailSent(): static
{
return $this->state(fn(array $attributes) => [
'email_sent' => true,
]);
}
/**
* Indicate that the email was not sent.
*/
public function emailNotSent(): static
{
return $this->state(fn(array $attributes) => [
'email_sent' => false,
]);
}
/**
* Create an invitation from a specific user.
*/
public function from(User $inviter): static
{
return $this->state(fn(array $attributes) => [
'invited_by' => $inviter->id,
]);
}
/**
* Create an invitation for a specific email.
*/
public function forEmail(string $email): static
{
return $this->state(fn(array $attributes) => [
'email' => $email,
]);
}
/**
* Create an invitation with a specific code for testing.
*/
public function withCode(string $code): static
{
return $this->state(fn(array $attributes) => [
'code' => $code,
]);
}
/**
* Create a pending invitation (default state but explicit).
*/
public function pending(): static
{
return $this->state(fn(array $attributes) => [
'accepted_at' => null,
'user_id' => null,
]);
}
/**
* Create an invitation that expires in a specific timeframe.
*/
public function expiresIn(string $timeframe): static
{
return $this->state(fn(array $attributes) => [
'expires_at' => now()->add($timeframe),
]);
}
/**
* Create a recent invitation.
*/
public function recent(): static
{
return $this->state(fn(array $attributes) => [
'created_at' => now()->subMinutes(fake()->numberBetween(1, 60)),
'updated_at' => now()->subMinutes(fake()->numberBetween(1, 60)),
]);
}
/**
* Create an old invitation.
*/
public function old(): static
{
return $this->state(fn(array $attributes) => [
'created_at' => now()->subDays(fake()->numberBetween(7, 30)),
'updated_at' => now()->subDays(fake()->numberBetween(7, 30)),
]);
}
}