From 85b327b949c2c0b21537b4b46cef07e7365cba5f Mon Sep 17 00:00:00 2001 From: Jeffrey Angenent Date: Wed, 24 Nov 2021 17:26:52 +0100 Subject: [PATCH 1/2] Add Event::fakeExcept() to fake all but certain events --- src/Illuminate/Support/Facades/Event.php | 15 ++++++++++++++ tests/Integration/Events/EventFakeTest.php | 23 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Illuminate/Support/Facades/Event.php b/src/Illuminate/Support/Facades/Event.php index cb4107fc54ee..c0833373bf07 100755 --- a/src/Illuminate/Support/Facades/Event.php +++ b/src/Illuminate/Support/Facades/Event.php @@ -45,6 +45,21 @@ public static function fake($eventsToFake = []) return $fake; } + /** + * Replace the bound instance with a fake, but allow the given events. + * + * @param string|string[] $eventsToAllow + * @return \Illuminate\Support\Testing\Fakes\EventFake + */ + public static function fakeExcept($eventsToAllow) + { + return static::fake([ + function (string $eventName) use ($eventsToAllow) { + return ! in_array($eventName, (array) $eventsToAllow); + }, + ]); + } + /** * Replace the bound instance with a fake during the given callable's execution. * diff --git a/tests/Integration/Events/EventFakeTest.php b/tests/Integration/Events/EventFakeTest.php index 3f5fab368aba..e6b7ab33c308 100644 --- a/tests/Integration/Events/EventFakeTest.php +++ b/tests/Integration/Events/EventFakeTest.php @@ -98,6 +98,29 @@ public function testNonFakedHaltedEventGetsProperlyDispatchedAndReturnsResponse( Event::assertNotDispatched(NonImportantEvent::class); } + public function testFakeExceptAllowsGivenEventToBeDispatched() + { + Event::fakeExcept(NonImportantEvent::class); + + Event::dispatch(NonImportantEvent::class); + + Event::assertNotDispatched(NonImportantEvent::class); + } + + public function testFakeExceptAllowsGivenEventsToBeDispatched() + { + Event::fakeExcept([ + NonImportantEvent::class, + 'non-fake-event', + ]); + + Event::dispatch(NonImportantEvent::class); + Event::dispatch('non-fake-event'); + + Event::assertNotDispatched(NonImportantEvent::class); + Event::assertNotDispatched('non-fake-event'); + } + public function testAssertListening() { Event::fake(); From 400fecf6accb65103f83d547087c2519f6211db3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 24 Nov 2021 20:33:18 -0600 Subject: [PATCH 2/2] formatting --- src/Illuminate/Support/Facades/Event.php | 27 +++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/Facades/Event.php b/src/Illuminate/Support/Facades/Event.php index c0833373bf07..02f26f95d9bf 100755 --- a/src/Illuminate/Support/Facades/Event.php +++ b/src/Illuminate/Support/Facades/Event.php @@ -46,15 +46,15 @@ public static function fake($eventsToFake = []) } /** - * Replace the bound instance with a fake, but allow the given events. + * Replace the bound instance with a fake that fakes all events except the given events. * - * @param string|string[] $eventsToAllow + * @param string[]|string $eventsToAllow * @return \Illuminate\Support\Testing\Fakes\EventFake */ public static function fakeExcept($eventsToAllow) { return static::fake([ - function (string $eventName) use ($eventsToAllow) { + function ($eventName) use ($eventsToAllow) { return ! in_array($eventName, (array) $eventsToAllow); }, ]); @@ -81,6 +81,27 @@ public static function fakeFor(callable $callable, array $eventsToFake = []) }); } + /** + * Replace the bound instance with a fake during the given callable's execution. + * + * @param callable $callable + * @param array $eventsToAllow + * @return mixed + */ + public static function fakeExceptFor(callable $callable, array $eventsToAllow = []) + { + $originalDispatcher = static::getFacadeRoot(); + + static::fakeExcept($eventsToAllow); + + return tap($callable(), function () use ($originalDispatcher) { + static::swap($originalDispatcher); + + Model::setEventDispatcher($originalDispatcher); + Cache::refreshEventDispatcher(); + }); + } + /** * Get the registered name of the component. *