Is there a good way to utilize configure() function when you need to conditionally add items to a factory in an afterCreating hook #39195
Unanswered
roni-estein
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
This was a hacky solution but it worked, in a way that wouldn't interfere with parallel testing. It's terrible and there must be a better solution...
I was given a factory that was a bit messed up and slow because it was always utilizing $this->faker->image which causes a download. However, for testing, images are only required for a tiny fraction of tests, so it should be isolated in a state, rather than being the default action.
However, I was unable to find a way to get the state to manipulate any class variables since the object is regenerated. Instead i added some static class variables and a naive hash to determine if the configure should fire. It's horrible but it gets the job done.
I ideally I was hoping for the new instance to clone the private/protected variables on the class which we could manipulate with public facing methods to avoid this dance. Is there a better solution? If there isn't, I could try to put together a PR if it would be welcome or happily assist someone else who wanted to run with this.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions