adaba
adaba

Reputation: 384

PHPUnit trait not found after updating composer.json

Original composer.json

"autoload": {
    "psr-4": { "": "src/" },
    "classmap": [ "app/AppKernel.php", "app/AppCache.php" ],
    "exclude-from-classmap": [ "/tests/" ]
},
"autoload-dev": {
    "psr-4": { "tests\\": "tests/" }
}

New composer.json

"autoload": {
    "psr-4": {
        "AppBundle\\": "src/AppBundle",
        "TOTO\\": "src/TOTO"
    },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php" ],
    "exclude-from-classmap": [ "/tests/" ]
},
"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    },
    "files": [
        "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
    ]
},

My trait is defined in tests\TOTO\Services\InvokeMethodTrait and is called by other tests such as tests\TOTO\Services\ConversationServiceTest, since that change for optimizing the autoloader I get Fatal error: Trait 'tests\TOTO\Services\InvokeMethodTrait' not found in /var/www/symfony/tests/TOTO/Services/ConversationServiceTest.php on line 17. It was working properly before and I didn't change anything in the related classes.

Any idea?

Upvotes: 0

Views: 1015

Answers (1)

xmike
xmike

Reputation: 1038

Original part of composer.json had:

"psr-4": { "tests\\": "tests/" }

and the new one:

"psr-4": {
    "Tests\\": "tests/"
}

There can be seen a change in case for tests namespace. That is not an issue for PHP (natively) since namespaces and classes are case-insensitive (great answer with summary on PHP case sensitivity https://stackoverflow.com/a/33273959/5264262). But composer's autoloader receives that unknown class name as string and resolves all that fully qualified class names to some paths to require a file, so the process is case sensitive. There was some discussion on the opened issue at https://github.com/composer/composer/issues/2767 concerning the topic.

The final point is: our namespaces and class naming in composer.json should be case-sensitive compliant.

Upvotes: 1

Related Questions