Feat: Add factories and basic seeder

This commit is contained in:
Jonathan Nielson 2024-08-03 23:49:42 -05:00
parent d7886ae9b5
commit d91b49275f
6 changed files with 117 additions and 10 deletions

View File

@ -11,10 +11,10 @@ class Article extends Model
use HasFactory;
/** The name of the database table storing this Model's records. */
protected string $table = 'articles';
protected $table = 'articles';
/** The attributes on the Model that can be mass-assigned */
protected array $fillable = [
protected $fillable = [
'title',
'subtitle',
'author_id',

View File

@ -12,10 +12,10 @@ class Author extends Model
use HasFactory;
/** The name of the database table storing this Model's records. */
protected string $table = 'authors';
protected $table = 'authors';
/** The attributes on the Model that can be mass-assigned */
protected array $fillable = [
protected $fillable = [
'display_name',
'profile_image_url',
'user_id',

View File

@ -0,0 +1,52 @@
<?php
namespace Database\Factories;
use App\Models\Author;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Article>
*/
class ArticleFactory extends Factory
{
/**
* The current User being used by the factory.
*/
protected static Author $author;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'title' => fake()->words(3, true),
'slug' => fake()->unique()->slug(),
'author_id' => static::$author->id ??= Author::factory()->create()->id,
'subtitle' => fake()->sentence(),
'body' => fake()->paragraphs(2, true),
'published_at' => fake()->date('Y-m-d H:m:s', now()->addDays(rand(0,20))),
'header_image_url' => fake()->imageUrl(),
'path_to_markdown_source' => fake()->name(),
'processed_at' => fake()->date('Y-m-d H:m:s'),
];
}
/**
* Indicate the model's associated Author
*/
public function author(int|Author $author): static
{
if(is_int($author)) {
$author = Author::findOrFail($author);
}
static::$author = $author;
return $this->state(fn (array $attributes) => [
'author_id' => $author->id,
]);
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace Database\Factories;
use Illuminate\Foundation\Auth\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Author>
*/
class AuthorFactory extends Factory
{
/**
* The current User being used by the factory.
*/
protected static User $user;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'display_name' => fake()->userName(),
'profile_image_url' => fake()->imageUrl(),
'user_id' => static::$user->id ??= User::factory()->create()->id,
'biography' => fake()->paragraph(),
'primary_link' => fake()->url(),
'secondary_link' => rand(0,1) == 0 ? fake()->url() : null,
'mastodon_handle' => rand(0,1) == 0 ? fake()->userName() : null,
];
}
/**
* Indicate the model's associated User
*/
public function user(int|User $user): static
{
if(is_int($user)) {
$user = User::findOrFail($user);
}
static::$user = $user;
return $this->state(fn (array $attributes) => [
'user_id' => $user->id,
]);
}
}

View File

@ -41,4 +41,5 @@ class UserFactory extends Factory
'email_verified_at' => null,
]);
}
}

View File

@ -2,7 +2,9 @@
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\Models\User;
use App\Models\Author;
use App\Models\Article;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
@ -12,11 +14,13 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// \App\Models\User::factory(10)->create();
$test_user = User::factory()->create([
'name' => 'Test User',
'email' => 'test@example.com',
]);
// \App\Models\User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
$test_author = Author::factory()->user($test_user)->create();
Article::factory()->author($test_author)->count(30)->create();
}
}