Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat improve model factory after* methods #1195

Merged
merged 3 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions stubs/Factory.stub
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,20 @@ class Factory
* @return array<model-property<TModel>, mixed>
*/
abstract public function definition();

/**
* Add a new "after making" callback to the model definition.
*
* @param \Closure(TModel): mixed $callback
* @return static
*/
public function afterMaking(\Closure $callback) {}

/**
* Add a new "after creating" callback to the model definition.
*
* @param \Closure(TModel): mixed $callback
* @return static
*/
public function afterCreating(\Closure $callback) {}
}
20 changes: 20 additions & 0 deletions tests/Features/ReturnTypes/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public function testStateWithParentModelUsingFactory(): UserFactory
return User::factory()->unverified();
}

public function testAfterMakingWithParentModelUsingFactory(): UserFactory
{
return User::factory()->afterMaking(fn (User $user) => $user);
}

public function testAfterCreatingWithParentModelUsingFactory(): UserFactory
{
return User::factory()->afterCreating(fn (User $user) => $user);
}

public function testFactory(): PostFactory
{
return Post::factory();
Expand Down Expand Up @@ -92,4 +102,14 @@ public function testMake()
{
return Post::factory()->make();
}

public function testAfterMaking(): PostFactory
{
return Post::factory()->afterMaking(fn (Post $post) => $post);
}

public function testAfterCreating(): PostFactory
{
return Post::factory()->afterCreating(fn (Post $post) => $post);
}
}