views:

76

answers:

3

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.

+2  A: 

You might want to try using file_exists() on absolute filenames if they aren't, already, e.g. file_exists($file_name) might be file_exists(dirname(__FILE__) . '/../myclasses/' . $file_name), since (as per amphetamachine's answer), file_exists() does not use the include_path setting of PHP.

pinkgothic
You've pointed me the right direction. The problem was that I used $_SERVER[ "DOCUMENT_ROOT" ] in autoload, which was an empty string as PHPUnit is running through CGI, therefore resulting into relative path being passed into file_exists. Now I'm parsing my own "document root" and it works perfectly!
Ondrej Slinták
+3  A: 

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() ).

amphetamachine
+1  A: 

give the absolute path in the file_exits function, it might have the file path not correct in that file_exits function what u have worte

sam