1.7.0 2020-12-01 08:37 UTC

This package is auto-updated.

Last update: 2021-05-29 01:10:46 UTC


Latest Version Build Status Total Downloads

Test if your bundle is compatible with different Symfony versions

When you want to make sure that your bundle works with different versions of Symfony you need to create a custom AppKernel and load your bundle and configuration.

Using this bundle test together with Matthias Nobacks's SymfonyDependencyInjectionTest will give you a good base for testing a Symfony bundle.


Via Composer

$ composer require --dev nyholm/symfony-bundle-test

Write a test

use Nyholm\BundleTest\BaseBundleTestCase;
use Acme\AcmeFooBundle;
use Acme\Service\Foo;

class BundleInitializationTest extends BaseBundleTestCase
    protected function getBundleClass()
        return AcmeFooBundle::class;

    public function testInitBundle()
        // Boot the kernel.

        // Get the container
        $container = $this->getContainer();

        // Test if you services exists
        $service = $container->get('acme.foo');
        $this->assertInstanceOf(Foo::class, $service);

    public function testBundleWithDifferentConfiguration()
        // Create a new Kernel
        $kernel = $this->createKernel();

        // Add some configuration

        // Add some other bundles we depend on

        // Boot the kernel as normal ...

        // ...

Private services in Symfony 4

In Symfony 4 services are private by default. This is a good thing, but in order to test them properly we need to make them public when we are running the tests. This can easily be done with a compiler pass.

class BundleInitializationTest extends BaseBundleTestCase
    protected function setUp()

        // Make all services public
        $this->addCompilerPass(new PublicServicePass());

        // Make services public that have an idea that matches a regex
        $this->addCompilerPass(new PublicServicePass('|my_bundle.*|'));

    // ...

Known Issue

Be aware that if you make all services public then you will not get any errors if your bundles access this services even if they are private by nature.

Configure Travis

You want Travis to run against each currently supported LTS version of Symfony (since there would be only one per major version), plus the current if it's not an LTS too. There is no need for testing against version in between because Symfony follows Semantic Versioning.

language: php
sudo: false
        - $HOME/.composer/cache/files
        - $HOME/symfony-bridge/.phpunit

        - PHPUNIT_FLAGS=""
        - SYMFONY_PHPUNIT_DIR="$HOME/symfony-bridge/.phpunit"

    fast_finish: true
          # Minimum supported dependencies with the latest and oldest PHP version
        - php: 7.2
          env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"
        - php: 5.5
          env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"

          # Test the latest stable release
        - php: 5.5
        - php: 5.6
        - php: 7.0
        - php: 7.1
        - php: 7.2
          env: COVERAGE=true PHPUNIT_FLAGS="-v --testsuite main --coverage-text --coverage-clover=build/coverage.xml"

        - php: 7.1
          env: DEPENDENCIES="dunglas/symfony-lock:^2"
        - php: 7.1
          env: DEPENDENCIES="dunglas/symfony-lock:^3"
        - php: 7.1
          env: DEPENDENCIES="dunglas/symfony-lock:^4"

          # Latest commit to master
        - php: 7.2
          env: STABILITY="dev"

          # Dev-master is allowed to fail.
        - env: STABILITY="dev"

    - if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
    - if ! [ -z "$STABILITY" ]; then composer config minimum-stability ${STABILITY}; fi;
    - composer require --no-update symfony/phpunit-bridge:^4.0 ${DEPENDENCIES}

    # To be removed when this issue will be resolved: https://github.com/composer/composer/issues/5355
    - if [[ "$COMPOSER_FLAGS" == *"--prefer-lowest"* ]]; then composer update --prefer-dist --no-interaction --prefer-stable --quiet; fi
    - composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction
    - ./vendor/bin/simple-phpunit install

    - composer validate --strict --no-check-lock
    - ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS