Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow faking only specific events #19429

Merged
merged 6 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Facades/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class Event extends Facade
/**
* Replace the bound instance with a fake.
*
* @param array|string $eventsToFake
* @return void
*/
public static function fake()
public static function fake($eventsToFake = [])
{
static::swap($fake = new EventFake);
static::swap($fake = new EventFake(static::getFacadeRoot(), $eventsToFake));

Model::setEventDispatcher($fake);
}
Expand Down
48 changes: 46 additions & 2 deletions src/Illuminate/Support/Testing/Fakes/EventFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,47 @@

namespace Illuminate\Support\Testing\Fakes;

use Illuminate\Support\Arr;
use PHPUnit\Framework\Assert as PHPUnit;
use Illuminate\Contracts\Events\Dispatcher;

class EventFake implements Dispatcher
{
/**
* All of the events that have been dispatched keyed by type.
* The original event dispatcher.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $dispatcher;

/**
* The event types that should be intercepted instead of dispatched.
*
* @var array
*/
protected $eventsToFake;

/**
* All of the events that have been intercepted keyed by type.
*
* @var array
*/
protected $events = [];

/**
* Create a new event fake instance.
*
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
* @param array|string $eventsToFake
* @return void
*/
public function __construct(Dispatcher $dispatcher, $eventsToFake = [])
{
$this->dispatcher = $dispatcher;

$this->eventsToFake = Arr::wrap($eventsToFake);
}

/**
* Assert if an event was dispatched based on a truth-test callback.
*
Expand Down Expand Up @@ -159,7 +188,22 @@ public function dispatch($event, $payload = [], $halt = false)
{
$name = is_object($event) ? get_class($event) : (string) $event;

$this->events[$name][] = func_get_args();
if ($this->shouldFakeEvent($name)) {
$this->events[$name][] = func_get_args();
} else {
$this->dispatcher->dispatch($event, $payload, $halt);
}
}

/**
* Determine if an event should be faked or actually dispatched.
*
* @param string $eventName
* @return bool
*/
protected function shouldFakeEvent($eventName)
{
return empty($this->eventsToFake) || in_array($eventName, $this->eventsToFake);
}

/**
Expand Down