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
607 stars 62 forks source link

Find functions do not refresh entity #638

Closed cooldude77 closed 4 days ago

cooldude77 commented 1 week ago

Hi,

I will try to keep this short.

I am doing a test case using Zenstruck Browser and then using find method of foundry to read the entity.

Process 1 First I insert the entity, let us say an order item mapped to a cart

I use Foundry find method to see it the order item has been inserted

Next I use assert to determine if the item has been created

Process 2 Then I update the cart quantity for that item . I repeat the find method . However find method brings up the previous quantity , not the updated one.

If I do an assert for individual quantity property of item , the test passes.

IMO, find does not refresh the entity but calling a method does auto refresh on the entity and thus test gets passed. For example item->getQuantity() triggers refresh This happens in Proxy class


private function _autoRefresh(): void
    {
        if (!$this->autoRefresh) {
            return;
        }

        $this->_refresh();
    }

With find or find By refresh is not triggered. If we use doctrine entity manager, it gets us the updated value.

Even though there is no problem in test output, during debugging I see the unrefreshed value using find method.

Is this an issue? Is this resolvable.

nikophil commented 5 days ago

Hi!

could you provide a little bit more code please?

Indeed, using find() a second time may only get the object from doctrine's cache, but as soon as you use a getter, the proxy will refresh the object. Then, I'm not sure to understand where is the problem?

Maybe you can also call $item->_refresh() instead of using find()?

cooldude77 commented 4 days ago

Hi @nikophil ,

You got the problem exactly right. It was a little bit troublesome when I was trying to debug a problem while using a test case. Internally , the Factory methods use find(By) of doctrine and which does the intended behavior as you explained.

I was feeling that since Factory find is a wrapper, it would be good idea to refresh it before querying doctrine. As for now I have resorted to calling entitymanager and then finding the object.