Developed with love by KnpLabs Hire us for your project!
15

MockeryServiceBundle

by ivan1986

A symfony bundle that eases creation of test doubles.

MockeryServiceBundle

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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2018 Ivan Borzenkov <ivan.borzenkov@gmail.com>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
  • Add homepage
    By ivan1986, 6 months ago
  • add LICENSE
    By ivan1986, 6 months ago
  • first version - clone of TestDoubleBundle
    By ivan1986, 6 months ago
  • init commit
    By ivan1986, 6 months ago