Even after implementing the repository template to abstract the data access level (Eloquent ORM) when you do something like:
$students = StudentRepository::all(); return view('students.index', ['students' => $students]);
You end up sending a subclass of \Eloquent\Model or Eloquent\Collection to the view .
This means that if your view tries to perform lazy loading and you run tests on the CI server, this may lead to a crash due to the lack of a database connection.
This problem leads to the following solutions:
- You mock the
Eloquent subclasses directly in your tests so that when you try to lazy load you can set the return value - You implement interfaces for each model and bind them using laravel IoC .
The disadvantages for these two options are:
For option 1: If you finish mocking Eloquent in your tests, then what's the point of implementing a repository template (which seems to be very popular because of its “flexibility” if you need to change from Eloquent to another ORM), since you have to rewrite your tests too.
For option 2: If you are writing interfaces for each Eloquent\Model , you only need to write additional code for setters and getters.
If so far I'm still on the right track, option 2 would be a better choice , but there is absolutely no thread or information about abstracting Eloquent\Model in interfaces to make fun of the calls made by the view, which are either lazy-loaded or just properties .
So why is that? Am I not seeing something? perhaps tests should exit before rendering the view (and $this->assertViewHas($variable) pointless)? maybe there is a way to ignore crashes when this happens in a view? maybe only people unit test JSON API controllers?
source share