8000 GitHub - SymfonyTest/symfony-bundle-test at bc6c33ef3d2342f89c65e3a85ca153aa0f7801fc
[go: up one dir, main page]

Skip to content

SymfonyTest/symfony-bundle-test

Repository files navigation

Symfony Bundle Test

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.

Install

Via Composer

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

Write a test

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Nyholm\BundleTest\AppKernel;
use Acme\AcmeFooBundle;
use Acme\Service\Foo;

class BundleInitializationTest extends KernelTestCase
{
    protected static function createKernel(array $options = [])
    {
        KernelTestCase::$class = AppKernel::class;

        /**
         * @var AppKernel $kernel
         */
        $kernel = parent::createKernel($options);
        $kernel->addBundle(AcmeFooBundle::class);
        $kernel->handleOptions($options);

        return $kernel;
    }

    public function testInitBundle()
    {
        // Boot the kernel.
        $kernel = self::bootKernel();

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

        // Or for FrameworkBundle@^5.3.6 to access private services without the PublicCompilerPass
        // $container = self::getContainer();

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

    public function testBundleWithDifferentConfiguration()
    {
        // Boot the kernel as normal ...
        $kernel = self::bootKernel([
            'bundles' => [
                // Add some other bundles we depend on
                OtherBundle::class,
            ],
            'configFiles' => [
                // Add some configuration
                __DIR__.'/config.yml',
            ],
        ]);

        // ...
    }
}

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.

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Nyholm\BundleTest\AppKernel;
use Nyholm\BundleTest\CompilerPass\PublicServicePass;
use Acme\AcmeFooBundle;

class BundleInitializationTest extends KernelTestCase
{
    protected static function createKernel(array $options = [])
    {
        KernelTestCase::$class = AppKernel::class;

        /**
         * @var AppKernel $kernel
         */
        $kernel = parent::createKernel($options);
        $kernel->addBundle(AcmeFooBundle::class);
        $kernel->handleOptions($options);

        return $kernel;
    }

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

        self::bootKernel([
            'compilerPasses' => [
                // Make all services public
                new PublicServicePass(),

                // Make services public that have an idea that matches a regex
                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 Github Actions

You want "Github actions" 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.

Create a file in .github/workflows directory:

#.github/workflows/php.yml
name: My bundle test

on:
  push: ~
  pull_request: ~

jobs:
  build:
    runs-on: ${{ matrix.operating-system }}
    name: PHP ${{ matrix.php }} and Symfony ${{ matrix.symfony }}
    strategy:
      matrix:
        operating-system: [ ubuntu-latest, windows-latest ]
        php: [ '7.4', '8.0' ]
        symfony: ['4.4', '5.3']

    steps:
      - uses: actions/checkout@master

      - name: Setup PHP ${{ matrix.php }}
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          tools: flex

      - name: Download dependencies
        env:
          SYMFONY_REQUIRE: ${{ matrix.symfony }}
        uses: ramsey/composer-install@v1

      - name: Run test suite on PHP ${{ matrix.php }} and Symfony ${{ matrix.symfony }}
        run: ./vendor/bin/phpunit

About

Smoke test your Symfony bundle

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 25

Languages

0