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

[5.6] Allow closure to determine if event should be faked #24887

Merged
merged 3 commits into from
Jul 22, 2018
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
19 changes: 16 additions & 3 deletions src/Illuminate/Support/Testing/Fakes/EventFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function dispatch($event, $payload = [], $halt = false)
{
$name = is_object($event) ? get_class($event) : (string) $event;

if ($this->shouldFakeEvent($name)) {
if ($this->shouldFakeEvent($name, $payload)) {
$this->events[$name][] = func_get_args();
} else {
$this->dispatcher->dispatch($event, $payload, $halt);
Expand All @@ -218,11 +218,24 @@ public function dispatch($event, $payload = [], $halt = false)
* Determine if an event should be faked or actually dispatched.
*
* @param string $eventName
* @param mixed $payload
* @return bool
*/
protected function shouldFakeEvent($eventName)
protected function shouldFakeEvent($eventName, $payload)
{
return empty($this->eventsToFake) || in_array($eventName, $this->eventsToFake);
if (empty($this->eventsToFake)) {
return true;
}

return collect($this->eventsToFake)
->filter(function ($event) use ($eventName, $payload) {
if (is_callable($event)) {
return $event($eventName, $payload);
}

return $event === $eventName;
})
->isEmpty();
}

/**
Expand Down
23 changes: 22 additions & 1 deletion tests/Support/SupportTestingEventFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function setUp()
$this->fake = new EventFake(m::mock(Dispatcher::class));
}

public function testAssertDispacthed()
public function testAssertDispatched()
{
try {
$this->fake->assertDispatched(EventStub::class);
Expand Down Expand Up @@ -74,6 +74,27 @@ public function testAssertNotDispatched()
$this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\EventStub] event was dispatched.'));
}
}

public function testAssertDispatchedWithIgnore()
{
$dispatcher = m::mock(Dispatcher::class);
$dispatcher->shouldReceive('dispatch')->twice();

$fake = new EventFake($dispatcher, [
'Foo',
function ($event, $payload) {
return $event === 'Bar' && $payload['id'] === 1;
},
]);

$fake->dispatch('Foo');
$fake->dispatch('Bar', ['id' => 1]);
$fake->dispatch('Baz');

$fake->assertNotDispatched('Foo');
$fake->assertNotDispatched('Bar');
$fake->assertDispatched('Baz');
}
}

class EventStub
Expand Down