From 2eb8734f1d24ba50430638b49618c083726c6bad Mon Sep 17 00:00:00 2001 From: Arjan Date: Wed, 18 Jul 2018 20:02:39 +0200 Subject: [PATCH 1/3] Allow closures to check if event should be faked --- .../Support/Testing/Fakes/EventFake.php | 19 ++++++++++++++--- tests/Support/SupportTestingEventFakeTest.php | 21 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/EventFake.php b/src/Illuminate/Support/Testing/Fakes/EventFake.php index edbb90eb1b96..539c14b25617 100644 --- a/src/Illuminate/Support/Testing/Fakes/EventFake.php +++ b/src/Illuminate/Support/Testing/Fakes/EventFake.php @@ -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); @@ -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(); } /** diff --git a/tests/Support/SupportTestingEventFakeTest.php b/tests/Support/SupportTestingEventFakeTest.php index 21ba64dbaa50..bb2d07e83994 100644 --- a/tests/Support/SupportTestingEventFakeTest.php +++ b/tests/Support/SupportTestingEventFakeTest.php @@ -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 From eaa03b5067a323da7194fe605faa75e5b20cb38c Mon Sep 17 00:00:00 2001 From: Arjan Date: Wed, 18 Jul 2018 20:02:46 +0200 Subject: [PATCH 2/3] Fix typo --- tests/Support/SupportTestingEventFakeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportTestingEventFakeTest.php b/tests/Support/SupportTestingEventFakeTest.php index bb2d07e83994..ccef26133767 100644 --- a/tests/Support/SupportTestingEventFakeTest.php +++ b/tests/Support/SupportTestingEventFakeTest.php @@ -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); From 33db136da97d2111790e591f0c32497e3ce11e51 Mon Sep 17 00:00:00 2001 From: Arjan Date: Wed, 18 Jul 2018 20:22:43 +0200 Subject: [PATCH 3/3] Style fix --- tests/Support/SupportTestingEventFakeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportTestingEventFakeTest.php b/tests/Support/SupportTestingEventFakeTest.php index ccef26133767..75d30323724e 100644 --- a/tests/Support/SupportTestingEventFakeTest.php +++ b/tests/Support/SupportTestingEventFakeTest.php @@ -84,7 +84,7 @@ public function testAssertDispatchedWithIgnore() 'Foo', function ($event, $payload) { return $event === 'Bar' && $payload['id'] === 1; - } + }, ]); $fake->dispatch('Foo');