lukeraymonddowning / poser

Create class based model factories in Laravel applications in seconds.
MIT License
277 stars 10 forks source link

Adds support for custom macros #82

Closed lukeraymonddowning closed 4 years ago

lukeraymonddowning commented 4 years ago

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 of UserFactory::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? The Macroable trait would allow a user to add something along the lines of Factory::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 and Factory implement the __call and __callStatic methods, and to my knowledge I can't call the Macroable magic methods from the Factory. 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 of Macroable in the Factory.

lukeraymonddowning commented 4 years ago

@AlanHolmes @andreich1980 do you have any thoughts on this, particularly the concerns?

AlanHolmes commented 4 years ago

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 .

AlanHolmes commented 4 years ago

Oh wow, that is badly formatted when replying via email lol

andreich1980 commented 4 years ago

I know nothing about magic of "call". And I like Alan's idea with custom base factory class that extends poser factory.

lukeraymonddowning commented 4 years ago

Closing this due to lack of necessity.