The following is a simplified class of the controller I am working on:
class UserController extends Controller {
protected $user;
public function __construct(User $user) {
$this->user = user;
}
public function contact(ContactFormRequest $request) {
$user = $this->user
->where('email', $request->email)
->with(['orders' => function($q) {
$q->orderBy('created_at', 'desc')
->first();
}])
->first();
return view('contact', ['user' => $user]);
}
}
This is the test case I have thus far:
class ExampleTest extends TestCase {
public function setUp () {
$this->mockUser = Mockery::mock(Model::class, User::class);
$this->app->instance('User', $this->mockUser);
}
public function testContactForm() {
$this->userMock
->shouldReceive('where')
->once()
->with('email', 'fake@email.com')
->andReturn(new Collection(new User, new User));
$this->visit('contact-form');
}
}
I'll admit I am writing this from memory instead of having the existing code in front of me. But this test at work passes PHPUnit. The problem is that the visit throws an error and I have additional ->see('something')
test I need to make.
I need the rest of the mock that gives a full and usable user mock to the controller and view.
I expect it looks something like (this is where the code goes awry)
$this->userMock
->shouldReceive('where')
->once()
->with('email', 'fake@email.com')
->andReturn(new Collection(new User))
->shouldReceive('with')
->once();
->with(['orders' => function($q) {
$q->orderBy('created_at', 'desc')
->first();
}])
->andReturn( **something** )
->shouldReceive('first')
->once()
->andReturn(new Collection(new User));