I am performing functional tests with PHPUnit in a Symfony 4.3 project. All my test classes extend from TestCase.
I am having problems to stub the result of a method that makes a call to an external service. I don't want to check that this service works in my project functional tests, so I do the following:
public function testPutEndpoint()
{
$stub = $this->createMock(ExternalRepository::class);
$stub->method('put')->willReturn(['data'=> ['data']]);
{
list($responseCode, $content) = $this->makeCurl(
//Here a curl to the endpoint of my project is done
);
$this->assertEquals(200, $responseCode);
}
Here I see how the code continues going throw the real method ignoring the sub.
So my question is, how can I stub a method that is inside the logic to test, but I cannot call it directly in the test class?
Also, the constructor of the endpoint receives the Repository as an injection:
protected $externalRepository;
public function __construct(ExternalRepository $externalRepository)
{
$this->externalRepository = $externalRepository;
$this->commandBus = $commandBus;
}