Installation
Add the bundle to composer.json (despite being archived, it may still work for legacy projects):
composer require davidbadura/fixtures-bundle
Register the bundle in config/bundles.php:
return [
// ...
DavidBadura\FixturesBundle\DavidBaduraFixturesBundle::class => ['all' => true],
];
Basic Fixture File
Create a YAML fixture (e.g., config/fixtures/users.yml):
App\Entity\User:
- { name: 'John Doe', email: 'john@example.com' }
Load Fixtures
Use the fixture:load command:
php bin/console fixture:load
Verify Check the database for the inserted records.
Populate a test database with seed data.
$this->get('fixture.manager')->load('path/to/fixtures');
Development Workflow
config/fixtures/ (e.g., users.yml, posts.yml).tags: [users]), then load selectively:
php bin/console fixture:load --tag=users
Dependency Resolution
Fixtures can reference each other (e.g., a Post fixture referencing a User):
App\Entity\Post:
- { title: 'Hello', author: '@user1' } # Resolves to a User fixture
Custom Converters
Extend DavidBadura\FixturesBundle\Converter\ConverterInterface to handle custom logic (e.g., for setCreatedAt methods):
class CustomDateConverter implements ConverterInterface {
public function convert($value, $method, $object) {
return new \DateTime($value);
}
}
Register it in config/packages/david_badura_fixtures.yaml:
david_badura_fixtures:
converters:
- App\Converter\CustomDateConverter
Programmatic Loading Load fixtures in tests or migrations:
$fixtureManager = $this->get('fixture.manager');
$fixtureManager->load('path/to/fixtures');
Faker Integration
Use DavidBaduraFakerBundle for dynamic data:
App\Entity\User:
- { name: '<name>', email: '<email>' }
MongoDB Support
Configure the bundle for MongoDB entities in config/packages/david_badura_fixtures.yaml:
david_badura_fixtures:
orm: false # Disable Doctrine ORM
odm: true # Enable Doctrine ODM
Validation Enable validation in config to ensure fixtures meet entity constraints:
david_badura_fixtures:
validate: true
Archived Status The bundle is no longer maintained. Use Nelmio/Alice for new projects.
Doctrine DBAL Dependency
If using MongoDB, ensure david_badura_fixtures.orm is set to false to avoid conflicts.
Bidirectional References
Circular references (e.g., User ↔ Post) may fail silently. Debug with:
php bin/console debug:fixtures
FakerBundle Dependency
DavidBaduraFakerBundle is required for Faker placeholders (<name>, <email>). Install it separately:
composer require davidbadura/faker-bundle
Dry Run Test fixtures without loading:
php bin/console fixture:load --dry-run
Verbose Output Enable debug mode for detailed logs:
php bin/console fixture:load -vvv
Event Listeners
Override fixture loading via events (e.g., OnFixturesLoaded). Register in config/services.yaml:
services:
App\EventListener\FixturesListener:
tags:
- { name: kernel.event_listener, event: david_badura.fixtures.on_load, method: onFixturesLoad }
Custom Fixture Formats
Extend the bundle to support XML or CSV by implementing DavidBadura\FixturesBundle\Loader\LoaderInterface.
Pre/Post-Load Hooks Use events to run logic before/after loading:
// src/EventListener/FixturesListener.php
public function onFixturesLoad(OnFixturesLoadEvent $event) {
$fixtures = $event->getFixtures();
// Modify fixtures dynamically
}
Override Default Converter Replace the default converter in config:
david_badura_fixtures:
default_converter: App\Converter\CustomConverter
Case-Sensitive Tags
Fixture tags are case-sensitive. Use consistent casing (e.g., users, not Users).
MongoDB Collection Names
Ensure odm: true is set and collection names match your fixtures.
Security Encoder
For password hashing, configure the encoder in david_badura_fixtures.security.encoder. Example:
david_badura_fixtures:
security:
encoder: App\Security\UserPasswordEncoder
How can I help you explore Laravel packages today?