3

To compliment an existing smorgasbord of arrangements between phpunit,autoload and namespace is this:

I have created a simple test project that runs PhpUnit tests and uses namespace autoloading. I register the autoloading in the bootstrap file like so:

set_include_path(get_include_path() . PATH_SEPARATOR . "/path/to/classes/folder");
spl_autoload_register();

and inside a unit test I load and test my class like so:

$obj = new \some\space\someClass(); // which is in the classes/some/space folder
$this->assertTrue($obj->foo()=='bar');

And I get an error

Fatal error: Class '\some\space\someClass' not found in testSomeClass.php...

2
  • And someClass is defined in classes/some/space/someClass.php with case matching exactly? Commented Jan 18, 2012 at 22:49
  • yes, I actually used my ide's (phpstorm) autocomplete for the most part. Commented Jan 18, 2012 at 22:51

2 Answers 2

7

While this is not phpunit specific you need to change:

spl_autoload_register();

to

spl_autoload_register('spl_autoload');

Any other component that registers an autoloader unregisters the default __autoload().

If your code has an existing __autoload function then this function must be explicitly registered on the __autoload stack. This is because spl_autoload_register() will effectively replace the engine cache for the __autoload function by either spl_autoload() or spl_autoload_call().

So this is how spl-autoload works together with anything else that uses autoloading.

Sign up to request clarification or add additional context in comments.

2 Comments

While your answer does solve the problem, I'm not very clear as to why that is, considering that it worked fine with the first call (without 'spl_autoload' string as a parameter), and the only place it ceased is when I was using it inside phpunit. Thanks
According to the docs for spl_autoload_register() "if no parameter is provided, then the default implementation of spl_autoload() will be registered." No parameter should be equivalent to passing 'spl_autoload' and the above change should have no effect! Awesome. :)
4

Make sure your path to classes folder is made relative to script which is run for tests execution. If the script is in subfolder of your projects (for example tests/) then your path should be ../path/to/classes/folder

That's what I have in my tests/bootstrap.php

set_include_path(dirname(__FILE__).'/../classes'.PATH_SEPARATOR.get_include_path());

spl_autoload_extensions('.php');
spl_autoload_register('spl_autoload');

9 Comments

right, I've tried using absolute paths, with the same results
I've tried various combinations of relative paths as well, still with no luck
does your autoload work when runnng the application normally?
Mchl, have you ever had phpunit working with this sort of class autoloading? I'm asking because an affirmative to this would mean that this is a fault of configuration on my machine, rather than a quirk of phpunit, or other.
How odd - I've changed spl_autoload_register() to spl_autoload_register('spl_autoload') and now it's working
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.