A Symfony bundle to manage fixtures with nelmio/alice and fzaninotto/Faker.
The database support is done in FidryAliceDataFixtures. Check this project to know which database/ORM is supported.
Warning: this is the documentation for HautelookAliceBundle 2.0. If you want to check the documentation for 1.x, head this way.
HautelookAliceBundle changed a lot, it first was acting as a simple bundle for nelmio/alice, it then started to ship some additional features to enrich it.
HautelookAliceBundle 1.x was the first milestone reaching a certain level of maturity in its usage:
- Easily load a set of fixtures from a command
- Being able to define different sets of fixtures for multiple environments
- Customize the data generation with custom Faker providers
- Customize the loading behaviour with processors
HautelookAliceBundle 2.x changes a lot, although not so much. In 1.x, a lot of complexity was brought in the bundle due to nelmio/alice 2.x limitations and were at best workarounds (like the lack of handling of circular references). A lot of that complexity has been pushed back to nelmio/alice 3.x which has a much more flexible design. As a result:
- nelmio/alice 3.x allows you to easily create PHP objects with random data in an elegant way
- FidryAliceDataFixtures is a persistence layer for nelmio/alice 3.x. If you need to persist the loaded objects, it is the package you need. It provides you the flexibility to be able to purge the data between each loadings or wrap the loading in a transaction for your tests for example to simply rollback once the test is finished instead of calling an expansive purge.
- hautelook/alice-bundle 2.x is now only focused on the fixture discovery: find the appropriate files and load them. If you need to load specific sets of files for your tests, FidryAliceDataFixtures is enough.
With Symfony Flex (recommended):
# If you do not have Doctrine installed yet:
composer require doctrine-orm
composer require --dev hautelook/alice-bundle
Without Flex you will have to install doctrine/orm
and register the bundles accordingly in app/AppKernel.php
or
wherever your Kernel class is located:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
// ...
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
];
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
//...
$bundles[] = new Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle();
$bundles[] = new Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle();
$bundles[] = new Hautelook\AliceBundle\HautelookAliceBundle();
}
return $bundles;
}
Configure the bundle to your needs, for example:
# config/packages/dev/hautelook_alice.yaml
hautelook_alice:
fixtures_path: 'fixtures' # Path to which to look for fixtures relative to the project directory or the bundle path.
root_dirs:
- '%kernel.root_dir%'
- '%kernel.project_dir%'
If you are using a non-flex architecture, you may want to use Resources/fixtures
instead of fixtures
.
Assuming you are using Doctrine, make sure you
have the doctrine/doctrine-bundle
and
doctrine/data-fixtures
packages installed.
Then create a fixture file in one of the following location:
fixtures
if you are using flexapp/Resources/fixtures
if you have a non-flex bundle-less Symfony applicationsrc/AppBundle/Resources/fixtures
or any bundle under which you want to place the fixtures
# fixtures/dummy.yaml
App\Entity\Dummy:
dummy_{1..10}:
name: <name()>
related_dummy: '@related_dummy*'
# fixtures/related_dummy.yaml
App\Entity\RelatedDummy:
related_dummy_{1..10}:
name: <name()>
Then simply load your fixtures with the doctrine command php bin/console hautelook:fixtures:load
.
If you want to load the fixtures of a bundle only, do php bin/console hautelook:fixtures:load -b MyFirstBundle -b MySecondBundle
.
See more.
Next chapter: Advanced usage
- Behat extension: AliceBundleExtension
- Bundle for generating AliceBundle compatible fixtures directly from Doctrine entities: AliceGeneratorBundle
- Upgrade guide
- Changelog
This bundle was originaly developped by Baldur RENSCH and HauteLook. It is now maintained by Théo FIDRY.