jasonmccreary / laravel-test-assertions

A set of helpful assertions when testing Laravel applications.
321 stars 34 forks source link

Add assert event has listener #11

Closed jcergolj closed 3 years ago

jcergolj commented 4 years ago

Here is my proposal for adding Event has Listener Assertion. I copied the code from Event List Listener artisan command and I added assertion.

Usage:

$this->assertEventHasListener(ProfileImageUploadedEvent::class, ResizeImageListener::class);
jasonmccreary commented 4 years ago

@jcergolj sorry for the delay on this. A few follow-up questions before merging.

jcergolj commented 4 years ago

@jasonmccreary no worries.

1.What does the getProviders do? From the Laravel docs: Get the registered service provider instances if any exist.

    // vendor/laravel/framework/src/Illuminate/Foundation/Application.php
    /**
     * Get the registered service provider instances if any exist.
     *
     * @param  \Illuminate\Support\ServiceProvider|string  $provider
     * @return array
     */
    public function getProviders($provider)
    {
        $name = is_string($provider) ? $provider : get_class($provider);

        return Arr::where($this->serviceProviders, function ($value) use ($name) {
            return $value instanceof $name;
        });
    }
  1. When would you use this over using Event::fake? If I understand this correctly I would use Event::fake in a feature test to assert the event is fired. Then I would unit test the listener. And finally, a unit test that event has a listener. IMO this gives me an option to test in isolation a bit more.

Here is an article how would I use this: https://www.linkedin.com/pulse/my-elegant-way-testing-laravel-events-listeners-janez-cergolj/

jasonmccreary commented 3 years ago

Closed due to inactivity and conflicts. Feel free to rebase and reopen.