trunkstar / symfony-bundle-test
1.8.3
2021-12-02 16:22 UTC
Requires
- php: ^7.2 || ^8.0
- symfony/dependency-injection: ^4.3 || ^5.0 || ^6.0
- symfony/framework-bundle: ^4.3 || ^5.0 || ^6.0
- symfony/http-kernel: ^4.3 || ^5.0 || ^6.0
- symfony/yaml: ^4.3 || ^5.0 || ^6.0
Requires (Dev)
- phpunit/phpunit: ^8.5 || ^9.4
README
测试您的包是否与不同的Symfony版本兼容
当您想确保您的包与不同的Symfony版本兼容时,您需要创建一个自定义的 TestKernel
并加载您的包和配置。
使用此包与Matthias Nobacks的 SymfonyDependencyInjectionTest 结合使用,将为您测试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版本(因为每个主要版本只有一个),以及当前版本(如果不是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-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