mardi 10 juillet 2018

Laravel 5.4: Observer Events not working with PHPUnit

I'm trying to test a model Observer for my User class and I'm experiencing the same problems as many other programmers.
The updating/updated/... events won't fire when using PHPUnit. Trying it with tinker works.
So here's the problem: I tried this solution. Unfortunately without success.
This is my TestClass:

class UserObserverTest extends TestCase {

use DatabaseTransactions;

public function setUp()
  {
    parent::setUp();
    $this->user = factory("App\User")->create();
    $this->plan = new UserSettings();
    $this->difficulty1 = factory("App\Difficulty")->create();
    $this->difficulty2 = factory("App\Difficulty")->create();
    $this->plan->getExercisePlan()->appendAppExercise(factory("App\Exercise")->create(), false, $this->difficulty1, Constants::VORWAERTS);
    $this->plan->getExercisePlan()->appendAppExercise(factory("App\Exercise")->create(), false, $this->difficulty2, Constants::VORWAERTS);
    $this->user->settings = $this->plan->toJson();
    $this->user->save();
    $this->user = $this->user->fresh();

  }

public function test_Difficulty2HasUsedCounter0AfterDeletingFromPlan()
  {
    $this->assertEquals(1, $this->difficulty2->fresh()->used);
    $plan = new UserSettings($this->user->settings);
    $exercises = $plan->getExercisePlan()->getExercisesArray();
    array_pull($exercises, 1);
    $this->user->settings = $plan->toJson();
    $this->user->save();
    $this->assertEquals(0, $this->difficulty2->fresh()->used);
  }
}

UserObserver:

    class UserObserver
{

    /**
     * Listen to the User updating event
     * @param User $user
     */
    public function updating(User $user)
    {
        $settings = new UserSettings($user->settings);
        foreach($settings->getExercisePlan()->getExercisesArray() as $exercise)
        {
            $difficulty = Difficulty::find($exercise->getDifficultyId());
            $difficulty->used--;
            $difficulty->save();
        }
    }

    /**
     * Listen to the user updated event
     * @param User $user
     */
    public function updated(User $user)
   {
       $settings = new UserSettings($user->settings);
       foreach($settings->getExercisePlan()->getExercisesArray() as $exercise)
       {
           $difficulty = Difficulty::find($exercise->getDifficultyId());
           $difficulty->used++;
           $difficulty->save();
       }
   }

    /**
     * Listen to the User deleting event.
     *
     * @param  User  $user
     * @return void
     */
    public function deleting(User $user)
    {
        $settings = new UserSettings($user->settings);
        foreach($settings->getExercisePlan()->getExercisesArray() as $exercise)
        {
            $difficulty = Difficulty::find($exercise->getDifficultyId());
            $difficulty->used--;
            $difficulty->save();
        }
    }
}    

I registered the Observer using User::observe(UserObserver::class); inside boot() of AppServiceProvider



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire