PHPUnit: set autoload psr-4 in composer.json

I use the following composer.json configs to use phpunit:

{
  "require-dev": {
    "phpunit/phpunit": "^9"
  },
  "autoload": {
    "psr-4": {
      "App\\": "src"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "Tests\\": "tests"
    }
  }
}

My project files are as follows:

> src
    > Configs
    > DB
    > Helper
         Config.php

> tests
> vendor
composer.json
composer.lock

This error will be thrown as long as "App\\": "src" in composer.json:

Uncaught Error: Class 'App\Helper\Config' not found in

But if I change the src to app (in composer.json) and rename the src directory to app, the problem is solved. That is, the composer file should look like this:

{
  "require-dev": {
    "phpunit/phpunit": "^9"
  },
  "autoload": {
    "psr-4": {
      "App\\": "app" // ** rename src to app **
    }
  },
  "autoload-dev": {
    "psr-4": {
      "Tests\\": "tests"
    }
  }
}

And arrange directories and files as follows:

> app
    > Configs
    > DB
    > Helper
         Config.php

> tests
> vendor
composer.json
composer.lock

What could be the cause of this problem?

Every time you make changes to autoload or autoload-dev in composer.json you need to run composer install for those changes to take effect.

It sounds like you didn’t do that.

1 Like

I used the composer dump-autoload command.

Yes, that should work too. No idea what’s going on then :man_shrugging:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.