Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Create factory modifiers #14241

Merged
merged 1 commit into from
Oct 6, 2016
Merged

[5.3] Create factory modifiers #14241

merged 1 commit into from
Oct 6, 2016

Conversation

browner12
Copy link
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
@arthurkirkosa
Copy link
Contributor

See Multiple Factory Types in Model Factories

@browner12
Copy link
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
Contributor Author

wondering if there are any thoughts on this?

@browner12
Copy link
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
Member

Yeh, don't worry about StyleCI. :)

@kamui545
Copy link
Contributor

Looking forward to this, pretty neat!

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

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

@tillkruss
Copy link
Collaborator

tillkruss commented Oct 6, 2016

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

@browner12
Copy link
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.

None yet

6 participants