__autoload not respected when testing with PHPUnit

Viewed 4426

How can I make PHPUnit respect __autoload functions?

For example, I have these three files:

loader.php

function __autoload($name)
{
    echo "foo\n";
    require_once("$name.php");
}

test.php

require_once("loader.php");

class FooTest extends PHPUnit_Framework_TestCase
{
    function testFoo()
    {
        new Foo();
    }
}

foo.php

require_once("loader.php");
new Foo();

As expected php foo.php errors out, saying that file "Foo.php" doesn't exist. The testFoo() function however errors out by saying that there is no such class as Foo, and never echos the "foo\n" line.

3 Answers

PHPUnit uses spl_autoload_register which turns off __autoload.

Related