Fixtures can use values from a service, like the Faker service, which generates fake data.
There are a few possibilities;
- Write your own converter or event listener, or,
- Add a service from the service provider to the FixtureManager.
You can register a service easy over the FixtureManager.
use DavidBadura\Fixtures\FixtureManager\FixtureManager;
$fixtureManager = FixtureManager::createDefaultFixtureManager($objectManager);
$faker = \Faker\Factory::create();
$fixtureManager->addService('faker', $faker);
Tip to use the faker, you must install the faker package. As example over composer:
{
"require": {
"fzaninotto/faker": "~1.1"
}
}
or over cli composer.phar require fzaninotto/faker "~1.1"
After the service is registerd, your can use ist like this <{ServiceName}::{MethodName}({Attributes]}>
# install.yml
user:
properties:
class: "YourBundle\Entity\User"
data:
david:
name: <faker::name()>
email: <faker::email()>
The example above is resolved as follows:
# install.yml
user:
properties:
class: "YourBundle\Entity\User"
data:
david:
name: Max Mustermann
email: [email protected]
This is a complex example:
# install.yml
user:
properties:
class: "YourBundle\Entity\User"
data:
user{0..1}:
name: <faker::name()>
email: <faker::email()>
groups: ["@group:group{0..1}"]
notice: "<faker::sentence(5)>"
group:
properties:
class: "YourBundle\Entity\Group"
data:
group{0..1}:
name: <faker::name()>
And will be convertet to:
# install.yml
user:
properties:
class: "YourBundle\Entity\User"
data:
user0:
name: Max Mustermann
email: [email protected]
groups: ["@group:group1"]
notice: "Sit vitae voluptas sint non voluptates."
user1:
name: Franz Müller
email: [email protected]
groups: ["@group:group1"]
notice: "Sit vitae voluptas sint non voluptates."
group:
properties:
class: "YourBundle\Entity\Group"
data:
group0:
name: Test
group1:
name: Admin
The Faker Generator has a lot of methods. For more information read the documentation.