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
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',
],
]);
// ...
}
}
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.*|')
],
]);
}
// ...
}
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.
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