使用 PHPUnit Bridge 测试可重用的 Symfony 包

Testing Reusable Symfony Bundles with PHPUnit Bridge

我最近 运行 跨越 PHPUnit Bridge,并且一直在我的任何独立 Symfony 应用程序中使用它。但是,我注意到一些弃用通知是针对我们维护的可重用捆绑包依赖项发出的。

为了诊断,我打开了可重复使用的捆绑项目并安装了 symfony/phpunit-bridge,但是在 运行 phpunit 注意到没有弃用通知等正在为项目输出.

那么您如何使用带有可重复使用包的 symfony/phpunit-bridge 包?

I noticed some deprecation notices coming through for a reusable bundle dependency that we maintain.

To diagnose, I opened up the reusable bundle project and installed symfony/phpunit-bridge, but after running phpunit noticed that there were no deprecation notices, etc, being output for the project.

相同的代码并不总是触发相同的警告这一事实可能表明测试是不同的。

如果这来自 PHP 代码,您可以使用 PHP_CodeCoverage 查看已测试和未测试的代码。当您使用 PHPUnit 时,您可以添加一个选项以生成代码覆盖率报告,例如phpunit … --coverage-html cov/ 将在 cov/ 目录中生成一份 HTML 报告。通过比较输出,您可以查看从 Symfony 启动的测试是否调用与从 Bundle 启动的测试相同的代码。

如果测试不同,可以set up a Symfony environment for your bundle:

Create a TestKernel

<?php
// Tests/Controller/App/AppKernel.php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // Dependencies
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            [...]
            // My Bundle to test
            new Beberlei\WorkflowBundle\BeberleiWorkflowBundle(),
        );

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        // We don't need that Environment stuff, just one config
        $loader->load(__DIR__.'/config.yml');
    }
}

Creating the config.yml

# Tests/Controller/App/config.yml
framework:
    secret:          secret
    charset:         UTF-8
    test: ~
    router:          { resource: "%kernel.root_dir%/routing.yml" }
    form:            true
    csrf_protection: true
    validation:      { enable_annotations: true }
    templating:      { engines: ['twig'] }
    session:
        auto_start:     false
        storage_id: session.storage.filesystem

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: error
            handler:      nested
        nested:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug

Creating the routing.yml

# Tests/Controller/App/routing.yml
BeberleiWorkflowBundle:
    resource: "@BeberleiWorkflowBundle/Controller/"
    type:     annotation
    prefix:   /

Modifying the phpunit.xml.dist

<!-- phpunit.xml.dist -->
<phpunit bootstrap="Tests/bootstrap.php">
    <php>
        <server name="KERNEL_DIR" value="Tests/Controller/App" />
    </php>
</phpunit>

然后,您可以在composer.json中需要的包中添加"symfony/symfony": "~2.3"并安装包。未来的测试将启动此 AppKernel 并在完整的 Symfony 环境中执行测试。