【问题标题】:How to use $_SERVER['DOCUMENT_ROOT'] with Phpunit如何在 Phpunit 中使用 $_SERVER['DOCUMENT_ROOT']
【发布时间】:2021-03-11 09:00:29
【问题描述】:

我正在尝试设置 Phpunit,并且在我的整个项目中,$_SERVER['DOCUMENT_ROOT'] 用于链接所有文件/函数/连接等。在我的测试类中,如果我链接我的 global_php_includes.php 文件的绝对路径(其中是包含更多其他内容的父文件)然后每次调用 $_SERVER['DOCUMENT_ROOT'] 时都会出错。

我对 Phpunit 很陌生,有什么办法可以解决这个问题吗?我使用作曲家安装它。这是我的 xml 文件:

<?xml version="1.0" encoding="UTF-8" ?>

<phpunit bootstrap="vendor/autoload.php" colors="true" verbose="true" stopOnFailure="true">

    <testsuites>
        <testsuite name="unit">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

</phpunit>

【问题讨论】:

  • 您遇到了哪个错误?
  • 提到了使用 Document_Root 的所有内容我收到一个错误,指出存在 No such file or directory

标签: php unit-testing phpunit


【解决方案1】:

面对这个问题,你做不到(据我所知)。

如果您通过 CLI 启动 phpunit,它不会被服务器直接使用,而是使用 PHP,因此您必须指定绝对路径(与执行 cron 作业的方式相同)。

我建议你在你的网站上使用 classes 以便更容易使用 PHPUnit,但为你的项目使用绝对路径

【讨论】:

    猜你喜欢
    • 2013-08-12
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2013-06-01
    相关资源
    最近更新 更多