Skip to content

Latest commit

 

History

History
124 lines (96 loc) · 2.55 KB

service_provider.md

File metadata and controls

124 lines (96 loc) · 2.55 KB

Service Provider

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.

Add a service

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"

Use a service

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: max@example.de

Complex usecase

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: test@googlemail.com
            groups: ["@group:group1"]
            notice: "Sit vitae voluptas sint non voluptates."
        user1:
            name: Franz Müller
            email: example@yahoo.com
            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.