nyholm/symfony-bundle-test

3.0.0 2024-01-08 10:18 UTC

README

Total Downloads

测试您的bundle是否与不同的Symfony版本兼容

当您想确保您的bundle与不同的Symfony版本兼容时,您需要创建一个自定义的TestKernel,并加载您的bundle和配置。

使用此bundle测试与Matthias Nobacks的SymfonyDependencyInjectionTest结合,将为测试一个Symfony bundle提供一个良好的基础。

支持

当前支持的PHP和Symfony版本。

在报告问题之前,请始终尝试更新到此包的最新版本。

安装

通过Composer

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

编写测试

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Nyholm\BundleTest\TestKernel;
use Acme\AcmeFooBundle;
use Acme\Service\Foo;
use Symfony\Component\HttpKernel\KernelInterface;

class BundleInitializationTest extends KernelTestCase
{
    protected static function getKernelClass(): string
    {
        return TestKernel::class;
    }

    protected static function createKernel(array $options = []): KernelInterface
    {
        /**
         * @var TestKernel $kernel
         */
        $kernel = parent::createKernel($options);
        $kernel->addTestBundle(AcmeFooBundle::class);
        $kernel->handleOptions($options);

        return $kernel;
    }

    public function testInitBundle(): void
    {
        // 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 your services exists
        $this->assertTrue($container->has('acme.foo'));
        $service = $container->get('acme.foo');
        $this->assertInstanceOf(Foo::class, $service);
    }

    public function testBundleWithDifferentConfiguration(): void
    {
        // Boot the kernel with a config closure, the handleOptions call in createKernel is important for that to work
        $kernel = self::bootKernel(['config' => static function(TestKernel $kernel){
            // Add some other bundles we depend on
            $kernel->addTestBundle(OtherBundle::class);

            // Add some configuration
            $kernel->addTestConfig(__DIR__.'/config.yml');
        }]);

        // ...
    }
}

配置Github Actions

您希望"Github actions"针对每个目前支持的LTS版本的Symfony(因为每个主要版本只有一个),以及如果它不是LTS版本的话,还有当前版本。没有必要测试中间版本,因为Symfony遵循语义化版本控制

在.github/workflows目录中创建一个文件

#.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-22.04', 'windows-2022' ]
        php: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
        symfony: ['5.4.*', '6.4.*', '7.0.*']
        exclude:
        - php: '7.4'
          symfony: '6.4.*'
        - php: '8.0'
          symfony: '6.4.*'
        - php: '7.4'
          symfony: '7.0.*'
        - php: '8.0'
          symfony: '7.0.*'
        - php: '8.1'
          symfony: '7.0.*'

    steps:
      - uses: actions/checkout@v4

      - 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@v2

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