2017 © Pedro Peláez
 

library symfony-dependency-injection-test

Library for testing user classes related to the Symfony Dependency Injection Component

image

matthiasnoback/symfony-dependency-injection-test

Library for testing user classes related to the Symfony Dependency Injection Component

  • Tuesday, March 6, 2018
  • by matthiasnoback
  • Repository
  • 10 Watchers
  • 150 Stars
  • 596,419 Installations
  • PHP
  • 411 Dependents
  • 0 Suggesters
  • 31 Forks
  • 3 Open issues
  • 35 Versions
  • 8 % Grown

The README.md

SymfonyDependencyInjectionTest

By Matthias Noback and contributors, (*1)

Build Status, (*2)

This library contains several PHPUnit test case classes and many semantic assertions which you can use to functionally test your container extensions (or "bundle extensions") and compiler passes. It also provides the tools to functionally test your container extension (or "bundle") configuration by verifying processed values from different types of configuration files., (*3)

Besides verifying their correctness, this library will also help you to adopt a TDD approach when developing these classes., (*4)

Installation

Using Composer:, (*5)

composer require --dev matthiasnoback/symfony-dependency-injection-test

Usage

Testing a container extension

To test your own container extension class MyExtension create a class and extend from Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase. Then implement the getContainerExtensions() method:, (*6)

<?php

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;

class MyExtensionTest extends AbstractExtensionTestCase
{
    protected function getContainerExtensions(): array
    {
        return [
            new MyExtension()
        ];
    }
}

Basically you will be testing your extension's load method, which will look something like this:, (*7)

<?php

class MyExtension extends Extension
{
    public function load(array $config, ContainerBuilder $container)
    {
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__));
        $loader->load('services.xml');

        // maybe process the configuration values in $config, then:

        $container->setParameter('parameter_name', 'some value');
    }
}

So in the test case you should test that after loading the container, the parameter has been set correctly:, (*8)

<?php

class MyExtensionTest extends AbstractExtensionTestCase
{
    /**
     * @test
     */
    public function after_loading_the_correct_parameter_has_been_set()
    {
        $this->load();

        $this->assertContainerBuilderHasParameter('parameter_name', 'some value');
    }
}

To test the effect of different configuration values, use the first argument of load():, (*9)

<?php

class MyExtensionTest extends AbstractExtensionTestCase
{
    /**
     * @test
     */
    public function after_loading_the_correct_parameter_has_been_set()
    {
        $this->load(['my' => ['enabled' => 'false']);

        ...
    }
}

To prevent duplication of required configuration values, you can provide some minimal configuration, by overriding the getMinimalConfiguration() method of the test case., (*10)

Testing a compiler pass

To test a compiler pass, create a test class and extend from Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase. Then implement the registerCompilerPass() method:, (*11)

<?php

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;

class MyCompilerPassTest extends AbstractCompilerPassTestCase
{
    protected function registerCompilerPass(ContainerBuilder $container): void
    {
        $container->addCompilerPass(new MyCompilerPass());
    }
}

In each test you can first set up the ContainerBuilder instance properly, depending on what your compiler pass is expected to do. For instance you can add some definitions with specific tags you will collect. Then after the "arrange" phase of your test, you need to "act", by calling the compile()method. Finally you may enter the "assert" stage and you should verify the correct behavior of the compiler pass by making assertions about the ContainerBuilder instance., (*12)

<?php

class MyCompilerPassTest extends AbstractCompilerPassTestCase
{
    /**
     * @test
     */
    public function if_compiler_pass_collects_services_by_adding_method_calls_these_will_exist()
    {
        $collectingService = new Definition();
        $this->setDefinition('collecting_service_id', $collectingService);

        $collectedService = new Definition();
        $collectedService->addTag('collect_with_method_calls');
        $this->setDefinition('collected_service', $collectedService);

        $this->compile();

        $this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
            'collecting_service_id',
            'add',
            [
                new Reference('collected_service')
            ]
        );
    }
}

Standard test for unobtrusiveness

The AbstractCompilerPassTestCase class always executes one specific test - compilation_should_not_fail_with_empty_container() - which makes sure that the compiler pass is unobtrusive. For example, when your compiler pass assumes the existence of a service, an exception will be thrown, and this test will fail:, (*13)

