Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer require-dev requireing dependencies in different packages require-dev

I have some tests that are namespaced being autoloaded in package A using

"autoload-dev": {
    "psr-4": {
        "Vendor\\PackageA\\PhpUnit\\": "tests/PhpUnit"
    }
},

This works fine.

I have another package, package B which also have namespaced tests that require one of the namespaced tests in package A

"autoload-dev": {
    "psr-4": {
        "Vendor\\PackageB\\PhpUnit\\": "tests/PhpUnit"
    }
},

However, when I try and include the file in Package B, the class is not found

use Vendor\PackageA\PhpUnit\MyTestFromA;

class MyTestFromB extends MyTestFromA
{

Making me think that the autoload-dev stuff from other packages is not being loaded.

PHP Fatal error: Class 'Vendor\PackageA\PhpUnit\MyTestFromA' not found in /full/path/to/PackageBClass.php on line 3

When I try and import a file that is being autoloaded using autoload from package B rather than autoload-dev, I get no error.

How can I overcome this?

Part of me is thinking to make a package just for the tests and have it autoloaded in both without autoload-dev but I want to confirm first.

like image 324
myol Avatar asked Dec 05 '16 15:12

myol


People also ask

What is difference between require and require dev in composer?

require: These are must packages for the code to run. It defines the actual dependency as well as package version. require_dev: It defines the packages necessary for developing the project and not needed in production environment. Note: The require and require_dev are important parameters available in composer.

What is composer require -- dev?

According to composer's manual: require-dev (root-only) Lists packages required for developing this package, or running tests, etc. The dev requirements of the root package are installed by default. Both install or update support the --no-dev option that prevents dev dependencies from being installed.

How do I update composer and all dependencies?

To update dependencies two commands can be used: composer update and composer require . The difference between these two commands is that composer update will try to update a dependency based on the current constraints in composer. json and will only update composer. lock .


1 Answers

Solution: Composer autoload-dev does not work.

Take a look at the docs. It says: "autoload-dev (root only)". root only means it only applies to the root package. As you included the package, the shown composer.json file is not the root package and the autoload-dev section is thus ignored.

like image 154
Grzegorz Gajda Avatar answered Sep 21 '22 10:09

Grzegorz Gajda