How to encapsulate dynamic test flows in Laravel?
The use of model factories to perform the testing process in Laravel is really amazing. However, in large applications, problems arise.
Let's follow an example, let's suppose we have a model entity-relationship like this:
- We have a user that can belong to N Companies that in turn can have N Areas that in turn can have N Responsibilities that can be assigned to users.
See what I'm getting at?
There are many Model Factories to initialize with different possible states in many tests and possibly repeated indefinitely as the application grows.
For this I have found several solutions that do not convince me:
- Encapsulate model packages that represent concrete functionalities in helper functions: This solution may end up making the tests look better, but in my opinion, it is like sweeping the problem under the carpet. In the end, you will have to write functions and functions and functions and functions in one or multiple helpers files which makes the tests more coupled.
- Use Traits to encapsulate groups of related models: It can be a more robust solution because in the traits you can place not only the Model factories but also functions that support that mutate those properties of the model factories. However what happens when I need two groups of similar models but with small changes in states ? nothing this solution may fall short in the medium term.
I am thinking about creating "profiles", which are classes with the strategy pattern that help me to build different groups of model factories. However, it is too much work (more than I can afford right now).
There is some package or design pattern that I am overlooking. I mean, Laravel makes me write beautiful code. I want my tests to be beautiful too.
Please or to participate in this conversation.