<?php

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class MyCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $definition = $container->getDefinition('some_service_id');

        ...
    }
}

So you need to always add one or more guard clauses inside the process() method:, (*14)

<?php

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class MyCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        if (!$container->hasDefinition('some_service_id')) {
            return;
        }

        $definition = $container->getDefinition('some_service_id');

        ...
    }
}

Use findDefinition() instead of getDefinition()

You may not know in advance if a service id stands for a service definition, or for an alias. So instead of hasDefinition() and getDefinition() you may consider using has() and findDefinition(). These methods recognize both aliases and definitions., (*15)

Test different configuration file formats

The Symfony DependencyInjection component supports many different types of configuration loaders: Yaml, XML, and PHP files, but also closures. When you create a Configuration class for your bundle, you need to make sure that each of these formats is supported. Special attention needs to be given to XML files., (*16)

In order to verify that any type of configuration file will be correctly loaded by your bundle, you must install the SymfonyConfigTest library and create a test class that extends from AbstractExtensionConfigurationTestCase:, (*17)

<?php

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionConfigurationTestCase;

class ConfigurationTest extends AbstractExtensionConfigurationTestCase
{
    protected function getContainerExtension()
    {
        return new TwigExtension();
    }

    protected function getConfiguration()
    {
        return new Configuration();
    }
}

Now inside each test method you can use the assertProcessedConfigurationEquals($expectedConfiguration, $sources) method to verify that after loading the given sources the processed configuration equals the expected array of values:, (*18)

# in Fixtures/config.yml
twig:
    extensions: ['twig.extension.foo']
<!-- in Fixtures/config.xml -->
<container>
    <twig:config>
        <twig:extension>twig.extension.bar</twig:extension>
    </twig:config>
</container>
<?php
...

class ConfigurationTest extends AbstractExtensionConfigurationTestCase
{
    ...

    /**
     * @test
     */
    public function it_converts_extension_elements_to_extensions()
    {
        $expectedConfiguration = [
            'extensions' => ['twig.extension.foo', 'twig.extension.bar']
        ];

        $sources = [
            __DIR__ . '/Fixtures/config.yml',
            __DIR__ . '/Fixtures/config.xml',
        ];

        $this->assertProcessedConfigurationEquals($expectedConfiguration, $sources);
    }
}

List of assertions

These are the available semantic assertions for each of the test cases shown above:, (*19)

assertContainerBuilderHasService($serviceId)
Assert that the ContainerBuilder for this test has a service definition with the given id.
assertContainerBuilderHasService($serviceId, $expectedClass)
Assert that the ContainerBuilder for this test has a service definition with the given id and class.
assertContainerBuilderNotHasService($serviceId)
Assert that the ContainerBuilder for this test does not have a service definition with the given id.
assertContainerBuilderHasSyntheticService($serviceId)
Assert that the ContainerBuilder for this test has a synthetic service with the given id.
assertContainerBuilderHasAlias($aliasId)
Assert that the ContainerBuilder for this test has an alias.
assertContainerBuilderHasAlias($aliasId, $expectedServiceId)
Assert that the ContainerBuilder for this test has an alias and that it is an alias for the given service id.
assertContainerBuilderHasParameter($parameterName)
Assert that the ContainerBuilder for this test has a parameter.
assertContainerBuilderHasParameter($parameterName, $expectedParameterValue)
Assert that the ContainerBuilder for this test has a parameter and that its value is the given value.
assertContainerBuilderHasExactParameter($parameterName)
Assert that the ContainerBuilder for this test has a parameter.
assertContainerBuilderHasExactParameter($parameterName, $expectedParameterValue)
Assert that the ContainerBuilder for this test has a parameter and that its value is the given value, as well as its type matches given value type.
assertContainerBuilderHasServiceDefinitionWithArgument($serviceId, $argumentIndex)
Assert that the ContainerBuilder for this test has a service definition with the given id, which has an argument at the given index.
assertContainerBuilderHasServiceDefinitionWithArgument($serviceId, $argumentIndex, $expectedValue)
Assert that the ContainerBuilder for this test has a service definition with the given id, which has an argument at the given index, and its value is the given value.
assertContainerBuilderHasServiceDefinitionWithServiceLocatorArgument($serviceId, $argumentIndex, $expectedValue)
Assert that the ContainerBuilder for this test has a service definition with the given id, which has an argument at the given index, and its value is a ServiceLocator with a reference-map equal to the given value.
assertContainerBuilderHasServiceDefinitionWithMethodCall($serviceId, $method, array $arguments = [], $index = null)
Assert that the ContainerBuilder for this test has a service definition with the given id, which has a method call to the given method with the given arguments. If index is provided, invocation index order of method call is asserted as well.
assertContainerBuilderHasServiceDefinitionWithTag($serviceId, $tag, array $attributes = [])
Assert that the ContainerBuilder for this test has a service definition with the given id, which has the given tag with the given arguments.
assertContainerBuilderHasServiceDefinitionWithParent($serviceId, $parentServiceId)
Assert that the ContainerBuilder for this test has a service definition with the given id which is a decorated service and it has the given parent service.
assertContainerBuilderHasServiceLocator($serviceId, $expectedServiceMap)
Assert that the ContainerBuilder for this test has a ServiceLocator service definition with the given id.

