Closed lukeraymonddowning closed 4 years ago
@AlanHolmes @andreich1980 do you have any thoughts on this, particularly the concerns?
I've not yet made something macroable, but I have recently used them for the first time in tests, and it didnt seem to have IDE completion (tho that might have been me doing it wrong).
Another option that would allow for them to have custom methods, is them just creating their own base factory class that extends the poser one, which will def keep IDE completion.
A way that could make this easier for them when using the make command would be to allow for the stub to be customised like you can in the latest version of laravel.
Havent had chance to check your code out yet, as just quickly replying on my phone, but thought the above was an alternative that wont have the issues you mentioned about the magic methods
Regards Alan Holmes
On Sun, 3 May 2020, 14:19 Luke Downing, notifications@github.com wrote:
@AlanHolmes https://github.com/AlanHolmes @andreich1980 https://github.com/andreich1980 do you have any thoughts on this, particularly the concerns?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lukeraymonddowning/poser/pull/82#issuecomment-623109124, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABAXFMW7UEBXY7IOVDUFIR3RPVVNRANCNFSM4MYD6XEQ .
Oh wow, that is badly formatted when replying via email lol
I know nothing about magic of "call". And I like Alan's idea with custom base factory class that extends poser factory.
Closing this due to lack of necessity.
This PR adds the
Macroable
trait to the base Poser Factory class, allowing the user to define extension macro functions in a service provider (or even in the setup function of a test for smaller use cases).As an example of a use case, the other day an issue came through where somebody was trying to use the syntax
UserFactory::items(10)->create()
instead ofUserFactory::times(10)->create()
, which obviously threw an error.Whilst I prefer the
times
syntax, who am I to prevent users from being able to use the syntax they or their team prefer? TheMacroable
trait would allow a user to add something along the lines ofFactory::macro('items', fn($count) => static::times($count))
to their service provider and instantly gain access to the new syntax in all of their factories.I'm sure you can imagine many other use cases.
Concerns
My only concern with this is the fact that both
Macroable
andFactory
implement the__call
and__callStatic
methods, and to my knowledge I can't call theMacroable
magic methods from theFactory
. Please let me know if this is in fact possible and I can update it.My workaround is to basically reimplement the
__call
and__callStatic
magic method logic ofMacroable
in the Factory.