Category: Laravel, PHP, api

I’ve been searching for this test and the most similar i could find was from a Adam Wathan 2016’s video about “Three Approaches to Testing Events in Laravel” but that was not exactly what i wanted, so i’m writing this to people that are suffering the same problem :) I was needing a test that could identify if a listener had been changed, imagine if someone changes a letter in the EventServiceProvider and a important part of your application stops doing its job.

Here is an example of the test that validates the Listeners, when creating this test i first came across with the Closure type, i had never heard of it before, well, it is basically a class format so i had a class and i needed to get Information from that class, and it was at this point that i found the ReflectionFunction, it is from Reflection that is a php API that adds the ability to introspect classes and do other cool stuff, i recommend you to check out the php doc about these. Lets make an proposital error in the code to see if the test is working fine adding another “r” at the end of the second listener.

it will launch an error every time the Event has a listener that it should not have or when a listener has been modified and does not correspond to the valid listeners.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners