Composer is not responsible for loading autoload.php
, but the whatever framework you are using is. PHPUnit, in your case.
PHPUnit only loads vendor/autoload.php
because that file is bootstrapped in phpunit.xml
configuration.
Much easier than doing any weird injection during composer run is simply to create your own testing bootstrap file.
If you check phpunit.xml
you'll find a bootstrap declaration which by default loads vendor/autoload.php
:
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
Create a new bootstrap file (e.g. testing_bootstrap.php
) and there include whatever files you need in addition to vendor/autoload.php
:
<?php
// testing_bootstrap.php
require 'path/to/wordpress/wp-load.php';
require 'vendor/autoload.php`;
And modify your phpunit.xml
file so it uses this file to bootstrap your tests.
bootstrap="testing_bootstrap.php"
This is cleaner and more maintainable, and accomplishes the right result. Which files are loaded/bootstrapped before execution is not composer's job.
To accomplish the same during a regular Laravel run, you'll need to modify Laravel's entry point file, you'll find there that autoload is required there:
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
require __DIR__.'/../vendor/autoload.php';
If you want to load a different file before autoload, simply add the appropriate require
or include
statement before that point.
E.g.:
require 'path/to/wordpress/wp-load.php';
require __DIR__.'/../vendor/autoload.php';
Using the files
key of composer will simply not work. These files are loaded on vendor/composer/autoload_files.php
file, and this in turn is loaded on vendor/composer/autoload_real.php::getLoader
, after the rest of the autoloading process setup has been performed.