I'm trying to test a command where there's a method I want to mock because it makes a call to an external service using Guzzle, but no matter what I try, I can't seem to mock it successfully. It always calls the original method. Any ideas? This is my command (relevant code):
class CommandToTest extends Command
{
//constants, setup handle and other stuff
//other methods
protected function methodToMock()
{
//method with the call to external web service that should be mocked
}
//other methods
}
Test:
class TestForCommand extends TestCase
{
//constants etc
public function testCommandToTest()
{
$commandMock = Mockery::mock(CommandToTest::class)->makePartial();
$commandMock->shouldAllowMockingProtectedMethods()->shouldReceive('methodToMock')
->andReturn([
'Test01' => 'T01',
'Test02' => 'T02',
'Test03' => 'T03'
]);
$this->app->instance(CommandToTest::class, $commandMock);
//other stuff
$this->artisan('tools:commandtotest', [//params])
->expectsConfirmation(//params ok)
->assertOk();
}
//assertions
}
But no matter what I try, the real method is always called. How can I make this work? Should I approach it from another angle? Thanks.