PHPUnit doesn't allow me to include files

梦想的初衷 提交于 2019-12-12 18:39:43

问题


PHPUnit is giving me an error message whenever I try to include a file. For example the follwing code gives me the error:

<?php

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

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

?>

But if I remove the fourth line (require_once "controller/ProductController.php";), it runs fine.

The error I get is:

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

EDIT: My include_path in the php.ini file is:

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

What is strange:

<?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/**

And also:

<?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.**

This is very strange to me.

Any ideas?


回答1:


Its a know issue.

Verify your PHUnit installed below packeges:

pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear install phpunit/PHP_CodeCoverage

try sudo if you don't have permissions.

Some links which helps you :

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

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

phpunit require_once() error




回答2:


Ok, it took me days to figure this one out. I hope nobody will ever go through what I went.

I had in my php.ini:

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

But because I used to not use multiple php include paths in my ini file, I had used

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

in some of the files, even if they weren't the ones that I was trying to include.

Anyway, it might sound silly and confusing, but if you find yourself in a situation where PHPUnit does not load properly for some reason, think of the possibility of your code changing the include path somehow.




回答3:


I had a similar issue, PHPUnit could not locate files in parent directories. It only located files being children to the current directory of the class importing them, e,g.

file structure:

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 has problems finding the relative path when trying to go to parent folders. After reading this answer. The solution was to insert dirname(dirname(__FILE__)), dirname(__FILE__) which returns the path of the current directory, by applying dirname to the path, it returns the parent path.

index.php

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


来源:https://stackoverflow.com/questions/15193816/phpunit-doesnt-allow-me-to-include-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!