h4cc/alice-fixtures-bundle
Symfony2 bundle for flexible data fixtures using nelmio/alice and Faker. Load fixtures from YAML/PHP, decouple from Doctrine DataFixtures, and optionally drop & recreate Doctrine ORM schema. Supports Doctrine ORM and MongoDB ODM. Work in progress (<1.0).
Installation:
composer require h4cc/alice-fixtures-bundle
Add to AppKernel.php under dev/test environments:
$bundles[] = new h4cc\AliceFixturesBundle\h4ccAliceFixturesBundle();
Basic Configuration (config_dev.yml):
h4cc_alice_fixtures:
locale: en_US
seed: 1
do_flush: true
First Fixture (src/Acme/DemoBundle/DataFixtures/Alice/Users.yml):
App\Entity\User:
name: '<name>'
email: '<email>'
Load Fixtures:
php bin/console h4cc_alice_fixtures:load:files src/Acme/DemoBundle/DataFixtures/Alice/Users.yml
Use the load:files command to populate a database with test data from YAML/PHP files. Ideal for:
Example:
php bin/console h4cc_alice_fixtures:load:files src/Acme/DemoBundle/DataFixtures/Alice/Users.yml src/Acme/DemoBundle/DataFixtures/Alice/Posts.yml
Define Fixtures:
DataFixtures/Alice/ directory.Users.yml):
App\Entity\User:
name: '<firstName> <lastName>'
email: '<email>'
roles: ['ROLE_USER']
Load Fixtures Programmatically:
$manager = $this->get('h4cc_alice_fixtures.manager');
$objects = $manager->loadFiles([__DIR__.'/Users.yml']);
$manager->persist($objects, $dropSchema = false);
Use FixtureSets for Complex Scenarios:
$set = $manager->createFixtureSet();
$set->addFile(__DIR__.'/Users.yml');
$set->addFile(__DIR__.'/Posts.yml');
$set->setLocale('de_DE');
$set->setDoDrop(true); // Drops and recreates schema
$manager->load($set);
Autoload FixtureSets:
Place FixtureSet classes in DataFixtures/Alice/ with Set.php suffix. Load all with:
php bin/console h4cc_alice_fixtures:load:sets
Testing:
Load fixtures in setUp() for PHPUnit tests:
public function setUp(): void {
$client = static::createClient();
$manager = $client->getContainer()->get('h4cc_alice_fixtures.manager');
$manager->load(require(__DIR__.'/Fixtures/TestSet.php'));
}
Custom Providers: Extend Faker with custom providers (e.g., for domain-specific data):
services:
app.faker.provider:
class: App\Fixtures\CustomFakerProvider
tags:
- { name: h4cc_alice_fixtures.provider }
Use in fixtures:
App\Entity\Product:
name: '<customProvider()->generateName()>'
Processors: Modify entities before/after persistence:
services:
app.alice.processor:
class: App\Fixtures\Processor\EncryptEmailProcessor
tags:
- { name: h4cc_alice_fixtures.processor }
Multi-Database Support:
Configure multiple managers in config_dev.yml:
h4cc_alice_fixtures:
managers:
orm:
doctrine: orm
mongodb:
doctrine: mongodb-odm
Access via:
$ormManager = $this->get('h4cc_alice_fixtures.orm_manager');
$mongoManager = $this->get('h4cc_alice_fixtures.mongodb_manager');
Schema Drop Warning:
setDoDrop(true) drops all tables managed by Doctrine. Use only in dev/test environments.Circular References:
load:sets share a global reference pool. Circular references (e.g., User referencing Post which references User) will fail.loadFiles() for isolated loads.Faker Seed Consistency:
seed for reproducible data:
h4cc_alice_fixtures:
seed: 42
$set->setSeed(123);
Performance:
do_flush: true can be slow for large datasets. Disable flushing if not needed:
h4cc_alice_fixtures:
do_flush: false
Bundle Order:
AppKernel bundle order. Use setOrder() to customize:
$set->setOrder(10); // Load after default order (1)
Verify Fixture Loading:
$objects = $manager->loadFiles([$file]);
dump(count($objects)); // Debug count
Locale Issues:
de_DE for German names):
h4cc_alice_fixtures:
locale: de_DE
Processor Debugging:
dump() in custom processors to inspect entity states:
public function prePersist($entity) {
dump($entity); // Debug before persistence
}
Schema Drop Errors:
schema_manager service configuration.Custom Loaders:
h4cc_alice_fixtures.loader.factory service. Example:
services:
custom_loader_factory:
class: App\Fixtures\CustomLoaderFactory
decorates: h4cc_alice_fixtures.loader.factory
Dynamic Fixture Generation:
$set = $manager->createFixtureSet();
for ($i = 0; $i < 10; $i++) {
$set->addFile(__DIR__.'/dynamic/Post_'.$i.'.yml');
}
Conditional Loading:
if ($this->getEnvironment() === 'test') {
$manager->load($set);
}
MongoDB-Specific Quirks:
doctrine: mongodb-odm is set and collections are properly mapped. Example config:
h4cc_alice_fixtures:
managers:
mongodb:
doctrine: mongodb-odm
schema_tool: ~ # MongoDB doesn't need schema drops
How can I help you explore Laravel packages today?