Install the Bundle
composer require hautelook/alice-bundle --dev
Register in config/bundles.php (Symfony 4+):
Hautelook\AliceBundle\HautelookAliceBundle::class => ['dev' => true],
Enable Fixtures
Add to config/packages/hautelook_alice.yaml:
hautelook_alice:
connection: default # Your Doctrine connection
locale: en_US # Default locale for Faker
First Fixture
Create a YAML file (e.g., config/fixtures/users.yml):
App\Entity\User:
user{1..10}:
email: '<email>'
username: '<userName>'
roles: ['ROLE_USER']
Load fixtures via CLI:
php bin/console hautelook:fixtures:load
Verify Check database records or use:
php bin/console hautelook:fixtures:show
posts table with 50 test records for frontend testing.config/fixtures/posts.yml:
App\Entity\Post:
post{1..50}:
title: '<sentence>'
content: '<paragraph>'
publishedAt: '<dateTimeBetween>-1 year'
author: '@user*' # Reference existing 'user' fixture
php bin/console hautelook:fixtures:load --env=dev
users.yml, posts.yml, orders.yml).@entity syntax to reference other fixtures (e.g., @user* for random users).# config/fixtures/ecommerce.yml
App\Entity\Order:
order{1..20}:
user: '@user*'
items:
- { product: '@product*', quantity: '<numberBetween(1,5)>', price: '<randomFloat(2,10,50)>' }
--env flag to load different fixtures per environment:
# Load minimal data for CI
php bin/console hautelook:fixtures:load --env=test
# Load full data for local dev
php bin/console hautelook:fixtures:load --env=dev
config/packages/hautelook_alice.yaml:
hautelook_alice:
fixtures: ['%kernel.project_dir%/config/fixtures/{env}.yml']
App\Entity\Product:
product{1..100}:
name: '<commerceProductName>'
price: '<randomFloat(2,10,1000)>'
sku: '<isbn13>'
description: '<paragraphs(3, true)>'
// src/Faker/Provider/AppProvider.php
namespace App\Faker\Provider;
use Faker\Provider\Base;
class AppProvider extends Base {
public function fakeAppData() {
return 'custom_' . $this->word;
}
}
Register in config/packages/hautelook_alice.yaml:
hautelook_alice:
faker:
providers: ['App\Faker\Provider\AppProvider']
phpunit.xml for consistent test environments:
<env name="KERNEL_CLASS" value="App\Kernel"/>
<env name="APP_ENV" value="test"/>
<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
<file>vendor/autoload.php</file>
<file>config/bootstrap.php</file>
<include file="config/packages/test/_fixtures.php"/>
<exec dir="." command="php bin/console hautelook:fixtures:load --env=test --append"/>
</php>
# config/fixtures/posts.yml
App\Entity\Post:
post{1..10}:
title: '<sentence>'
slug: ~ # Will be set via Doctrine lifecycle
Add a subscriber:
// src/EventSubscriber/FixtureSubscriber.php
namespace App\EventSubscriber;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
class FixtureSubscriber implements EventSubscriber {
public function getSubscribedEvents() {
return [Events::postPersist];
}
public function postPersist(LifecycleEventArgs $args) {
$entity = $args->getObject();
if ($entity instanceof \App\Entity\Post && empty($entity->getSlug())) {
$entity->setSlug(\Ramsey\Uuid\Uuid::uuid4()->toString());
}
}
}
Alice\Loader\NativeLoader for custom logic:
// src/Loader/CustomLoader.php
namespace App\Loader;
use Alice\Loader\NativeLoader;
use Doctrine\Common\Persistence\ObjectManager;
class CustomLoader extends NativeLoader {
protected function getObjectManager(): ObjectManager {
$om = parent::getObjectManager();
// Custom logic (e.g., set default tenant)
return $om;
}
}
Configure in config/packages/hautelook_alice.yaml:
hautelook_alice:
loader: App\Loader\CustomLoader
Fixture Order Dependencies
posts.yml before users.yml fails if Post references User.--append to load incrementally or define a clear order in config/packages/hautelook_alice.yaml:
hautelook_alice:
fixtures_order: ['users.yml', 'posts.yml', 'orders.yml']
Faker Locale Mismatch
<address> generates invalid data in non-English locales.hautelook_alice.yaml or override per fixture:
hautelook_alice:
locale: fr_FR
Or use Faker’s locale methods:
App\Entity\User:
user{1..5}:
address: '<address>'
# Override locale for this field
phone: '<phoneNumber>@fr' # French phone number
Circular References
@post* in users.yml and @user* in posts.yml causes infinite loops.* sparingly or limit references with min/max:
App\Entity\User:
user{1..10}:
posts: '@post{1..3}' # Reference 1-3 posts
Doctrine Proxy Classes
ORM\AbstractQuery::HYDRATE_ARRAY:
// In CustomLoader
$query->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);
Unique Constraints
<email> duplicates violate UNIQUE constraints.App\Entity\User:
user{1..10}:
email: '<uniqueEmail>'
Dry Run Mode
php bin/console hautelook:fixtures:load --dry-run
Verbose Output
php bin/console hautelook:fixtures:load -vvv
Fixture Validation
php bin/console debug:container --parameters
How can I help you explore Laravel packages today?