Setting up parameter as array in Nelmio Alice fixture generator

Essentially just a guess based on a quick look at the docs, but I suspect the problem may be that in roles: 35%? [ROLE_ADMIN, ROLE_USER, ROLE_PROFILE_ONE, ROLE_PROFILE_TWO] the bit after roles: is being interpreted as a single string, because it doesn't start with [ as a normal YAML array would need to.

As for a solution, I suspect that you can't do it straight in the YAML like that.

One (not proven) option: use a custom Faker method:

Faker

public function roles()
{
    return = ['ROLE_ADMIN', 'ROLE_USER', 'ROLE_PROFILE_ONE', 'ROLE_PROFILE_TWO'];
}

YAML

User{1..10}:
    username: <firstNameMale>
    email: <companyEmail>
    enabled: <boolean(35)>
    plainPassword: <lexify>
    roles: 35%? <roles()>
    groups: @Group*

Final query: do you really want Alice to assign all those Roles to the User 35% of the time? If not, and in fact you want some probability-based choice of one of them in each User, then I suppose what you need is still a custom method, but put the selection logic in there instead of in the YAML.

EDIT

Ah, sounds like you want random single Roles for each test instance, in which case you'll need custom code something like this:

public function randomRole()
{
    $names = ['ROLE_ADMIN', 'ROLE_USER', 'ROLE_PROFILE_ONE', 'ROLE_PROFILE_TWO'];

    return $names[array_rand($names)];
}

According to Alice it looks like you can stick that straight in the YAML like this:

User{1..10}:
    username: <firstNameMale>
    email: <companyEmail>
    enabled: <boolean(35)>
    plainPassword: <lexify>
    roles: <?php $names = ['ROLE_ADMIN', 'ROLE_USER', 'ROLE_PROFILE_ONE', 'ROLE_PROFILE_TWO']; echo $names[array_rand($names)]; ?>
    groups: @Group*

Or the AliceFixturesBundle docs tell you how to include a separate Provider (as described above)

services.yml

services:
    your.faker.provider:
        class: YourProviderClass
        tags:
            -  { name: h4cc_alice_fixtures.provider }


This suggestion doesn't work, keeping for posterity but moved down the bottom!

I thought maybe you can do it be defining the array separately at the top and then referring to it, using Alice Value Objects, but since an array is not a normal object I can't see how to instantiate it. You'd want something like this:

Array:
    Array0: [ROLE_ADMIN, ROLE_USER, ROLE_PROFILE_ONE, ROLE_PROFILE_TWO]

UserBundle\Entity\User:
    User0:
        username: admin
        email: [email protected]
        enabled: 1
        plainPassword: admin
        roles: [ROLE_ADMIN]
        groups: @Group0

    User{1..10}:
        username: <firstNameMale>
        email: <companyEmail>
        enabled: <boolean(35)>
        plainPassword: <lexify>
        roles: 35%? @Array0
        groups: @Group*