Installation:
composer require hautelook/alice-bundle
Add to config/bundles.php (Symfony) or config/packages/hautelook_alice.yaml (Laravel via Symfony bridge):
Hautelook\AliceBundle\HautelookAliceBundle::class => ['all' => true],
First Use Case:
Create a YAML fixture file (e.g., config/fixtures/users.yml):
App\Entity\User:
user1:
email: 'user1@example.com'
password: '<password()>'
roles: ['ROLE_USER']
user2:
email: 'user2@example.com'
password: '<password()>'
roles: ['ROLE_ADMIN']
Load fixtures via CLI:
php bin/console hautelook:fixtures:load --fixtures=config/fixtures/users.yml
Laravel Integration:
Use symfony/flex or manually bridge Symfony components. For Laravel, leverage spatie/laravel-symfony-messenger or manually invoke:
$loader = new \Hautelook\AliceBundle\Loader\YamlLoader();
$faker = \Faker\Factory::create();
$objectSet = $loader->load('config/fixtures/users.yml', $faker);
$purger = new \Fidry\AliceDataFixtures\Purger\ORMPurger();
$executor = new \Fidry\AliceDataFixtures\Executor();
$executor->execute($objectSet, $purger);
Fixture Development:
config/fixtures/{module}.yml).<email(), <name()>) for dynamic data.references:
App\Entity\Post:
post1:
title: 'First Post'
author: '@user1' # Reference to user1
Loading Strategies:
php bin/console hautelook:fixtures:load --fixtures=config/fixtures/
bootstrap/app.php or service providers:
$this->loadFixtures('config/fixtures/users.yml');
Helper method:
protected function loadFixtures(string $path) {
$loader = new \Hautelook\AliceBundle\Loader\YamlLoader();
$faker = \Faker\Factory::create();
$objectSet = $loader->load($path, $faker);
$purger = new \Fidry\AliceDataFixtures\Purger\ORMPurger();
$executor = new \Fidry\AliceDataFixtures\Executor();
$executor->execute($objectSet, $purger);
}
Environment-Specific Fixtures:
dev, test, etc.:
config/fixtures/
dev/
users.yml
test/
users.yml
posts.yml
bootstrap/app.php:
if (app()->environment('test')) {
$this->loadFixtures('config/fixtures/test/users.yml');
}
Testing:
public function testUserCreation() {
$this->loadFixtures('config/fixtures/test/users.yml');
$this->assertDatabaseHas('users', ['email' => 'user1@example.com']);
}
DatabaseTransactions trait to reset fixtures after each test.Seeding:
Artisan::call('migrate:fresh --seed') by creating a custom seeder:
class FixtureSeeder extends Seeder {
public function run() {
$this->loadFixtures('config/fixtures/dev/*.yml');
}
}
Database Compatibility:
User) before child entities (e.g., Post).Faker Locale:
config/faker.php (Symfony) or manually:
$faker = \Faker\Factory::create('fr_FR'); // French locale
Fixture Overwriting:
Purger\ORMPurger::setPurgeMode() to control behavior:
$purger->setPurgeMode(\Fidry\AliceDataFixtures\Purger\PurgerInterface::PURGE_MODE_TRUNCATE);
Circular References:
User referencing Post which references User). Use @ syntax sparingly or restructure fixtures.Performance:
$executor->setLogger(new \Monolog\Logger('fixtures'));
$executor->setLoggerLevel(\Psr\Log\LogLevel::WARNING);
Fixture Validation:
php bin/console hautelook:fixtures:load --fixtures=users.yml -v
Database State:
doctrine:schema:validate (Symfony) or schema:dump (Laravel) to verify schema matches fixtures.Faker Issues:
$faker->seed(1234);
Custom Loaders:
\Hautelook\AliceBundle\Loader\LoaderInterface for custom formats (e.g., JSON API):
class JsonApiLoader implements LoaderInterface {
public function load(string $path, \Faker\Generator $faker) {
// Custom logic
}
}
Post-Processing:
EventDispatcher to hook into fixture lifecycle (e.g., hash passwords after load):
# config/packages/hautelook_alice.yaml
hautelook_alice:
events:
post_load: App\EventListener\HashPasswordListener
Laravel Service Provider:
public function register() {
$this->app->singleton(\Faker\Generator::class, function () {
return \Faker\Factory::create();
});
}
Custom Faker Providers:
\Faker\Factory::create()->addProvider(new \YourNamespace\CustomFakerProvider());
Symfony vs. Laravel:
EventDispatcher, Loader). Use spatie/laravel-symfony-messenger for easier integration.Caching:
# config/packages/hautelook_alice.yaml
hautelook_alice:
cache: false
Environment Variables:
.env for dynamic fixture paths:
fixtures_path: '%env(FIxtures_DIR)%/config/fixtures'
How can I help you explore Laravel packages today?