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
611 stars 63 forks source link

withAttributes in documentation #480

Closed MichaelBrauner closed 1 week ago

MichaelBrauner commented 12 months ago

I am currently using the Zenstruck Foundry Bundle and finding it very helpful. However, I came across a section in the documentation that seems a bit unclear to me, and I was wondering if you could help clarify.

The section is located here. There's an example code snippet that shows how to use the withAttributes method in the factory:

$posts = PostFactory::new(['title' => 'Post A'])
    ->withAttributes([
        'body' => 'Post Body...',
        'category' => CategoryFactory::new(['name' => 'php']),
    ])
    //... rest of the code
;

In this code, a new Category instance is created with the name 'php' for each Post. However, in the comments of the following getCategory() method calls, it is indicated that the Category is random:

$posts[0]->getCategory(); // random Category
$posts[1]->getCategory(); // random Category (different than above)

Could you help clarify if the Category should indeed be random as per the comments, or if it should be the 'php' category as created in the withAttributes method call? Is there a piece of the puzzle I'm missing here, or could the documentation benefit from some clarification?

Thanks for your time and all the hard work on this bundle. Looking forward to your response.

Best, Michael

kbond commented 11 months ago

Hi Michael, thanks for the report. This is indeed confusing.

It is indeed a random category (each are created by createOne). I think I was trying to demonstrate the first category (with the one with name 'php' being overridden by the second). Confusing just describing that 😵‍💫