ivan1986/mockery-service-bundle

Integrate mockery for test symfony services

v0.1.1 2018-06-13 14:40 UTC

README

What ?

A symfony bundle that eases creation of mock services.

Using DIC tags, you can automatically replace a service with either a mock, a stub or a fake.

This package os copy of https://github.com/docteurklein/TestDoubleBundle for mockery

Why ?

To improve isolation of tests and increase the precision and variation of test fixtures.

Usually, our behat suite is using real data, coming from database fixtures. This forces us to create gobal, universal, works-for-all fixtures.

A real database also implies to reset the state before each scenario. This process is slow, and is just a workaround for having broken isolation.

An ideal test suite would run each scenario using only in-memory repositories. Each scenario should define how the SUS behaves given a specific context. Having a global implicit context (the database fixtures) makes it really hard to test different cases.

One solution is to replace your repositories with stubs. Each scenario configures only the stubs required for it to work.

Note: Stubbed data is not resilient across processes, and thus doesn't fit for end-to-end testing like a typical mink+behat suite.

But now that repositories are doubled, how do you know if your real repositories still work? Well, that's the role of infrastructure tests. Only those run against a real backend, be it a database for repositories, or a server for an http client.

To access the real services, just use <original-id>.real.

By doing that, you theoretically have a good coverage, isolation, speed and you can better catch the origin of a regression.

How ?

install

composer require ivan1986/mockery-service-bundle --dev

register the bundle


    public function registerBundles()
    {
        // ...
        if ($this->getEnvironment() === 'test') {
            $bundles[] = new Ivan1986\MockeryServiceBundle\MockeryServiceBundle();
        }
        // ...
    }

Note: You might want to add this bundle only in test env.

Types of mocks

Create by Mockery::mock(class):

    tags:
        - name: mocked.service

Create stub:

    tags:
        - name: mocked.service
          stub: ClassName

Create fake:

    tags:
        - name: mocked.service
          fake: serviceId