Setting up parameter as array in Nelmio Alice fixture generator Setting up parameter as array in Nelmio Alice fixture generator symfony symfony

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: admin@local.com        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*


The problem I believe is that you need to set an array of roles, so you can't just return one role. Either do this in-line:

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*

Another issue could be that when you give an array to something it may unpack it as a list of arg. Try to pass it [ ['Foo'] ] i.e. an array as the first arg of the other array. Either way once you figure it out I think you should send a pull request for the docs or file an issue at least because this probably shouldn't be so complicated.


Try this one

roles: <randomElements(['ROLE_ADMIN', 'ROLE_USER', 'ROLE_PROFILE_ONE', 'ROLE_PROFILE_TWO'], 2)>

randomElements args:
1- array 'Array to take elements from. Defaults to a-f'.
2- integer 'Number of elements to take'.
3- boolean 'Allow elements to be picked several times. Defaults to false'.

It will return array.