Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Junior Grossi:
Solitary or Sociable? Testing Events and Listeners using Laravel
Apr 30, 2018 @ 14:38:58

In a new post to his site Junior Grossi covers two main methods for testing event handling in Laravel-based applications: either solitary or sociable.

Testing with Laravel is very easy, but it can be a nightmare when the tests depend on Events and Listeners. In this post I’m gonna show you how you can simplify and improve those tests.

[...] When testing events in Laravel, you can fake() events in a very simple way, like the documentation says. The problem is not with the events, but with the listeners, because usually a listener does a single action, but with more than one listener you start writing repeatable tests.

He starts by looking at some of the background behind the ideas of "solitary" and "sociable" as it relates to events. He then gets into the code, providing some examples of event tests (and how it could evolve into larger, unwieldy tests). He provides some recommendations on refactoring tests to help get around these issues. As a bonus at the end of the post he shows how to create sociable tests, complete with example code.

tagged: testing event handling laravel tutorial solitary sociable

Link: https://blog.jgrossi.com/2018/solitary-or-sociable-testing-events-and-listeners-using-laravel/


Trending Topics: