// UserController Test public function testDeleteUser() { $userId = 1; $mockUser = $this->getMockBuilder(User::class)->getMock(); // expect deleteUser() to be called once with $userId $mockUser->expects($this->once()) ->method('deleteUser') ->with($this->equalTo($userId)); $controller = new UserController($mockUser); $controller->delete($userId); }
// LoginController Test public function testAuthenticate() { $email = "test@test.com"; $password = "password"; $mockAuth = $this->getMockBuilder('Illuminate\Auth\AuthManager') ->disableOriginalConstructor() ->getMock(); // expect Auth::attempt() to be called once with $email and $password as parameters $mockAuth->shouldReceive('attempt') ->once() ->with(['email' => $email, 'password' => $password]); $controller = new LoginController($mockAuth); $controller->authenticate($email, $password); }Here, we are using the Laravel Auth package library to authenticate a user. We create a mock of the AuthManager class and expect that the attempt() method is called once with the correct parameters.