Laravel 5.3.8 is released with new fakes for events, jobs, mail, and notifications


September 9th, 2016

Laravel 5.3.8 is released with new fakes for events, jobs, mail, and notifications

Laravel 5.3.8 is now released with new improvements for testing by included new fakes for events, jobs, mail, and notifications.

Here is a quick look at how these new testing featurs work:

Laravel Events

Laravel now provides three helpers for mocking events. The first is expectsEvents method which verifies the expected events are fired, but prevents any listeners for those events from executing. The second is the inverse doesntExpectEvents to verify that the given events are not fired:

1public function testOrderShipping()
3 $this->expectsEvents(OrderShipped::class);
4 $this->doesntExpectEvents(OrderFailedToShip::class);

The next helper is withoutEvents which prevents all events from running:

1public function testUserRegistration()
3 $this->withoutEvents();

As an alternative to mocking, you may use the Event facade’s fake method to prevent all event listeners from executing.

1use App\Events\OrderShipped;
2use App\Events\OrderFailedToShip;
3use Illuminate\Support\Facades\Event;
5class ExampleTest extends TestCase
7 /**
8 * Test order shipping.
9 */
10 public function testOrderShipping()
11 {
12 Event::fake();
14 // Perform order shipping...
16 Event::assertFired(OrderShipped::class, function ($e) use ($order) {
17 return $e->order->id === $order->id;
18 });
20 Event::assertNotFired(OrderFailedToShip::class);
21 }

These same style helpers can also be used in jobs, mail, and notifications and the documentation is already updated with all the details.

Upgrading to 5.3.8 should just be a matter of running composer update and these new features will be available to be used.

Filed in:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.