Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

paulbarker's avatar

Composer Autoloading Issue With Test Files and Autoload-Dev

The other day, after implementing some tests for our application, I got a deploy failure with this error:

Mon Feb 27 16:48:31 PST 2017
From bitbucket.org:********/********
 * branch            production -> FETCH_HEAD
Already up-to-date.
Loading composer repositories with package information
Installing dependencies from lock file
Nothing to install or update
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postInstall
> php artisan optimize
Generating optimized class loader
The compiled class file has been removed.
PHP Fatal error:  Class 'PHPUnit_Framework_TestCase' not found in /home/forge/********/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php on line 11

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /home/forge/********/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php on line 11

The only thing I changed in my composer.json file was from this:

"autoload-dev": {
    "classmap": [
        "tests/TestCase.php"
    ]
},

to this

"autoload-dev": {
    "classmap": [
        "tests/TestCase.php"
    ],
    "files": [
        "tests/Traits/MailTracking.php",
        "tests/Integration/API/ApiTester.php",
        "tests/helpers/functions.php"
    ]

},

Everything works fine locally, but when Forge tries to deploy from our production branch, it fails. It seems like it's trying to autoload phpunit, but phpunit is in require-dev—it's not even installed in production.

Why is it looking for PHPUnit_Framework_TestCase? And why does it only break when Forge tries to deploy? I've tried running the exact same deploy scripts locally, but I can't reproduce the error. Yet it fails if those lines are in composer.json when I push.

0 likes
1 reply
golonix's avatar

On production, you probably install your packages with --no-dev flag (which is understandable) and after packages have being installed, post install scripts are executed (defined in your composer.json file). The problem might be caused because optimize command is run there by default and it doesn't know in which context it is run against. So there is a problem with no dev packages installed and autoloading tries to include files from dev packages.

You can write your own scripts that will react properly within given development environment and include them in your composer.json. Take a look here: https://getcomposer.org/doc/articles/scripts.md

Here is quick explanation about optimize command: php artisan optimize is a wrapper aroundcomposer dump-autoload command. optimize command is not able to pass any additional flags to composer dump-autoload execution. It either updates autoload files (php artisan optimize --psr) or updates and optimizes autoload files (php artisan optimize). Right now, there is no way to pass --no-dev flag that is supported by composer.

Please or to participate in this conversation.