I just wanted to share my experience about testing, which was quite a bumpy ride considering that I did not write tests for the longest time. Your question triggered familiar memories.
- Did you write the sort and search code? (No, actual sort and search is by SQL)
- Then why does it need to be tested?
I hope this can help you think about tests in a different way and maybe result in writing code in a different way. From what I can tell, you might be better off testing if search terms are properly "remembered" by your code, likewise for sort.
Going further, after they are remembered correctly, they will be sent off to SQL. I ask the 2 questions again. Who actually constructs the SQL? (It is eloquent), does it need to be tested? This would be a subjective yes and no. If you really want to test it, probably can be done by just extracting the SQL using ->toSql() and checking that your terms are there.
Because of this, you might be better off abstracting/removing $request and view() and move the search/sort terms construction into another (more testable) function
An example can be:
public function addSearchAndSort($query_builder, Array $search, Array $sort)
{
return $query_builder;
}
Hope it helps.