【问题标题】:PHPUnit doesn't allow me to include filesPHPUnit 不允许我包含文件
【发布时间】:2019-06-18 16:52:21
【问题描述】:

每当我尝试包含文件时,PHPUnit 都会给我一条错误消息。例如下面的代码给了我错误:

<?php

include_once "PHPUnit/Autoload.php";
require_once "controller/ProductController.php";

class SecurityTests extends PHPUnit_Framework_TestCase
{
    public function testSmth() {
        $this->assertTrue(1==1);
    }
}

?>

但如果我删除第四行 (require_once "controller/ProductController.php";),它运行良好。

我得到的错误是:

Warning: require_once(PHPUnit/Framework/Error.php): failed to open stream: No such file or directory in E:\wamp\bin\php\php5.4.3\pear\PHPUnit\Util\ErrorHandler.php on line 48

编辑: 我在 php.ini 文件中的 include_path 是:

include_path = ".;E:\wamp\bin\php\php5.4.3\pear\;E:\wamp\www\renting\"

有什么奇怪的:

<?php
echo get_include_path(); **// Will echo .;E:\wamp\bin\php\php5.4.3\pear\;E:\wamp\www\renting\** 
require_once 'PHPUnit/Autoload.php';
require_once 'controller/AccountController.php';
echo get_include_path(); **// Will echo E:/wamp/www/renting/**

还有:

<?php

require_once 'controller/AccountController.php';
echo get_include_path(); **// Will echo E:/wamp/www/renting/**
require_once 'PHPUnit/Autoload.php';

echo get_include_path(); **// Will not even echo.**

这对我来说很奇怪。

有什么想法吗?

【问题讨论】:

  • 你的目录结构是什么样的,你从哪里运行测试?
  • 我正在使用 PhpStorm 运行测试,但是如果我使用命令行从测试文件夹运行它,我会得到相同的结果。
  • 如果我删除包含的文件,我不会收到任何错误,测试运行正常。

标签: phpunit


【解决方案1】:

我遇到了类似的问题,PHPUnit 无法在父目录中找到文件。它只是 定位的文件是导入它们的类的当前目录的子目录,例如

文件结构:

var/
    www/
        /api
            index.php
            conf.php
        /config
           dbconfig.php

index.php

require_once 'conf.php'; #works fine
require_once '../config/dbconfig.php; #ERROR

PHPUnit 在尝试转到父文件夹时无法找到相对路径。 看完this answer.后解决办法是插入dirname(dirname(__FILE__))dirname(__FILE__)返回当前目录的路径,通过在路径上应用dirname,返回父路径。

index.php

require_once 'conf.php'; #works fine
require_once dirname(dirname(__FILE__)) . '../config/dbconfig.php'; #works fine.

【讨论】:

    【解决方案2】:

    这是一个已知问题。

    验证您安装在包下的 PHUnit:

    pear 频道-发现 pear.phpunit.de
    梨频道发现 components.ez.no
    梨安装 phpunit/PHP_CodeCoverage

    如果您没有权限,请尝试 sudo

    一些对你有帮助的链接:

    https://bugs.launchpad.net/ubuntu/+source/phpunit/+bug/701544

    fatal error 'File/Iterator/Autoload.php' not found when running phpunit

    phpunit require_once() error

    【讨论】:

    • 好吧,看来我的文件实际上并没有丢失,也没有什么可供我下载的。问题是它没有设法包含它需要的文件,即使它们在那里。我不明白。
    【解决方案3】:

    好吧,我花了好几天才弄清楚这一点。我希望没有人会经历我所经历的。

    我的 php.ini 中有:

    include_path = ".;E:\wamp\bin\php\php5.4.3\pear\;E:\wamp\www\renting\"
    

    但是因为我以前没有在我的ini文件中使用多个php包含路径,所以我使用了

    set_include_path("E:/wamp/www/renting/");
    

    在某些文件中,即使它们不是我试图包含的文件。

    无论如何,这可能听起来很愚蠢和令人困惑,但是如果您发现自己处于 PHPUnit 由于某种原因无法正确加载的情况,请考虑您的代码以某种方式更改包含路径的可能性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-02
      • 2019-01-09
      • 2020-05-20
      • 2014-11-10
      • 2019-04-08
      • 1970-01-01
      • 2015-09-14
      • 2021-01-29
      相关资源
      最近更新 更多