jasonmccreary / laravel-test-assertions

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

Assert Event has Listener #10

Closed jcergolj closed 4 years ago

jcergolj commented 4 years ago

Assert Event has Listener might be an assertion that others may use too. My strategy for testing event is as follows: In a feature test, I assert that the event is fired. In a unit test, I assert everything related to the listener:

public function test_listener()
{
    $listener = new Listener();
    $listener->handle();

    $this->assert.....
}

What I need is a test in an EventServiceProviderTest class to assert that an event has a listener.

I copied and modified code from event list artican command:

public function assertEventHasListener($event, $listener)
{
        $events = [];

        foreach ($this->app->getProviders(EventServiceProvider::class) as $provider) {
            $providerEvents = array_merge_recursive(
                $provider->shouldDiscoverEvents() ? $provider->discoverEvents() : [], $provider->listens()
            );

            $events = array_merge_recursive($events, $providerEvents);
        }

        $this->assertTrue(in_array($listener, $events[$event]));
}

I can create a PR but I don't have access.

jasonmccreary commented 4 years ago

Could be interesting.

In order to contribute to an open source project on GitHub, it's best to make a fork. Review this project for more details on making your first contribution.

jcergolj commented 4 years ago

Thanks for the guidance. I am contributing to an open source project for the first time. PR submitted.

jasonmccreary commented 4 years ago

Nice work!

I'm going to close this issue and focus on the PR later today.