使用 Symfony 的 PHPUnit Bridge 时如何在 Behat 中使用 PHPUnit?

How to use PHPUnit in Behat when using Symfony's PHPUnit Bridge?

在我的 Behat 上下文中,我已经包含了 PHPUnit 的断言,如 the manual:

中所述
require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';

不过,我现在使用的是 Symfony 的 PHPUnit bridge and so I've removed my explicitly-installed PHPUnit and am using the one brought in by the bridge. I needed to do this to avoid the "multiple versions" error described here

但是现在,我不能在 behat 中使用 PHPUnit!

PHP Warning:  Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Warning: require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory'

如何引用PHPUnit桥引入的PHPUnit实例?

事实证明,我需要通过它们的路径在 vendor 中请求相关文件。对我来说,那是:

require_once(__DIR__.'/../../vendor/symfony/phpunit-bridge/bin/.phpunit/phpunit-5.7/vendor/autoload.php');
require_once(__DIR__.'/../../vendor/symfony/phpunit-bridge/bin/.phpunit/phpunit-5.7/src/Framework/Assert/Functions.php');

对于 Symfony 4,这足以让它为我工作:

require_once(__DIR__ . '/../../vendor/bin/.phpunit/phpunit-5.7/vendor/autoload.php');

然而,这并不是一个很好的解决方案,因为如果您使用 PHP7.2,PHPUnit Bridge 将安装 PHPUnit 6.3 而不是 5.7。

我尝试按如下方式更改 composer.json:

"autoload-dev": {
    "psr-4": {
        "App\Tests\": "tests/"
    },
    "classmap": [
        "vendor/bin/.phpunit"
    ]
},

这确实有效,但会破坏全新安装,因为该文件夹在您 运行 simple-phpunit.

之前不存在

我怀疑这可以通过使用 composer 脚本来解决 运行 simple-phpunit,但我对 post-install-cmdpre-autoload-dump 的简短实验让我一无所获。