Alice 3.x no longer ships with a persistence layer, so this library provides one!
- Installation
- Basic usage
- Advanced usage
- Contributing
You can use Composer to install the library to your project:
composer require --dev theofidry/alice-data-fixtures:^1.0@beta nelmio/alice:^3.0@rc
# with Doctrine
composer require --dev theofidry/alice-data-fixtures:^1.0@beta \
nelmio/alice:^3.0@rc \
doctrine/orm:^2.5 \
doctrine/data-fixtures
# with Eloquent
composer require --dev theofidry/alice-data-fixtures:^1.0@beta \
nelmio/alice:^3.0@rc \
illuminate/database:~5.3.0
This library ships with a Symfony bundle FidryAliceDataFixturesBundle
.
To use it with Doctrine do not forget to install doctrine/doctrine-bundle
and enable the DoctrineBundle
(done by default in Symfony Standard Edition).
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();
}
return $bundles;
}
To use it with Eloquent do not forget to install illuminate/database
and
WouterJEloquentBundle
(wouterj/eloquent-bundle
).
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 WouterJ\EloquentBundle\WouterJEloquentBundle(),
];
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
//...
$bundles[] = new Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle();
$bundles[] = new Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle();
}
//...
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
//...
$bundles[] = new Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle();
}
return $bundles;
}
The full configuration reference is:
# app/config/config.yml
# Default config
fidry_alice_data_fixtures:
db_drivers:
doctrine_orm: ~
doctrine_mongodb_odm: ~
doctrine_phpcr_odm: ~
eloquent_orm: ~
For each driver, is the appropriate bundle is detected, e.g. DoctrineORMBundle for Doctrine and WouterJEloquentBundle
for Eloquent, the services related to those driver will be enabled. If you want to skip those checks you can turn
a specific driver to true
instead. If you want to disable a specific driver, simply force the value false
instead.
Create a fixture file in src/AppBundle/Resources/fixtures
:
# src/AppBundle/Resources/fixtures/dummy.yml
AppBundle\Entity\Dummy:
dummy_{1..10}:
name: <name()>
related_dummy: '@related_dummy*'
# src/AppBundle/Resources/fixtures/related_dummy.yml
AppBundle\Entity\RelatedDummy:
related_dummy_{1..10}:
name: <name()>
Then you can load those files using a LoaderInterface
:
$files = [
'path/to/src/AppBundle/Resources/fixtures/dummy.yml',
'path/to/src/AppBundle/Resources/fixtures/related_dummy.yml',
];
// Choose your loader
$loader = $container->get('fidry_alice_data_fixtures.doctrine.loader'); // For Doctrine ORM
$loader = $container->get('fidry_alice_data_fixtures.doctrine_mongodb.loader'); // For Doctrine MongoDB ODM
$loader = $container->get('fidry_alice_data_fixtures.doctrine_phpcr.loader'); // For Doctrine PHPCR
$loader = $container->get('fidry_alice_data_fixtures.eloquent.loader'); // For Eloquent ORM
$objects = $loader->load($files);
// $objects is now an array of persisted `Dummy` and `RelatedDummy`
Clone the project, install the dependencies and use bin/test.sh
to run all the tests!