forked from eatvio/eatvio-infra
added tests for blogPost
This commit is contained in:
@@ -3,16 +3,62 @@
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\BlogPost;
|
||||
use App\Models\User;
|
||||
use App\Models\BlogPostComment;
|
||||
use App\Models\Tag;
|
||||
|
||||
class BlogPostSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
public function run(): void
|
||||
{
|
||||
//
|
||||
$blogPosts = BlogPost::factory()->count(10)->create();
|
||||
|
||||
if (User::count() > 0 && BlogPost::count() > 0) {
|
||||
|
||||
$blogPosts->each(
|
||||
function ($blogPost) {
|
||||
User::inRandomOrder()
|
||||
->first()
|
||||
->author_of_blog_post()
|
||||
->attach($blogPost->id);
|
||||
}
|
||||
);
|
||||
|
||||
$comments = BlogPostComment::factory()->count(5)->make();
|
||||
|
||||
$comments->each(
|
||||
static function ($comment): void {
|
||||
$comment->author()->associate(
|
||||
User::inRandomOrder()
|
||||
->first()
|
||||
->id
|
||||
);
|
||||
|
||||
$comment->blog_post()->associate(
|
||||
BlogPost::inRandomOrder()
|
||||
->first()
|
||||
->id
|
||||
);
|
||||
|
||||
$comment->saveOrFail();
|
||||
}
|
||||
);
|
||||
|
||||
$blogPosts = BlogPost::all();
|
||||
|
||||
$blogPosts->each(
|
||||
static function ($blogPost): void {
|
||||
|
||||
$tags = Tag::factory()->count(5)->create();
|
||||
|
||||
$tagsIds = collect($tags)->map(fn($curTag) => $curTag['id']);
|
||||
$blogPost->tags()->attach($tagsIds);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user