Lukashoff
Lukashoff

Reputation: 71

PHPUnit autoloader error: Class 'Yii' not found

I am trying to start PHPUnit testing.

I am using composer to load PHPUnit 4.5 , Yii 1.1.14 and some custom Yii packages that we have built. Inside those custom packages, we autoload some files that set some aliases using the Yii class.

When running our application, we include the base Yii file manually, and then run the composer generated autoloads.

The trouble is, when we run PHPUnit.. the composer autoloads get run first. Even when specifying a bootstrap file with the include:

bin/phpunit --bootstrap carcass/phpunit.bootstrap.php  

Leading to the following Exception:

Fatal error: Class 'Yii' not found 

In fact it appears the autoloads are run even before the -- options are parsed:

bin/phpunit --help 

results in the same error. Removing the autoloads allows PHPunit to run.

Is there any way around this?

I tried placing an autoload for the Yii base file in our main composer.json, but the sub-packages' autoloads get run first.. same error.

I also tried placing an autoload for the Yii base file in each of the sub-packages.. but then we get redeclaration errors as composer uses require. I'm also not a massive fan of this option as it rigidly defines where the Yii definition comes from to sub-packages that don't really need to know.

Upvotes: 2

Views: 3511

Answers (1)

Lukashoff
Lukashoff

Reputation: 71

As the autoload classmap section is run first before all the files sections (including those from the sub-packages).

Placing the yii and YiiBase files in the classmap of the main composer.json for our project solved this issue:

"autoload": {  
    "classmap": [  
        "composer_packages/yiisoft/yii/framework/YiiBase.php",  
        "composer_packages/yiisoft/yii/framework/yii.php" 
    ], 
    "files": [ 
        ... 
    ] 
}

Upvotes: 5

Related Questions