josebailon
3 weeks ago
229
4
Testing

Laravel 8 - Factory namespace when developing package

Posted 3 weeks ago by josebailon

I'm learning how to develop packages for Laravel. When I try to use Factory on tests it fails because it tries to use a Factory with a fully qualified class name made by Database\Factories+modelnamespace+classname.

Error: Class 'Database\Factories\Myvendor\Mypackage\Models\PostFactory' not found

My model "Post" has a fully qualified class name as:

Myvendor\Mypackage\Models\Post

My factory has a fully qualified class name as any factory class when it would be made by artisan:

Database\Factories\PostFactory

When I try to use in a Test Post::factory()->make(); it fails because it can not find the Factory class:

Database\Factories\Myvendor\Mypackage\Models\PostFactory

How should I manage Factories classes in package developing? I thought that a Model using a the use HasFactory; trait would search for his factory in namespace Database\\Factories\\Classname

What am I missing?

I am developing the package using "orchestra/testbench": "^6.0"

My folder structure in the package is:

Package
	composer.json
	database
		factories
			PostFactory.php
	tests
		Feature
		Unit
	src
		Models
			Post.php

In composer.json y have an entry as:


    "autoload": {
        "psr-4": {
            "Myvendor\\Mypackage\\":"src/"
        }
    },
   "autoload-dev": {
        "psr-4": {
            "Myvendor\\Mypackage\\Tests\\":"tests/",
            "Database\\Factories\\":"database/factories/"
        }

Please sign in or create an account to participate in this conversation.