Omda's avatar
Level 1

My factory is not applied to my test cases

i am working with phpunit when i create model using factory is not create the field i was put in UserFactory, even if i delete UserFactory it created fields, i dont know what is going on.. this userFactory file

use Faker\Generator as Faker;

$factory->define(\App\User::class, function (Faker $faker) {
    static $password;
    return [
      'name' => $faker->name,  
      'email' => $faker->unique()->safeEmail,  
      'password' => $password ? : $password =bcrypt('12345678'),  
      'remember_token' => str_random(80),  
      'verified' => 1,  
    ];
});

and when use this

 $ownerUser = factory(\App\User::class)->create();

and result equal

    "name" => "Rollin Ebert"
    "email" => "[email protected]"
    "password" => "y$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm"
    "remember_token" => "kZx1MiWx5G"
    "updated_at" => "2019-09-14 12:00:09"
    "created_at" => "2019-09-14 12:00:09"
    "id" => 11
0 likes
12 replies
ftiersch's avatar

Where is your UserFactory file located? In database/factories?

Tray2's avatar

Is the filename userFactory.php or UserFactory.php?

If it's the first rename it to the second.

ftiersch's avatar

And your User class has a "verified" column? Is the verified column filled in the database when you execute the factory?

Omda's avatar
Level 1

@ftiersch this my migration

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('password');
            $table->boolean('verified')->default(false);
            $table->rememberToken();
            $table->timestamps();
        });
    }
tykus's avatar

It appears that you might have modified the $hidden or $visible property, or implemented toArray() on the model because I would not expect to see the password hash and remember token in the result. Did you also give the verified field the same treatment?

Omda's avatar
Level 1

@tykus this my attributes in mode user

 protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
Tray2's avatar

That depends, I would not use transactions unless it is critical that more than one database action needs to complete successfully.

Let's say you have a bank and you want to transfer funds from one account to another.

  1. Deduct the amount from the source account.
  2. Add the amount to the destination account.
  3. If both actions finish ok commit it else rollback.

So if you have that kind of need use transactions otherwise I would suggest that you don't use it.

Somethings to be aware of is that transactions can cause deadlocks in your database. This is normal behaviour but you need to handle it.

https://laravel.com/docs/6.x/database#database-transactions

mstrauss's avatar

@omda

Try updating the default value on your verified column in the migration to 0 (as opposed to false, since it's a boolean field), see below:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('password');
            $table->boolean('verified')->default(0);
            $table->rememberToken();
            $table->timestamps();
        });
    }
Omda's avatar
Level 1

my issue is solved, the problems i found there are two UserFactory.php one in root directory database and another in Module, cause i use Module package.

Please or to participate in this conversation.