Developed with love by KnpLabs Hire us for your project!
440

AliceBundle

by hautelook

A Symfony bundle to manage fixtures with Alice and Faker.

AliceBundle

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.

Package version
Build Status
SensioLabsInsight
Dependency Status
Scrutinizer Code Quality
Code Coverage
Slack

When to use this bundle?

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.

Documentation

  1. Install
  2. Basic usage
  3. Advanced usage
    1. Enabling databases
    2. Environment specific fixtures
    3. Fixtures parameters
      1. Alice parameters
      2. Application parameters
    4. Use service factories
    5. Load fixtures in a specific order
      1. Load fixtures in a specific order
      2. Persisting the classes in a specific order
  4. Custom Faker Providers
  5. Custom Alice Processors
  6. Resources

Other references:

Installation

Example of installation:

# If you are using Symfony standard edition, you can skip this step
composer require doctrine/doctrine-bundle doctrine/orm:^2.5

composer require --dev hautelook/alice-bundle doctrine/data-fixtures

Explanation: HautelookAliceBundle uses FidryAliceDataFixtures for the
persistence layer. As FidryAliceDataFixtures is compatible with different databases/ORM, one cannot be installed by
default. In the example above, we are using Doctrine ORM which requires
doctrine/orm doctrine/orm-bundle doctrine/data-fixtures.

Then, enable the bundle by updating your app/AppKernel.php file to enable the bundle:

<?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 (example with default values):

# app/config/config_dev.yml

hautelook_alice:
    fixtures_path: 'Resources/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%'

Basic usage

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:

  • Resources/fixtures if you are using flex
  • app/Resources/fixtures if you have a non-flex bundle-less Symfony application
  • src/AppBundle/Resources/fixtures or any bundle under which you want to place the fixtures
# Resources/fixtures/dummy.yml

AppBundle\Entity\Dummy:
    dummy_{1..10}:
        name: <name()>
        related_dummy: '@related_dummy*'
# Resources/fixtures/related_dummy.yml

AppBundle\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

Resources

Credits

This bundle was originaly developped by Baldur RENSCH and HauteLook. It is now maintained by Théo FIDRY.

Other contributors.

License

license

The MIT License (MIT)

Copyright (c) 2013 HauteLook

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • Update fixtures path (#400)
    By theofidry, 1 month ago
  • Adds a check for service tag (#393)
    By theofidry, 2 months ago
  • Update the badge to stable release
    By web-flow, 2 months ago
  • Update documentation for stable release (#384)
    By web-flow, 4 months ago
  • Allow Symfony 4 (#383)
    By web-flow, 4 months ago
  • Fix class path in FooProvider example (#382)
    By theofidry, 4 months ago
  • Fix Travis build with lower deps (#379)
    By web-flow, 4 months ago
  • Fix Travis build with lower deps
    By theofidry, 4 months ago
  • Update to PHPUnit 6.0 and README
    By theofidry, 4 months ago
  • Fix purger injection (#376)
    By theofidry, 5 months ago
  • Allow Symfony 4.0 (#375)
    By web-flow, 5 months ago
  • Refactor the doctrine command (#371)
    By web-flow, 5 months ago
  • Make logger argument optional in loader service (#374)
    By theofidry, 5 months ago
  • Add doc entry for HautelookAliceBundle usage (#372)
    By web-flow, 5 months ago
  • Add consistent behaviour of hautelook_alice.fixtures_path (#370)
    By theofidry, 5 months ago
  • Add documentation to define custom orders (#369)
    By web-flow, 5 months ago
  • Fix purge with truncate (#368)
    By web-flow, 5 months ago
  • Add support for autoconfiguring providers (#361)
    By theofidry, 5 months ago
  • Update deps and fix tests (#367)
    By web-flow, 5 months ago
  • Fix some broken links in UPGRADE.md (#365)
    By theofidry, 6 months ago
  • Fix the append option (#345)
    By web-flow, 9 months ago
  • Update composer description
    By web-flow, 9 months ago
  • Add support for Flex and bundless applications (#342)
    By theofidry, 10 months ago
  • Change Gitter for Slack
    By web-flow, 1 year ago
  • Upgrade to the last version of Alice (#297)
    By theofidry, 1 year ago
  • [docs] Fix the command name (#295)
    By theofidry, 1 year ago
  • [docs] Use bin/console instead of app/console (#294)
    By theofidry, 1 year ago
  • [docs] Missing parenthesis in bundle registration (#293)
    By theofidry, 1 year ago
  • Fix path for 1.x stable documentation (#289)
    By theofidry, 1 year ago
  • Delete cache files (#290)
    By theofidry, 1 year ago