Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3.3 service mocks for functional tests

Before Symfony 3.3 it was allowed to set a mocked service onto the container. Now with 3.3 a deprecation warning is thrown because the service is already pre-defined.

What is the new standard way to overwrite an existing or pre-defined service in the container to set a mocked service for a functional test?

E.g. in our case we set a new entity manager with a new mocked connection pointing to a cloned database for testing.

$container->set('doctrine.orm.entity_manager', $testEm);

Setting the "doctrine.orm.entity_manager" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.

like image 335
Uncaught Exception Avatar asked Jun 02 '17 07:06

Uncaught Exception


People also ask

What are Symfony tests?

These tests ensure that individual units of source code (e.g. a single class) behave as intended. These tests test a combination of classes and commonly interact with Symfony's service container. These tests do not yet cover the fully working application, those are called Application tests .

What is the use of request() method in Symfony?

The request () method also returns a crawler, which you can use to create more complex assertions in your tests: You can learn more about the crawler in The DOM Crawler. The test client simulates an HTTP client like a browser and makes requests into your Symfony application:

What is kerneltestcase in Symfony flex?

This assures that each test is run independently from each other. To run your application tests, the KernelTestCase class needs to find the application kernel to initialize. The kernel class is usually defined in the KERNEL_CLASS environment variable (included in the default .env.test file provided by Symfony Flex):

How to simulate logging in in Symfony functional tests?

Reproducing the actual steps - such as submitting a login form - makes a test very slow. For this reason, Symfony provides a loginUser () method to simulate logging in in your functional tests. Instead of logging in with real users, it's recommended to create a user only for tests.


1 Answers

I had the very same problem just a few days ago and I wrote a library to trick Symfony's DIC: https://github.com/TiMESPLiNTER/proxy-mock

The idea is to override the service in the config_test.yml with a "proxy" from the original service class which redirects all the calls to a mock which then can be set dynamically in the test case.

# config_test.yml
services:
    timesplinter.proxy_mock.factory:
        class: timesplinter\ProxyMock\ProxyMockFactory

    acme.api.client:
        factory: 'timesplinter.proxy_mock.factory:create'
        arguments: ['Acme\AppBunde\Services\ApiClient']

This will override the service defined in the original service.(xml|yml) with a proxy of it.

In the test case you can then do something like this:

// Your container aware test case (this exmaple is for PHPUnit)
$mock = $this->getMockBuilder(ApiClient::class)->disableOriginalConstructor()->getMock();

$container->set('acme.api.client')->setMock($mock);

With this your test will run against the mock you provide using the setMock() method.

The library is very new so some feature may be missing. If you use it and miss something please provide a pull request with the desired feature.

like image 61
TiMESPLiNTER Avatar answered Oct 20 '22 19:10

TiMESPLiNTER