-1

UserTest class :

// test birth_date cast class
public function testBirthDateCasting()
{
    $username = 'ali';

    // test get method
    $user = User::factory()->create([
        User::USERNAME      => $username,
        User::BIRTH_DATE => Carbon::now()->format('Y-m-d')
    ]);
    assertEquals(\Verta::parse($user->getAttribute(User::BIRTH_DATE))->format('Y-m-d'), $user->getAttribute(User::BIRTH_DATE));

    // test set method
    $newBirthDate = verta()->format('Y-m-d');
    $user->update([
        User::BIRTH_DATE=> $newBirthDate
    ]);

    // write test set method ...
}

in BirthDateCast class :

    class BirthDateCast implements CastsAttributes
{
    /**
     * Cast the given value.
     */
    public function get(Model $model, string $key, mixed $value, array $attributes): mixed
    {
        return \verta($value)->format('Y-m-d');
    }

   /**
    * Prepare the given value for storage.
    */
   public function set(Model $model, string $key, mixed $value, array $attributes): mixed
   {
    return Verta::parse($value)->formatGregorian('Y-m-j');
   }
}

hi everyone! How to test set method in BirthDateCast in Laravel?

Thanks in advance for your help Thanks in advance for your help Thanks in advance for your help Thanks in advance for your help

Ali Salehi
  • 23
  • 1
  • 8

0 Answers0