2017 © Pedro Peláez
 

library laravel-event-fake

An enhanced event fake for Laravel event testing

image

score-labs/laravel-event-fake

An enhanced event fake for Laravel event testing

  • Wednesday, March 28, 2018
  • by BrandonShar
  • Repository
  • 0 Watchers
  • 5 Stars
  • 85 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 2 Versions
  • 27 % Grown

The README.md

Laravel Event Fake

Enhanced Laravel Event Fake for testing, (*1)

Have you ever wanted more versatility when you're testing your Laravel events? If you're like me, you're uncomfortable with the idea of just testing the listeners in isolation, but leaving all of the events unfaked makes your tests a mess. Well fret no further kindred spirit! Score Labs' Laravel Event Fake is here!, (*2)

So how's it work?

Score Labs' Laravel Event Fake adds two new methods to the already great Event facade., (*3)

Installation

composer require --dev score-labs/laravel-event-fake

In your test classes, just import the new Event facade, (*4)

use ScoreLabs\Event; // full namespace
use Event; // overwritten alias via laravel auto-discovery

Methods

Fake Except

There are two ways to call Event::fakeExcept:, (*5)

Event::fakeExcept(EventThatIWantToFire::class);

Will fake every event except for EventThatIWantToFire. That event and all of its listeners will fire normally., (*6)

Event::fakeExcept(EventThatIWantToFire::class, TheOnlyListenerIWantToFire::class);

Will fake every event except for EventThatIWantToFire and will spy every listener except for `TheOnlyListenerIWantToFire. This usage is great for when you want to isolate a single listener, but you still want to test that the event setup is working as expected., (*7)

Once you've passed listeners to spy, you can still assert that they were fired without their code running by using, (*8)

Event::assertHandled(AnotherListener::class);
// or 
Event::assertHandled(AnotherListener::class, function ($event) {
    return $event instanceof EventIWantToFire;
});

:point_up: You can also pass arrays of class names for either argument to allow through multiple events/listeners at once!, (*9)

Fake Except Models

If you want to fake all events, but you still want events that you manually added in your models boot method to fire, just use, (*10)

Event::fakeExceptModels();

This way you can fake all events as usual, but still let your models get any necessary information they may need during a creating or saving event., (*11)

But what about all of the great existing features of Event::fake?

They are all still there! This is just a drop in enhancement and will be compatible with all usages of Event::fake in Laravel 5.5+, (*12)


This code is MIT Licensed and open to all contributors. For pull requests, please try to mimic the existing code style., (*13)

The Versions

28/03 2018

dev-master

9999999-dev

An enhanced event fake for Laravel event testing

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brandon Shar

laravel testing laravel event

28/03 2018

v1.0

1.0.0.0

An enhanced event fake for Laravel event testing

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brandon Shar

laravel testing laravel event