mandark
mandark

Reputation: 782

How to access a Symfony service that is used by a client request in a web test case?

I have an application that relies on third party services. In order to make sure that the application works properly, I want to mock the third party services and make sure that the application is working as expected.

This requires that I am able to configure the mock services before creating the requests. However, I am unable to do so.

Consider the following code:

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
//..

class MyTest extends WebTestCase
{

    public function testSignupLink()
    {
        $container = static::createClient()->getContainer();

        // This returns a different instance from the one used by the client request
        $service = $container->get('third-party-service');
        $service->setErrorState(MockService::SOME_ERROR_STATE);
        // ...

        // The request creates a new instance of the $service internally which doesn't have the error state that was set above
        $client->request('POST', '/abc/1');
    }
}

The 'abc' controller relies on a service that I can't access. When I access the service from the container, I get a different instance from the one that is used by the client request.

Is there any way to handle this?

Upvotes: 1

Views: 133

Answers (1)

Alexandru Olaru
Alexandru Olaru

Reputation: 7092

If I correctly understood you, here is what you need: https://github.com/PolishSymfonyCommunity/SymfonyMockerContainer

Upvotes: 2

Related Questions