doctrine-fixtures/data-generation-bundle
Install the Bundle
composer require --dev doctrine-fixtures/data-generation-bundle
Ensure it’s only installed in dev dependencies (as it’s for testing/data generation).
Configure fixtures-config.yml
Place the file in your project root (or config/ if preferred). Example:
user:
namespace: App\Entity\User
defined_roles: [ROLE_USER, ROLE_ADMIN]
rows: 10
post:
namespace: App\Entity\Post
rows: 20
Generate Data via CLI Run the generator with:
php bin/console data:generate
This populates your database with random test data based on the config.
First Use Case: Quick Test Data Use this to seed a local database for:
Define Fixtures in fixtures-config.yml
namespace and rows for simple entities.
product:
namespace: App\Entity\Product
rows: 50
construct for entities requiring constructor args (e.g., relationships).
order:
namespace: App\Entity\Order
construct: true # Expects a User entity as first argument
rows: 100
addReference() for relationships.
Example: Generate Order entities linked to pre-generated User entities.Dynamic Data Generation
user:
fields:
email: "{{email}}"
first_name: "{{firstName}}"
last_name: "{{lastName}}"
DataGenerator class to add domain-specific logic (e.g., valid email domains, date ranges).Seeding Workflows
data:generate during php artisan migrate:fresh --seed.--force to regenerate data without dropping tables:
php bin/console data:generate --force
rows: 0 or use environment variables to toggle features.Integration with Tests
DatabaseTransactions or DatabaseMigrations traits:
use DoctrineFixturesBundle\Test\FixturesTrait;
use DoctrineFixturesBundle\Test\IsolatedDatabaseTestCase;
class UserTest extends IsolatedDatabaseTestCase
{
use FixturesTrait;
protected function getFixturesDir(): string
{
return __DIR__ . '/../fixtures';
}
public function testSomething()
{
$this->loadFixtures([UserFixture::class]);
// Test logic here
}
}
Constructor Dependencies
construct: true is set but no referenced entity exists, generation fails.fixtures-config.yml.Unique Constraints
unique: true in config or override the generator to append UUIDs:
user:
fields:
email: "{{email}}@{{domain}}.com-{{uuid}}"
Database Truncation
data:generate truncates tables. Use --force carefully in production-like environments.--dry-run flag to preview SQL (contribute upstream if needed).Performance
rows: 1000 for initial tests, then scale up.Verbose Output Enable debug mode to see generated data:
php bin/console data:generate -vvv
Custom Generators Override the default generator for an entity:
custom_user:
namespace: App\Entity\User
generator: App\DataFixtures\CustomUserGenerator
Implement DataGeneratorInterface in your class.
Environment-Specific Configs
Use Symfony’s %env% or parameters.yml to switch fixtures between dev/test:
# config/packages/dev/fixtures-config.yml
user:
rows: 1000 # More data for dev
Add Custom Data Providers
Extend the bundle’s DataGenerator to support new field types (e.g., slug from title):
// src/DataFixtures/CustomGenerator.php
use DoctrineFixturesBundle\DataGenerator\DataGeneratorInterface;
class CustomGenerator implements DataGeneratorInterface
{
public function generate(array $data, EntityManagerInterface $em): void
{
$data['slug'] = Str::slug($data['title']);
// ...
}
}
Post-Generation Hooks Use Symfony’s event system to run logic after generation:
// config/services.yaml
App\EventListener\PostDataGenerationListener:
tags:
- { name: kernel.event_listener, event: data.generation.post, method: onPostGenerate }
Localization The bundle supports Faker’s locales. Specify in config:
user:
locale: fr_FR # French names/addresses
How can I help you explore Laravel packages today?