Available methods to set up container

In all test cases shown above, you have access to some methods to set up the container:, (*20)

setDefinition($serviceId, $definition)
Set a definition. The second parameter is a Definition class
registerService($serviceId, $class)
A shortcut for setDefinition. It returns a Definition object that can be modified if necessary.
setParameter($parameterId, $parameterValue)
Set a parameter.

Version Guidance

Version Released PHPUnit Status
6.x Aug 8, 2024 10.5 and 11.x Latest
5.x Nov 22, 2023 9.6 and 10.x Bugfixes
4.x Mar 28, 2019 8.x and 9.x Bugfixes
3.x Mar 5, 2018 7.x Bugfixes
2.x May 9, 2017 6.x Bugfixes
1.x Jul 4, 2016 4.x and 5.x EOL

The Versions

06/03 2018

dev-master

9999999-dev http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

06/03 2018

dev-analysis-z4DABG

dev-analysis-z4DABG http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

06/03 2018

v3.0.0

3.0.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

06/03 2018

v2.x-dev

2.9999999.9999999.9999999-dev http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

06/03 2018

v2.3.1

2.3.1.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

05/03 2018

dev-patch-2

dev-patch-2 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

05/03 2018

dev-patch-1

dev-patch-1 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

03/03 2018

dev-patch-3.x

dev-patch-3.x http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

04/02 2018

dev-analysis-q1o5Pw

dev-analysis-q1o5Pw http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

30/11 2017
30/11 2017

v2.3.0

2.3.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

30/10 2017

dev-feature/symfony-4

dev-feature/symfony-4 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

17/10 2017

v2.2.0

2.2.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

20/03 2017

v2.1.0

2.1.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

08/02 2017

dev-ng

dev-ng http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

08/02 2017

v2.0.0

2.0.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

30/11 2016
19/10 2016
12/11 2015

v0.7.6

0.7.6.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

29/10 2015

v0.7.5

0.7.5.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

20/06 2015

v0.7.4

0.7.4.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit dependency injection symfony2

19/04 2015

v0.7.3

0.7.3.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

phpunit dependency injection symfony2

18/04 2015

v0.7.2

0.7.2.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

phpunit dependency injection symfony2

07/07 2014

v0.7.1

0.7.1.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

phpunit dependency injection symfony2

24/06 2014

v0.7.0

0.7.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

phpunit dependency injection symfony2

28/04 2014

v0.6.0

0.6.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

phpunit dependency injection symfony2

10/04 2014

v0.5.0

0.5.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

phpunit dependency injection symfony2

26/02 2014

v0.4.0

0.4.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

phpunit dependency injection symfony2

16/11 2013

v0.3.0

0.3.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Development Requires

phpunit dependency injection symfony2

13/11 2013

v0.2.1

0.2.1.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Development Requires

phpunit dependency injection symfony2

13/11 2013

v0.2.0

0.2.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Development Requires

phpunit dependency injection symfony2

20/10 2013

v0.1.1

0.1.1.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

phpunit dependency injection symfony2

04/09 2013

v0.1.0

0.1.0.0 http://github.com/matthiasnoback/SymfonyDependencyInjectionTest

Library for testing user classes related to the Symfony Dependency Injection Component

  Sources   Download

MIT

The Requires

 

phpunit dependency injection symfony2