I want to test my Laravel Artisan command. So I need to mock an object and stubs this mocked object methods. In my test, I cannot use the real SFTP environment.
This is the handle() of my command:
public function handle()
{
$sftp = new SFTP('my.sftpenv.com');
$sftp->login('foo', 'bar');
}
I want to mock the SFTP in my test:
$sftp = $this->createMock(SFTP::class);
$sftp->expects($this->any())->method('login')->with('foo', 'bar');
$this->artisan('import:foo');
Running the test results in a Cannot connect to ...:22, which comes from the original login method of SFTP. So the mock/stub does not take effect.
So my question is: how can I mock an object in a Laravel Artisan command test?