butschster/entity-faker
Generate fake PHP entities and persist them via your ORM using a simple factory. Define per-class attribute generators with Faker, support inheritance via raw attributes, and create single or multiple entities using Laminas hydrators/entity factories.
Installation:
composer require butschster/entity-faker
Add the service provider to config/app.php (unchanged):
'providers' => [
// ...
Butschster\EntityFaker\EntityFakerServiceProvider::class,
],
Basic Usage (Updated for v2.0.0):
use Butschster\EntityFaker\EntityFaker;
$faker = app(EntityFaker::class);
$fakeUser = $faker->fake(User::class); // Generates a fake User instance via redesigned EntityFactory
First Use Case (Updated for v2.0.0):
Generate test data for a User model with a Post relationship:
$fakeUser = $faker->fake(User::class, [
'posts' => [Post::class, Post::class] // Array of related entities (now handled by redesigned factory)
]);
Faking Entities (Redesigned Factory):
// Single entity (now uses EntityFactory v2)
$fakePost = $faker->fake(Post::class);
// Multiple entities (batch processing optimized)
$fakePosts = $faker->fakeMany(Post::class, 5);
Customizing Attributes (Enhanced Rules):
// Override defaults (now supports more flexible rule parsing)
$fakeUser = $faker->fake(User::class, [
'name' => 'John Doe',
'email' => '{{email}}' // Leverages Faker's rule engine
]);
// Dynamic attribute generation
$fakeUser = $faker->fake(User::class, [
'age' => '{{numberBetween(20, 60)}}'
]);
Handling Relationships (Improved Resolution):
// One-to-many (now uses redesigned relationship resolver)
$fakeUser = $faker->fake(User::class, [
'posts' => [Post::class, Post::class]
]);
// Many-to-many (optimized for v2.0.0)
$fakePost = $faker->fake(Post::class, [
'tags' => [Tag::class, Tag::class, Tag::class]
]);
Seeding with Factories (Updated for v2.0.0):
// In a seeder (now leverages EntityFactory v2)
public function run()
{
$faker = app(EntityFaker::class);
$faker->fakeMany(User::class, 10)->each(function ($user) {
$user->save();
});
}
Integration with Tests (Unchanged):
/** @test */
public function test_user_creation()
{
$fakeUser = $this->app->make(EntityFaker::class)->fake(User::class);
$this->assertInstanceOf(User::class, $fakeUser);
}
Circular References (Still Applies):
User has a profile relationship and Profile has a user relationship, the faker may loop infinitely.ignoreRelationships():
$fakeUser = $faker->fake(User::class)->ignoreRelationships(['profile']);
Non-Serializable Attributes (Still Applies):
Carbon instances or closures may cause issues.->withoutAttributes(['created_at', 'updated_at']) or cast them in your model.Custom Accessors/Mutators (Still Applies):
getAttribute() or setAttribute(), the faker may not handle it correctly.->withoutAccessors() or mock the logic temporarily.Database Constraints (Still Applies):
unique constraints may fail.->ignoreConstraints() or handle duplicates manually.Inspect Generated Data (Enhanced in v2.0.0):
$fakeData = $faker->getFakeData(User::class);
dd($fakeData); // See raw data before instantiation (now includes factory metadata)
Enable Verbose Mode (Optimized):
$faker->setVerbose(true); // Logs what’s being faked (now includes factory resolution steps)
Reset Faker State (Updated for v2.0.0):
$faker->reset(); // Clears cached data for retries (now resets redesigned factory state)
Custom Faker Rules (Enhanced in v2.0.0):
$faker->extend('custom_rule', function () {
return 'custom_value';
});
// Usage:
$fakeUser = $faker->fake(User::class, ['bio' => '{{custom_rule}}']);
Override Default Rules (Now Supports Factory-Specific Overrides):
$faker->override(User::class, [
'email' => '{{email}}@example.com' // Force domain (now applies to EntityFactory v2)
]);
Event Listeners (Updated for v2.0.0):
entity-faker.faking or entity-faker.faked events for custom logic:
Event::listen('entity-faker.faking', function ($entity, $attributes) {
if ($entity instanceof User) {
$attributes['is_active'] = true;
}
});
Integration with Laravel Factories (Hybrid Approach):
$fakeUser = $faker->fake(User::class);
User::factory()->create(['email' => $fakeUser->email]);
Batch Faking (Optimized in v2.0.0):
$fakeUsers = $faker->fakeMany(User::class, 1000); // More efficient with redesigned factory
Disable Relationships for Bulk Operations (Still Applies):
$fakeUsers = $faker->fakeMany(User::class, 1000)
->ignoreRelationships(['posts', 'profile'])
->each(fn($u) => $u->save());
Cache Faker Instances (Still Applies):
$faker = app(EntityFaker::class); // Reuse instance in tests/seeds (now leverages v2.0.0 optimizations)
EntityFactory Redesign:
EntityFactory class has been completely redesigned. Existing custom implementations may need updates.Rule Parsing:
Method Signatures:
How can I help you explore Laravel packages today?