Maciej Sz
Maciej Sz

Reputation: 12035

How to require vendor/autoload.php with behat.yml

In my project I have an autoload.php file which is responsible for requiring context classes. This is the file auto-generated by Composer. How can I include that file using behat.yml?

In PHPUnit's phpunit.xml.dist I can do this simply with bootstrap attribute like this:

<phpunit bootstrap="vendor/autoload.php">

Is there a similar setting in Behat?

Note: I'm not asking about running Behat from vendor directory, because when I do it works as expected. That's because the file vendor/bin/behat contains the

include __DIR__.'/../vendor/autoload.php';

expression. But I also have behat installed globally and when I run global behat command it fails to autoload my context classes. And this is the use case that I'm interested in.

Upvotes: 3

Views: 6223

Answers (2)

Andy Rosslau
Andy Rosslau

Reputation: 131

You could use the following new BehatExtension:

https://github.com/Postcon/BehatBootstrapExtension

Upvotes: 0

Jakub Zalas
Jakub Zalas

Reputation: 36191

Composer's autoloader is used by default.

However, depending on where you've installed Behat a different autoloader will be used. Project's autoloader is used if you installed Behat in your project, while the global autoloader is used if you installed Behat globally.

There's no way of changing this behaviour with configuration alone. Note that this is an expected behaviour. See https://github.com/Behat/Behat/issues/490#issuecomment-40928786

Furthermore, installing Behat in your project is the recommended way.

You could probably write a Behat extension to include project's autoloader even if a global behat version is used, but I don't think it's worth it. It could also lead to odd autoloading issues with duplicate or wrong versions of classes etc.

Finally, you can also configure the autoloader manually: http://docs.behat.org/en/latest/guides/6.profiles.html#custom-autoloading

Upvotes: 3

Related Questions