【问题标题】:How to use PHPUnit in Behat when using Symfony's PHPUnit Bridge?使用 Symfony 的 PHPUnit Bridge 时如何在 Behat 中使用 PHPUnit?
【发布时间】:2017-07-28 11:07:47
【问题描述】:

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

require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';

但是,我现在使用的是 Symfony 的 PHPUnit bridge,所以我已经删除了我明确安装的 PHPUnit,并使用了网桥引入的那个。我需要这样做以避免here 描述的“多个版本”错误。

但是现在,我不能使用 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 实例?

【问题讨论】:

    标签: php symfony phpunit behat


    【解决方案1】:

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

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

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

    我尝试将 composer.json 更改如下:

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

    这确实有效,但会中断全新安装,因为在您运行 simple-phpunit 之前该文件夹不存在。

    我怀疑这可以使用作曲家脚本运行 simple-phpunit 来解决,但我对 post-install-cmdpre-autoload-dump 的简短实验让我无处可去。

    【讨论】:

      【解决方案2】:

      事实证明,我需要通过路径来要求供应商中的相关文件。对我来说,那是:

      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');
      

      【讨论】:

      • @lauda 恐怕这两天你不能接受自己对问题的回答。但我打算这样做。
      • 您可能还想在上游报告此情况。由于这与 Behat 相关,它可能会产生一些吸引力(或已经有了更好的解决方案)。
      • 有趣的想法,@hakre。不过,我不太确定我会在哪里报告。
      猜你喜欢
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      相关资源
      最近更新 更多