I'm looking for advice regarding unit testing and the database.
I know testing best practice is a very opinionated topic which different developer communities disagree on, but I wanted to know if the Laravel community has a preferred approach to unit testing.
My Laravel app is dependent on Eloquent and there are references to Eloquent persistence methods and model relations everywhere.
I want to unit test functionality in my application that isn't related to database persistence, and to my knowledge, unit testing by definition means testing in isolation (i.e. without talking to the database / network).
However, I am struggling at the moment because the classes I want to test depend on Eloquent models and specifically, relations, which are retrieved from the database through PHP's magic accessor (__get__).
Typically when I need to test a class in isolation of its dependencies, I will mock the dependencies using a "dumb" placeholder object which fulfils the dependency contract while ensuring external factors cannot affect the outcome of the test. It seems a lot of unnecessary work to mock Eloquent models though.
There are a few solutions I have considered:
- Give up on trying to "unit" test and just accept my app requires a database connection (could use an in memory SQLite database for example)
- Abstract the persistence logic via a repository (which could be mocked more easily)
- Mock the Eloquent models (not my favourite solution as I've got better things to do)
I'm assuming others have encountered this problem before, so what would you recommend?