clemblanco

clemblanco

Member Since 1 Month Ago

London

Full Stack Developer at Pod Point Ltd

Experience Points 80
Experience Level 1

4,920 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 0
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

18 Mar
1 month ago

clemblanco left a reply on Testing Model Observer Events - Laravel 5.6

Okay so @martinbean you were right about the Job.

App\Providers\AppServiceProvider

public function boot()
{
    User::observe(UserObserver::class);
}

App\Observers\UserObserver

public function created(User $user)
{
    BarJob::dispatch();
}

App\Jobs\BarJob

class BarJob implements ShouldQueue
{
    public function handle()
    {
        // Something happens here...
    }
}

Now I can test it with:

Bus::fake();

factory(User::class)->create();

Bus::assertDispatched(BarJob::class)

As soon as I try to trigger an event from with the Model Observer Event (instead of dispatching a Job), I cannot easily cover it with tests as the Listener is encapsulated within a CallQueuedListener "job" for some reason.

However, from the application point of view, my Event gets triggered when the Model Observer Event gets triggered and the application behaves as expected.

TLDR;

  • Dispatch a Job from within a Model Observer Event: YES
  • Trigger another Event from within a Model Observer Event: NO

Thanks @martinbean for your help.

Not sure if I should open an issue on laravel/framework or if it's the expected behaviour.

clemblanco left a reply on Testing Model Observer Events - Laravel 5.6

Yeah @martinbean that could do it but I need it to be queued so would you recommend instead of triggering an Event from a Model Observer, dispatching a Job to do the re-calculation?

clemblanco left a reply on Testing Model Observer Events - Laravel 5.6

Yeah could do but I need it to be queued so would you recommend instead of triggering an Event from a Model Observer, dispatch a Job to do the re-calculation?

clemblanco left a reply on Testing Model Observer Events - Laravel 5.6

Thanks for your answer @martinbean.

The initial idea was to have a single Event to trigger from various Model Observers so I could avoid duplicating code in two Model Observers.

clemblanco started a new conversation Testing Model Observer Events

I'm facing a weird issue today while trying to test a Model Observer Event.

App\Providers\AppServiceProvider

public function boot()
{
    User::observe(UserObserver::class);
}

App\Observers\UserObserver

public function created(User $user)
{
    event(new FooEvent);
}

App\Providers\EventServiceProvider

protected $listen = [
    'App\Events\FooEvent' => ['App\Listeners\BarListener']
];

App\Listeners\BarListener

class BarListener implements ShouldQueue
{
    public function handle(FooEvent $event)
    {
        // Something happens here...
    }
}

Now in my test if I do:

Queue::fake();

factory(User::class)->create();

Queue::assertPushed(BarListener::class)

The assertion will always fail saying The expected [App\Listeners\BarListener] job was not pushed.

Now if I dig a bit deeper in what this assertPushed() does, I can find that it runs through:

Illuminate\Support\Testing\Fakes\QueueFake

public function hasPushed($job)
{
    return isset($this->jobs[$job]) && ! empty($this->jobs[$job]);
}

and if I dump $this->jobs I can see it coming as:

array:1 [
  "Illuminate\Events\CallQueuedListener" => array:1 [
    0 => array:2 [
      "job" => Illuminate\Events\CallQueuedListener {#2709
        +class: "App\Listeners\BarListener"
        +method: "handle"
        +data: array:1 [
          0 => App\Events\FooEvent {#2715
            +user: App\User {#2832
              ...
            }
          ]
        }
      ]
    }
  ]
]

I understand that instead of having Illuminate\Events\CallQueuedListener I should see App\Listeners\BarListener used as a key in that array but I'm not entirely sure why...

Does it come from the fact I'm using a Model Observer to trigger another Event?

Or should Queue::assertPushed() only be used with Jobs? Not Listeners?

Or is it completely normal and should I instead assert that CallQueuedListener is triggered with the associated arguments with the following?

Queue::fake();

factory(User:class)->create();

Queue:: assertPushed(\Illuminate\Events\CallQueuedListener::class, function ($job) {
    return $job->class === \App\Listeners\BarListener::class;
});

I'm a bit confused...