3

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

在 PHPUnit 8中,数组子集断言已被弃用并生成警告。我想使用dms/phpunit-arraysubset-asserts 包来提供它们。使用常规的 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以避免混淆。

我试图通过添加替换来忽略它*,但只是添加替换会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 扩展的正确方法是什么?

4

2 回答 2

0

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

也可以使用 SYMFONY_PHPUNIT_REQUIREenv 变量要求将附加的包与其他所需的 PHPUnit 包一起安装。这对于安装 PHPUnit 插件特别有用,而无需将它们添加到您的主 composer.json文件中。

所以基本上,phpunit.xml.dist在现有<php>标签下编辑和声明 env 变量:

<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>
于 2021-09-23T08:11:20.507 回答
0

警告我不太了解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 verion 8.0phpunit.xml.dist并像这样安装你的插件composer require --no-plugins 'dms/phpunit-arraysubset-asserts:*',这样就可以了。

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

于 2020-12-08T06:05:43.917 回答