Lwin Maung Maung

My Notes and Blogs

Home Laravel Linux
<-

Laravel test and the life of casting

Published on  Feb 18th, 2024Updated:  May 2nd, 2024
1 mins Read

I am creating a set of Laravel tests for my old project, which has been up since 2017. I started writing the  Laravel tests since the system has been running long with manual testing. With the help of Pest, I can now write multiple Laravel tests fast by sacrificing some pros of PHPUnit. 

When writing a Laravel test, my test fails due to password failure. The user exists but cannot be authenticatable. I tried a lot of ways, but still no hope.

The problem lies under my nose. As it comes from old versions, it has many old writings. My code for setting the password attribute is as follows:

class User{
//pre-codes
public function setPasswordAttribute($value){
    $this->attributes['password']=Hash::make($value);
}
//post-codes
}

The problem lies in the code. It works in the magic method but not with create(). I have to change the codes to the following:

protected $casts = [
    'password' => 'hashed'
];

Viola! My Laravel test passed.

 

© lwinmaungmaung MMXXIII. All Rights Reserved.