2017 © Pedro Peláez
 

library symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

image

ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  • Tuesday, July 24, 2018
  • by ramunasd
  • Repository
  • 3 Watchers
  • 14 Stars
  • 9,436 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 10 Versions
  • 26 % Grown

The README.md

Symfony Container Mocks

Build Status Scrutinizer Code Quality PHP-Eye Coverage Status, (*1)

This container enables you to mock services in the Symfony dependency injection container. It is particularly useful in functional tests., (*2)

Features

  • Can replace any Symfony service or parameter
  • Automatically detects service class from service definition
  • Can be used with any mocking framework
  • Compatible with Symfony versions 2.7 - 3.4
  • Works on all supported version of PHP

OTB supported mocking frameworks

Installation

Add SymfonyContainerMocks using composer:, (*3)

composer require "ramunasd/symfony-container-mocks", (*4)

or edit your composer.json:, (*5)

{
    "require": {
        "ramunasd/symfony-container-mocks": "*"
    }
}

Replace base container class for test environment in app/AppKernel.php, (*6)

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use RDV\SymfonyContainerMocks\DependencyInjection\TestKernelTrait;

class AppKernel extends Kernel
{
    // use special container when env=test
    use TestKernelTrait;

    public function registerBundles()
    {
        return [
            // bundles
        ];
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
    }
}

And clear application cache., (*7)

Examples

Inject mocked service

<?php

namespace Acme\Bundle\AcmeBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Acme\Bundle\AcmeBundle\Service\Custom;

class AcmeControllerTest extends WebTestCase
{
    /**
     * @var \Symfony\Bundle\FrameworkBundle\Client $client
     */
    private $client;

    public function setUp()
    {
        parent::setUp();

        $this->client = static::createClient();
    }

    public function tearDown()
    {
        $this->client->getContainer()->tearDown();
        $this->client = null;

        parent::tearDown();
    }

    public function testSomethingWithMockedService()
    {
        $this->client->getContainer()->prophesize('acme.service.custom', Custom::class)
            ->someMethod([])
            ->willReturn(false)
            ->shouldBeCalledTimes(2);

        // ...
    }
}

Inject automatically mocked service

feature works only with flag "debug" enabled., (*8)

$mock = $this->client->getContainer()->prophesize('acme.service.custom');
$mock
    ->myMethod()
    ->willReturn(true);

Other mocking frameworks

// create stub
$mock = $this->getMock(Custom::class);

// inject service mock
self::$kernel->getContainer()->setMock('acme.service.custom', $mock);

// reset container state
self::$kernel->getContainer()->unMock('acme.service.custom');

Set specific framework parameter

// set custom value during test
self::$kernel->getContainer()->setMockedParameter('acme.service.parameter1', 'customValue1');

// trigger service, assert results

// reset all parameters to original values
self::$kernel->getContainer()->clearMockedParameters();

Things TO DO

  • Symfony 4.x support
  • PSR-11 adoption
  • https://github.com/jakzal/phpunit-injector - inject Symfony services into PHPUnit test cases

The Versions

24/07 2018

dev-master

9999999-dev https://github.com/ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  Sources   Download

MIT

The Requires

 

The Development Requires

tdd test phpunit prophecy container symfony

24/07 2018

dev-coveralls

dev-coveralls https://github.com/ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  Sources   Download

MIT

The Requires

 

The Development Requires

tdd test phpunit prophecy container symfony

13/06 2018

0.6

0.6.0.0 https://github.com/ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  Sources   Download

MIT

The Requires

 

The Development Requires

tdd test phpunit prophecy container symfony

10/03 2018

0.5

0.5.0.0 https://github.com/ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  Sources   Download

MIT

The Requires

 

The Development Requires

tdd test phpunit prophecy container symfony

30/11 2017

0.4.1

0.4.1.0 https://github.com/ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  Sources   Download

MIT

The Requires

 

The Development Requires

tdd test phpunit prophecy container symfony

14/08 2017

0.4

0.4.0.0 https://github.com/ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  Sources   Download

MIT

The Requires

 

The Development Requires

tdd test phpunit prophecy container symfony

26/04 2017

dev-symfony-3.3

dev-symfony-3.3 https://github.com/ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  Sources   Download

MIT

The Requires

 

The Development Requires

tdd test phpunit prophecy container symfony

14/07 2016

0.3

0.3.0.0 https://github.com/ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  Sources   Download

MIT

The Requires

 

The Development Requires

tdd test phpunit prophecy container symfony

05/05 2016

0.2

0.2.0.0 https://github.com/ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  Sources   Download

MIT

The Requires

 

The Development Requires

tdd test phpunit prophecy container symfony

26/02 2016

0.1

0.1.0.0 https://github.com/ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

  Sources   Download

MIT

The Requires

 

The Development Requires

tdd test phpunit prophecy container symfony