sajad torkamani

In a nutshell

Alice lets you create fixtures/fake data for testing using YAML/JSON/PHP files.

Recipes

Create a number of objects using ranges

Nelmio\Entity\User:
    user{1..10}:
        username: bob
        fullname: Bob
        birthDate: 1980-10-10
        email: bob@example.org
        favoriteNumber: 42

Use list of values

Nelmio\Entity\User:
    user_{alice, bob}:
        username: '<current()>'
        fullname: '<current()>'
        birthDate: 1980-10-10
        email: '<current()>\@example.org'
        favoriteNumber: 42

Get name (key) of current fixture

Use the <current()> token to get the current fixture name:

Nelmio\Entity\User:
    user_{alice, bob}:
        username: '<current()>'
        fullname: '<current()>'
        birthDate: 1980-10-10
        email: '<current()>\@example.org'
        favoriteNumber: 42

In the above example, <current()> will return user_alice for the value alice and user_bob for the value bob.

If we used a range like user{1..0}, <current()> will return user1, user2, etc.

Reference other fixtures

Nelmio\Entity\User:
    user_{1..10}:
        username: '<name()>'

Nelmio\Entity\UserDetail:
    userdetail_{@user_*}:  # is going to generate `userdetail_user_1`, `userdetail_user_2`, ..., `userdetail_user_10`
        user: <current()>
        email: '<email()>'

Reference specific fixture

Nelmio\Entity\User:
    user_bob:
        username: 'bob'

Nelmio\Entity\UserDetail:
    userdetail_{@user_bob}:
        user: <current()>   # holds `@user_bob`
        email: 'bob@test.de'

Ensure value is unique

Nelmio\Entity\User:
    user{1..10}:
        username (unique): '<username()>'

For method calls or constructors, you can do something like:

Nelmio\Entity\User:
    user{1..10}:
        __construct:
            0 (unique): '<username()>'

Ensure array-like property is unique

Nelmio\Entity\User:
    friends{1..2}:
        username (unique): '<username()>'
    user{1..2}:
        friends (unique): '@friends*' # array value

In the above example, user1#friends and `user2#friends won’t contain any duplicate fixture values.

Handle relations

Nelmio\Entity\User:
    # ...

Nelmio\Entity\Group:
    group1:
        name: Admins
        owner: '@user1'
        members: ['@user2', '@user3']

Or:

Nelmio\Entity\User:
    # ...

Nelmio\Entity\Group:
    group1:
        name: Admins
        owner: '@user1'
        members: '5x @user*'

Or:

members: '<numberBetween(1, 10)>x @user*'

Randomly set data

Use notation like '50%? value : otherValue to randomly set data.

Nelmio\Entity\User:
    user{1..10}:
        username: '<username()>'
        fullname: '<firstName()> <lastName()>'
        birthDate: '<date_create()>'
        email: '<email()>'
        favoriteNumber: '50%? <numberBetween(1, 200)>'

In the above example, 50% of users will have a favoriteNumber, the other 50% will their favoriteNumber set to null.

Specify constructor arguments

If your entities have constructors that require arguments, you can specify the arguments like so:

Nelmio\Entity\User:
    user1:
        __construct: ['<username()>']

This will do something like:

new Nelmio\Entity\User(someRandomUserName)

Don’t execute the constructor

Nelmio\Entity\User:
    user1:
        __construct: false

Other notes

Sources