lucidarch / laravel

[DEPRECATED] See https://github.com/lucidarch/lucid

Home Page:https://lucidarch.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Factories - model class not found

rfpdl opened this issue · comments

commented

I am currently writing test code for one of my project and in need to use factories.

composer.json (autoload block):

"autoload": {
    "classmap": [
        "database",
        "database/factories"
    ],
    "psr-4": {
        "Framework\\": "app/",
        "App\\": "src/",
        "Tests\\": "tests/"
    }
},

Company model (located at src/Data/Company.php)

<?php
namespace App\Data;
use Illuminate\Database\Eloquent\Model;

class Company extends Model

Company factory (located at app/database/factories/CompanyFactory.php)

<?php
use Faker\Generator as Faker;

$factory->define(App\Data\Company::class, function (Faker $faker) { //updated
    return [
        'trade_name' => $faker->name,
        'address' => $faker->address,
        'country' => $faker->country,
    ];
});

Test code (located at tests/Feature/Register.php)

public function test_something()
{
    $company = factory(App\Data\Company::class)->create(); //updated

   dd($company);die;
}

I am getting Error: Class 'Company' not found

Note:

  • However, if I move the src/Data/Company.php to app/Company.php and change the namespace, it works.
  • Factories works for User if I use factories('Framework\User')->raw(); and will not work if I use factories('Framework\User')->create();

My issues:

  • How to make it work (factories and model) with Lucid default setting (Or I can update some settings if required to make it work correctly)
  • Should I move it to the app/? If yes, then lucid make:model <name> is creating model in src/Data/
  • Should I create the factory under src/Services/<name>/database/factories instead?

Your helps is very much appreciated. Thank you

The factory should be the
Company factory (located at app/database/factories/CompanyFactory.php)

<?php
use Faker\Generator as Faker;

$factory->define(App\Data\Company::class, function (Faker $faker) {
    return [
        'trade_name' => $faker->name,
        'address' => $faker->address,
        'country' => $faker->country,
    ];
});

after changing this perform next commands:

php artisan clear-compiled 
composer dump-autoload
php artisan optimize
commented

@alexvtua I did as you mentioned, I am getting this error:

Error: Class 'Company' not found

@rfpdl I believe you should use
$factory->define(\App\Data\Company::class, function (Faker $faker) {
instead of $factory->define(App\Data\Company::class, function (Faker $faker) {

there is a missing \ (it should be added in the code to be known that you look at the global scope of namespaces)

in another way you could import import App\Data\Company at the top
and use Company::class

AS @o-hreshchuk suggested try this:

use App\Data\Company;

$factory->define(Company::class, function (Faker\Generator $faker) {
    return [ /** ... */ ];
});

I have my factories running using this approach.

Thanks @alexvtua @o-hreshchuk @kennyhorna for your suggestions. They should work as expected 👍