简体   繁体   中英

PHPUnit doesn't recognize file_exists()

I've set up a project with unit test files in NetBeans. I set bootstrap to C:\\www\\foo\\_tests\\TestAutoload.php and put simple autoload method to this file:

function __autoload( $class_name ) {
    // series of ifs
    if ( ... ) {
        $file_name = ...
    }

    if ( file_exists ( $file_name ) ) {
        require_once( $file_name );
    } else {
        echo "autoload error";
    }
}

All of my tests fail on autoload this way. They always output just "autoload error". If I don't check if file_exists and just use require_once( $file ) no matter what's in $file , it works perfectly.

Anyone encountered anything like this before? It's not something I couldn't resolve by simply not checking whether file exists or not, but I'm interested why it does this and if I can cheat it somehow.

From the PHP Manual page for file_exists :

Be aware: If you pass a relative path to file_exists, it will return false unless the path happens to be relative to the "current PHP dir" (see chdir() ).

如果尚未使用绝对文件名,则可能要尝试使用file_exists() ,例如, file_exists($file_name)可能是file_exists(dirname(__FILE__) . '/../myclasses/' . $file_name) ,因为(根据amphetamachine的回答), file_exists()不使用PHP的include_path设置。

在file_exits函数中给出绝对路径,则可能在该file_exits函数中文件路径不正确

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM