samedi 19 mars 2016

Laravel Unit Tests not finding custom namespace

I have a custom namespace in a Laravel 5 application, defined in my composer.json:

"psr-4": {
        "App\\": "app/",
        "JShaffstall\\CMSAuth\\": "packages/jshaffstall/CMSAuth/src"
    }

I have a User model in app that inherits from CMSUser in my custom namespace:

namespace App;

use JShaffstall\CMSAuth\CMSUser;

class User extends CMSUser
{
}

When running the application that works fine, and the custom classes there are found. My understanding was that phpunit used the same autoloader that the application does, so those classes should be available for tests, too.

But when I run phpunit and a test would create an instance of App\User, I get the following error:

.PHP Fatal error:  Class 'JShaffstall\CMSAuth\CMSUser' not found in /var/www/public/packages/app/User.php on line 8
PHP Fatal error:  Uncaught exception 'Illuminate\Contracts\Container\BindingResolutionException' with message 'Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.' in /var/www/public/packages/vendor/laravel/framework/src/Illuminate/Container/Container.php:752
Stack trace:
#0 /var/www/public/packages/vendor/laravel/framework/src/Illuminate/Container/Container.php(633): Illuminate\Container\Container->build('Illuminate\\Cont...', Array)
#1 /var/www/public/packages/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(687): Illuminate\Container\Container->make('Illuminate\\Cont...', Array)
#2 /var/www/public/packages/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(154): Illuminate\Foundation\Application->make('Illuminate\\Cont...')
#3 /var/www/public/packages/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(79): Illuminate\Foundation\Bootstrap\HandleExceptions->getExceptionHandler()
#4 /var/www/public/packages/vendor/laravel/fram in /var/www/public/packages/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 752

I'd thought it might have something to do with the order of inclusion of the custom namespaces in composer.json, but swapping those did not change anything.

Is there a step I'm missing for unit tests to find custom namespaces?

This seems similar to this question: Laravel Unit Tests cannot find namespaced class but that one is using the classmap rather than PSR4 autoload and doesn't have any answers.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire