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

[10.x] Allow Factory::count() to consume callables to be able to generate randomness during runtime. #49637

Closed
wants to merge 2 commits into from

Conversation

derpoho
Copy link

@derpoho derpoho commented Jan 10, 2024

Often one needs some randomness in generating Factories for the development environment. For this case the count method in the Factory Base class should accept a callable in addition to int or null. It's optional to be used by developers and i think it has no breaking changes or follow up problems - at least i cannot think of anything.

What could be done with this addition?

AnyModel::factory()->count(function() {
    return fake()->randomDigitNotNull();
});

AnyModel::factory()->count(fn() => fake()->randomDigitNotNull());

Hope this is of interest!

@derpoho derpoho changed the title Allow Factory::count() to consume callables to be able to generate randomness during runtime. [10.x] Allow Factory::count() to consume callables to be able to generate randomness during runtime. Jan 10, 2024
@taylorotwell
Copy link
Member

taylorotwell commented Jan 11, 2024

Do you have maybe a more real-world example? In your original example couldn't you just do ->count(random_int(1, 10))?

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@derpoho
Copy link
Author

derpoho commented Jan 15, 2024

@taylorotwell Thanks for your reply! Sure, here is my current use case as a description. I am sure there are more out there. Here is a discussion with some replies which take on this topic too: #36289

I think mainly it solves randomness during development in combination with relationships.

Let's say i want to make a Quiz, which has Sections and those sections have Questions.

Quiz::factory()
    ->has(
        QuizSection::factory()
            ->has(
                factory: QuizQuestion::factory()->count(random_int(1, 10)),
                relationship: 'questions'
            )
            ->count(random_int(1, 10)),
        'sections'
    )
    ->count(4)
    ->create();

This will create 4 Quizzes, all with the same 1-10 amount of sections and those sections with 1-10 amount of questions.
But all quizzes will be the same.

By extending count to take a resolvable function we could delay the count until runtime of the factory::make call. By doing so we can create 4 Quizzes with each random count of sections and those random count of questions. So basically complete randomness. This helps during development, especially if you want this randomness for the interface.

Quiz::factory()
   ->has(
       QuizSection::factory()
           ->has(
               factory: QuizQuestion::factory()->count(fn() => random_int(1, 10)),
               relationship: 'questions'
           )
           ->count(fn() => random_int(1, 10)),
       'sections'
   )
   ->count(4)
   ->create();

I hope this defines the use case better and you see the value in this.

@derpoho
Copy link
Author

derpoho commented Jan 22, 2024

Do you have maybe a more real-world example? In your original example couldn't you just do ->count(random_int(1, 10))?

@taylorotwell see above for a deeper example/use case.

@woodspire
Copy link

@taylorotwell @derpoho It would be really useful for me.

I am using a Factory sequence to alternate between different types of relations.
On the first sequence element, I would like 3 relations. For the next sequence entry, I would like 10. I cannot do that right now.

@WildEgo
Copy link

WildEgo commented Feb 8, 2024

@taylorotwell Thanks for your reply! Sure, here is my current use case as a description. I am sure there are more out there. Here is a discussion with some replies which take on this topic too: #36289

I think mainly it solves randomness during development in combination with relationships.

Let's say i want to make a Quiz, which has Sections and those sections have Questions.

Quiz::factory()
    ->has(
        QuizSection::factory()
            ->has(
                factory: QuizQuestion::factory()->count(random_int(1, 10)),
                relationship: 'questions'
            )
            ->count(random_int(1, 10)),
        'sections'
    )
    ->count(4)
    ->create();

This will create 4 Quizzes, all with the same 1-10 amount of sections and those sections with 1-10 amount of questions. But all quizzes will be the same.

By extending count to take a resolvable function we could delay the count until runtime of the factory::make call. By doing so we can create 4 Quizzes with each random count of sections and those random count of questions. So basically complete randomness. This helps during development, especially if you want this randomness for the interface.

Quiz::factory()
   ->has(
       QuizSection::factory()
           ->has(
               factory: QuizQuestion::factory()->count(fn() => random_int(1, 10)),
               relationship: 'questions'
           )
           ->count(fn() => random_int(1, 10)),
       'sections'
   )
   ->count(4)
   ->create();

I hope this defines the use case better and you see the value in this.

Yeah I ended up having the same requirement today, it's nice to mockup and test some stuff, I manually modified the vendor folder, would be sweet to get this merged honestly

@derpoho
Copy link
Author

derpoho commented Feb 11, 2024

@taylorotwell is too busy ;) Maybe it will come up some time again.

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.

4 participants