Mock API when performing a WebTestCase and do $client->submitform() in Symfony 5

I have a Test which submits a form. This form usually results in performing doing a external API call. I want to mock that because I’m not interested in the API but in the action.

Whenever I call submitForm the client is still making an external call, but I don’t want that.
The test also fails because the api expects a api key which the test does not have.

class SubscriptionControllerTest extends WebTestCase
    public function testSubscribe(): void
        $client = static::createClient();
        $userRepository = static::$container->get(UserRepository::class);
        $testUser       = $userRepository->findOneBy(['email' => UserFixtures::$testUser]);
        $clientMock = $this->createMock(ApiClient::class);
//replace the api with the mock one
        self::$container->set(ApiClient::class, $clientMock);

        $client->request('GET', '/subscription/new');
        $client->submitForm('btn-submit', [
            'subscribe[firstName]' => 'firstname',
            'subscribe[lastName]'  => 'lastname'


What am I doing wrong here?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *