Skip to content

[5.3] Create factory modifiers - #14241

Merged
taylorotwell merged 1 commit into
laravel:5.3from
browner12:factory-modifiers
Oct 6, 2016
Merged

taylorotwell merged 1 commit into
laravel:5.3from
browner12:factory-modifiers

Conversation

@browner12

Copy link
Copy Markdown
Contributor

i've come to find that I would would like to use factories a little different than is currently available. Currently you can define and defineAs factories. This works okay, but where I run into issues is where I have a lot of variable types. For example, I may have a Blog that has a published_at field and an user_id field. The Blog can be published or unpublished, and it can have an author or be written by anonymous. I could have a published authored Blog, an unpublished authored Blog, a published anonymous Blog, and an unpublished anonymous Blog. Currently, I would have to write 5 different factory types for this.

Ideally what I would like is 'modifiers' for each factory. To define them I would:

$factory->define(App\Blog::class, function (Faker\Generator $faker) {
    return [
        'title'        => $faker->word,
        'body'         => $faker->paragraph(2),
        'published_at' => null,
        'user_id'      => null,
    ];
});

$factory->defineModifier(\App\Blog::class, 'published', function (\Faker\Generator $faker) {

    return [
        'published_at' => $faker->dateTime,
    ];
});

$factory->defineModifier(\App\Blog::class, 'authored', function (\Faker\Generator $faker) {

    return [
        'user_id' => $faker->numberBetween(1, 10),
    ];
});

This would allow me to mix and match modifiers as needed. Then to use the factories I would

factory(\App\Blog::class, 5)->create();
factory(\App\Blog::class, 5)->modifiers('published')->create();
factory(\App\Blog::class, 5)->modifiers('authored')->create();
factory(\App\Blog::class, 5)->modifiers(['authored', 'published'])->create();

I believe this offers a lot more flexibility, and still allows people to do it the current way if they prefer.

@GrahamCampbell GrahamCampbell changed the title create factory modifiers [5.2] Create factory modifiers Jul 6, 2016
@kaizenlabsdev

Copy link
Copy Markdown
Contributor

See Multiple Factory Types in Model Factories

@browner12

Copy link
Copy Markdown
Contributor Author

thanks, I am aware of the multiple factories types. unfortunately, unless I'm mistaken, they don't allow you to apply multiple 'modifications' to the factory.

@browner12

Copy link
Copy Markdown
Contributor Author

wondering if there are any thoughts on this?

@browner12

Copy link
Copy Markdown
Contributor Author

am i supposed to fix this @GrahamCampbell or do we let StyleCI handle it afterwards?

@GrahamCampbell GrahamCampbell changed the title [5.2] Create factory modifiers [5.3] Create factory modifiers Aug 23, 2016
@GrahamCampbell
GrahamCampbell changed the base branch from 5.2 to 5.3 August 23, 2016 13:02
@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Yeh, don't worry about StyleCI. :)

@kamui545

Copy link
Copy Markdown
Contributor

Looking forward to this, pretty neat!

@taylorotwell
taylorotwell merged commit 1a4c410 into laravel:5.3 Oct 6, 2016
@taylorotwell

Copy link
Copy Markdown
Member

Sorry this took so long. I renamed "modifiers" to "states"... pretty much works the same though 👍

@tillkruss

tillkruss commented Oct 6, 2016 •

Copy link
Copy Markdown
Contributor

@browner12: Nice! Could you update the docs as well?

@browner12

Copy link
Copy Markdown
Contributor Author

no problem. glad it worked out.

was just going to ask, @taylorotwell would you like me to PR a docs update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants