zenstruck / foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html
MIT License
608 stars 63 forks source link

Support need for specific cases #568

Closed Zer0NimO-web closed 3 months ago

Zer0NimO-web commented 3 months ago

Hello,

I couldn't find a way to do some specific things while reading Foundry documentation.

First, i want to know if it is possible for a many-to-many relationship (between DIrectory and User) to do this :

     DirectoryFactory::createMany(
            3,
            [
                // ... stuff
                'users' => UserFactory::random(3, [ 'sharingZones' => [ $theOneIWant ] ]),   
                // here i want to add few users which are linked to a specific sharingZone
                // (a user can be linked to many sharingZones)
            ]
        );

Then, i want to build stories with parameters such as :

      SharingZoneStory::build($users, $documents);

But i don't know how to create a story which involves such parameters for the build() function.

Thanks a lot for your time.

nikophil commented 3 months ago

Hello,

for your first question, I think you should do this way:

    DirectoryFactory::createMany(
           3,
           [
               'users' => UserFactory::new([ 'sharingZones' => [ $theOneIWant ] ])->random(3),   
           ]
       );

For the second question, it is currently not possible to parametrize stories. This would be a nice addition, but which would come with some complexity because stories are only loaded once, even if you call it twice

Zer0NimO-web commented 3 months ago

Thank you very much. For the first question i'm afraid it won't work since i don't want to create new users, i want to get users already persisted which are linked with a specific sharingZone.

It's clear for the second case. Thanks a lot.

nikophil commented 3 months ago

oups, you're right :sweat_smile:

what about randomSet()?

Zer0NimO-web commented 3 months ago

It could do the job but still i don't know how to write the condition. I don't even know if it is possible to do something like :


UserFactory::randomSet(4, ['SharingZones' => /* ... is linked to $specificSharingZone ... */]);
nikophil commented 3 months ago

sorry, I was not very helpful on this one :sweat_smile:

I was thinking at first you had a ManyToOne between User and SharingZones. I think you cannot do it for *ToMany :man_shrugging:

Zer0NimO-web commented 3 months ago

No problem, you helped me a lot!