【问题标题】:How to use PHPUnit extensions with Symfony's PHPUnit Bridge?如何在 Symfony 的 PHPUnit Bridge 中使用 PHPUnit 扩展?
【发布时间】:2020-03-19 10:17:15
【问题描述】:

我在我的 Symfony 项目中使用PHPUnit Bridge。我目前正在使用 PHPUnit 7,我想升级到 PHPUnit 8。

在 PHPUnit 8 array subset assertions are deprecated 中并生成警告。我想使用dms/phpunit-arraysubset-asserts package 来提供它们。使用常规的 PHPUnit,我只需 composer require 就可以了。

现在,Bridge 没有原始的 PHPUnit 作为其依赖项,而是将其安装到一个临时文件夹,对其进行修补并从那里运行。 phpunit-arraysubset-asserts 具有 PHPUnit 依赖项,但会生成警告:

Adding phpunit/phpunit as a dependency is discouraged in favor of Symfony's PHPUnit Bridge. 


  * Instead:
    1. Remove it now: composer remove --dev phpunit/phpunit
    2. Use Symfony's bridge: composer require --dev phpunit

我不想安装phpunit/phpunit 以避免混淆。

我尝试了 ignore it by adding a * replacement,但只是将替换添加到 composer.json 会产生 Composer 错误:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - __root__ No version set (parsed as 1.0.0) conflicts with symfony/phpunit-bridge[v5.0.5].
    - symfony/phpunit-bridge v5.0.5 conflicts with __root__[No version set (parsed as 1.0.0)].
    - Installation request for __root__ No version set (parsed as 1.0.0) -> satisfiable by __root__[No version set (parsed as 1.0.0)].
    - Installation request for symfony/phpunit-bridge v5.0.5 -> satisfiable by symfony/phpunit-bridge[v5.0.5].

使用 PHPUnit Bridge 时添加 PHPUnit 扩展的正确方法是什么?

【问题讨论】:

  • 你可以看看这个,github.com/symfony/symfony/issues/30071,不知道有没有好的解决办法。
  • @TejasGosai 这是 Symfony 自己的测试套件中的一个随机问题。与我的问题无关。
  • 对不起...我认为它与 phpunit 的版本类似。我的道歉.. :)
  • 版本升级不是问题本身,我的问题是关于使用 Bridge 安装扩展。
  • @gronostaj 遇到同样的问题,你找到解决办法了吗?

标签: php symfony phpunit simple-phpunit


【解决方案1】:

警告 我不太了解 phpunit-bridge(实际上今天是我使用它的第一天),但我可以看到实现您想要的方法。这不是那么方便,但可能。我认为最好与 symfony 团队核实(也许有一种支持的方式)或者只使用普通的 phpunit(这似乎更容易,应该可以将它与 symfony 的 phpunit-bridge 集成,而不需要在桥上中继来安装 phpunit 本身)

# create new symfony project
symfony new gronostaj-phpunit
# cd in it
cd gronostaj-phpunit
# install phpunit-bridge
symfony composer req phpunit-bridge
# to change phpunit version to 9.5
vim phpunit.xml.dist
# install phpunit in symfony bridge way
bin/phpunit
# to update phpunit config
bin/phpunit --migrate-configuration
# black magic begins here
cd bin/.phpunit/phpunit-9.5-0
# to bypass `autoload-dev` in later dump-autoload
mkdir -p tests/_files ; touch tests/_files/CoverageNamespacedFunctionTest.php tests/_files/CoveredFunction.php tests/_files/NamespaceCoveredFunction.php
# install phpunit plugin
composer require --no-plugins dms/phpunit-arraysubset-asserts
# cd back to the root of project
cd -
# create test
--- phpunit/gronostaj-phpunit ‹master* M?› » cat tests/SubsetTest.php 
<?php

declare(strict_types=1);

namespace App\Tests;

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use DMS\PHPUnitExtensions\ArraySubset\Assert;
use PHPUnit\Framework\TestCase;

class SubsetTest extends TestCase
{
    use ArraySubsetAsserts;

    public function testWithTrait(): void
    {
        $expectedSubset = ['bar' => 0];
        $content = ['bar' => 0];

        self::assertArraySubset($expectedSubset, $content, true);
    }

    public function testWithDirectCall(): void
    {
        $expectedSubset = ['bar' => 0];
        $content = ['bar' => 0];

        Assert::assertArraySubset($expectedSubset, $content, true);
    }
}
# run test
--- phpunit/gronostaj-phpunit ‹master* M?› » bin/phpunit
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

Testing 
..                                                                  2 / 2 (100%)

Time: 00:00.006, Memory: 8.00 MB

OK (2 tests, 2 assertions)
# done

对于 phpunit 8,在 phpunit.xml.dist 中使用 phpunit 版本 8.0 并像 composer require --no-plugins 'dms/phpunit-arraysubset-asserts:*' 这样安装你的插件,这样就可以了。

为了让团队中的其他人可以重现它,您需要提供作曲家脚本(创建虚拟文件和安装插件,但它应该可以解决问题)并将它们连接到 post-installpost-update 事件中。

【讨论】:

    【解决方案2】:

    我过去也一直在努力解决这个问题,并在PHPUnit Bridge documentation 中找到了这个提示:

    也可能需要额外的软件包 使用 SYMFONY_PHPUNIT_REQUIRE 环境变量。这对于 安装 PHPUnit 插件而无需将它们添加到您的主 composer.json 文件。

    所以基本上,编辑phpunit.xml.dist 并在现有的&lt;php&gt; 标签下声明环境变量:

    <php>
        <server name="SYMFONY_PHPUNIT_REQUIRE" value="dms/phpunit-arraysubset-asserts" />
    </php>
    

    要添加多个包,请使用空格分隔符:

    <php>
        <server name="SYMFONY_PHPUNIT_REQUIRE" value="dms/phpunit-arraysubset-asserts spatie/phpunit-snapshot-assertions" />
    </php>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多