I’m trying to test one of my controllers and have followed the documentation, all works well. However I want to test an action that will connect to an external web service. I don’t actually want to test the web service itself so I’d prefer to mock it.
I’ve looked into how to do that in Symfony and it seems you can replace the service definitions in the container by calling set() on it, replacing the existing ID with a mocked service.
The only issue is that, whilst I can see calling the following:PHP1$client->getContainer()->get('service_id');
in my test function will get the mocked service, once I call $client->request();, the one returned in the controller itself is the actual service, not the mocked one. This seems to suggest that the client is resetting once I make the request.
I understand that the client will reset on the next request, but I don’t see how I can mock services if the client resets on the first request. I feel like I’m missing something because everything I’ve read suggests this should work in Symfony.
I’ve seen other suggestions that say I should write a test-specific kernel, but I want to keep my code in the bundle rather than messing with the underlying part of Symfony.
I’ve also looked into Guzzle mocks, but I don’t see how I can get these mocks into my services if I can’t access them.
You must be logged in to reply to this topic.