From 370fcd42e5cd4c25640da9314c10c58875ed672a Mon Sep 17 00:00:00 2001 From: Elliot Derhay Date: Mon, 8 Jul 2024 06:44:37 -0400 Subject: [PATCH] Ok, setting state on factory is not working... --- database/factories/PostFactory.php | 10 ++++++++-- tests/Pest.php | 5 +---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/database/factories/PostFactory.php b/database/factories/PostFactory.php index 5d4f8fa1..5b1d2f0a 100644 --- a/database/factories/PostFactory.php +++ b/database/factories/PostFactory.php @@ -23,8 +23,14 @@ public function definition(): array 'title' => fake()->realText(60), 'body' => fake()->realText(500), 'slug' => str($title)->slug()->toString(), - 'published' => $published, - 'published_at' => $published ? fake()->dateTime() : null, ]; } + + public function published(): static + { + return $this->state(static fn (array $attributes): array => [ + 'published' => true, + 'published_at' => fake()->dateTime(), + ]); + } } diff --git a/tests/Pest.php b/tests/Pest.php index 84885b05..e4e3d01e 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -71,10 +71,7 @@ function createPosts(int $count = 1, bool|null $publish = null): Collection|null { $factory = Post::factory()->count($count); - if ($publish !== null) $factory->afterMaking(function (Post $post) use ($publish) { - $post->published = $publish; - $post->published_at = $publish ? fake()->dateTime() : null; - }); + if ($publish) return $factory->published()->create(); return $factory->create(); }