bettergist/hautelook-alice-bundle
Installation
Add the package via Composer in a Laravel project (note: this is a Symfony bundle, but can be adapted for Laravel via bridge packages like symfony/bundle or spatie/laravel-symfony-support):
composer require bettergist/hautelook-alice-bundle
*For Laravel, ensure compatibility with FidryAliceDataFixtures (e.g., Doctrine ORM) by installing:
composer require theofidry/alice-data-fixtures-bundle
Configuration
Publish the bundle’s config (if available) or manually define in config/alice.php:
return [
'fixtures' => [
'path' => database_path('fixtures'),
'namespace' => 'Database\\Fixtures',
],
'faker_locale' => 'en_US',
];
First Fixture
Create a YAML fixture file (e.g., database/fixtures/users.yml):
App\Models\User:
user1:
name: '<name>'
email: '<email>'
password: '$2y$10$...' # Use Faker for hashing
Load it via Artisan (adapt for Laravel):
php artisan alice:load --fixtures=users
Fixture Organization
users.yml, posts.yml).App\Models\Post:
post1:
title: '<sentence>'
user: '@user1' # Reference existing fixture
Dynamic Data with Faker Leverage Faker providers in fixtures:
App\Models\Product:
product1:
name: '<color> <fruit>'
price: '<randomFloat(2, 10, 100)>'
Laravel-Specific Adaptations
AppServiceProvider:
$this->app->register(Hautelook\AliceBundle\AliceBundle::class);
// app/Console/Commands/LoadFixtures.php
use Hautelook\AliceBundle\Command\LoadFixturesCommand;
class LoadFixtures extends LoadFixturesCommand {
protected function getFixtures() {
return ['users', 'posts']; // Custom logic
}
}
Testing Integration Use fixtures in PHPUnit tests:
public function setUp(): void {
$this->loadFixtures(['users']);
}
Symfony vs. Laravel
FidryAliceDataFixtures. For Eloquent, use dama/doctrine-test-bundle as a bridge or stick to Doctrine.app/Console/Kernel.php:
protected $commands = [
\Hautelook\AliceBundle\Command\LoadFixturesCommand::class,
];
Fixture Loading Order
users) before child fixtures (e.g., posts). Use --fixtures flag to specify order:
php artisan alice:load --fixtures=users,posts
Faker Locale Issues
config/alice.php or per fixture:
# users.yml
App\Models\User:
user1:
name: '<name{fr_FR}>' # French name
Database Constraints
// In a custom command
$connection = $this->getContainer()->get('database_connection');
$connection->getSchemaManager()->getDatabasePlatform()->getForeignKeysSQL('your_table');
Dry Run
Use --dry-run to preview loaded data without committing:
php artisan alice:load --fixtures=users --dry-run
Logging
Enable debug mode in config/alice.php:
'debug' => env('APP_DEBUG', false),
Fixture Validation Validate YAML syntax with:
php artisan alice:validate --fixtures=users
Custom Faker Providers
Register new Faker providers in config/alice.php:
'faker_providers' => [
\YourNamespace\CustomFakerProvider::class,
],
Post-Load Callbacks
Use Symfony events (if supported) or Laravel’s Model::booted() to run logic after fixture load:
// app/Models/User.php
protected static function booted() {
static::created(function ($user) {
// Post-load logic
});
}
Environment-Specific Fixtures
Load different fixtures per environment (e.g., staging.yml, production.yml) by extending the Artisan command:
protected function getFixtures() {
return config('alice.fixtures.'.config('app.env'));
}
How can I help you explore Laravel packages today?