Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Class 'Database\Factories\Entities\Units\UnitFactory' not found #39

Closed
nafiesl opened this issue Apr 20, 2021 · 2 comments
Closed

Comments

@nafiesl
Copy link
Owner

nafiesl commented Apr 20, 2021

Description

Got error Class 'Database\Factories\Entities\Units\UnitFactory' not found when running unit tests after generating CRUD with a model in a different directory (other than app/Models).

Package Version

  • laravel/framework: v8.37.0
  • luthfi/simple-crud-generator: 2.3.0

How to Reproduce

  1. $ laravel new simple_crud_test
  2. $ cd simple_crud_test
  3. Update phpunit.xml using sqlite in memory database
    <server name="DB_CONNECTION" value="sqlite"/>
    <server name="DB_DATABASE" value=":memory:"/>
    
  4. $ composer require laravel/ui
  5. $ composer require luthfi/simple-crud-generator --dev
  6. $ php artisan ui bootstrap --auth
  7. $ php artisan make:crud Phone (Phone model on the app/Models directory)
  8. $ vendor/bin/phpunit (all tests are passed)
  9. $ php artisan make:crud Entities/Units/Unit (Unit model on the app/Entities/Units/Unit directory)
  10. $ vendor/bin/phpunit --stop-on-failure
  11. Get Error: Class 'Database\Factories\Entities\Units\UnitFactory' not found

Screenshot

screen_2021-04-20_008

@nafiesl
Copy link
Owner Author

nafiesl commented Apr 20, 2021

After done some research on the laravel/framework codebase, I found this line.
https://github.com/laravel/framework/blob/8.x/src/Illuminate/Database/Eloquent/Factories/Factory.php#L718-L720

Since Laravel 8.x, they are using class-based model factories, they need to automatically resolve the model factory class name based on the model class name.

If the file Factory.php#L718-L720 they are checking if the class namespace is App\Models, the model factory class should be in database/factories/ModelNameFactory.

But when the Laravel model is stored outside the "app/Models", they will resolve the model factory class name based on the model's full namespace.

In my case, if my model path is app/Entities/Units/Unit.php, then they will look for model factory class on database/factories/Entities/Units/UnitFactory.php

I have created the fix on PR #40.

@nafiesl
Copy link
Owner Author

nafiesl commented Apr 20, 2021

PR #40 has been merged, and the fix has been released on the tag/2.3.1. Closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant