Skip to content

Commit

Permalink
Ok, setting state on factory is not working...
Browse files Browse the repository at this point in the history
  • Loading branch information
JSn1nj4 committed Jul 8, 2024
1 parent 325ac97 commit 370fcd4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 8 additions & 2 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
}
}
5 changes: 1 addition & 4 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 370fcd4

Please sign in to comment.