From 0bed85a159982dcd299bc1ebb1ade5bc4614c65e Mon Sep 17 00:00:00 2001 From: Roy de Vos Burchart Date: Wed, 26 Oct 2022 16:04:09 +0200 Subject: [PATCH] throw exception when broadcast connection not configured --- .../Broadcasting/BroadcastManager.php | 4 +++ .../Broadcasting/BroadcastManagerTest.php | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Illuminate/Broadcasting/BroadcastManager.php b/src/Illuminate/Broadcasting/BroadcastManager.php index 616f99c6dc56..f479b0bcfc42 100644 --- a/src/Illuminate/Broadcasting/BroadcastManager.php +++ b/src/Illuminate/Broadcasting/BroadcastManager.php @@ -245,6 +245,10 @@ protected function resolve($name) { $config = $this->getConfig($name); + if (is_null($config)) { + throw new InvalidArgumentException("Broadcast connection [{$name}] is not defined."); + } + if (isset($this->customCreators[$config['driver']])) { return $this->callCustomCreator($config); } diff --git a/tests/Integration/Broadcasting/BroadcastManagerTest.php b/tests/Integration/Broadcasting/BroadcastManagerTest.php index 8ab47fb6554e..d23bff703f02 100644 --- a/tests/Integration/Broadcasting/BroadcastManagerTest.php +++ b/tests/Integration/Broadcasting/BroadcastManagerTest.php @@ -3,7 +3,10 @@ namespace Illuminate\Tests\Integration\Broadcasting; use Illuminate\Broadcasting\BroadcastEvent; +use Illuminate\Broadcasting\BroadcastManager; use Illuminate\Broadcasting\UniqueBroadcastEvent; +use Illuminate\Config\Repository; +use Illuminate\Container\Container; use Illuminate\Contracts\Broadcasting\ShouldBeUnique; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; @@ -11,6 +14,7 @@ use Illuminate\Support\Facades\Broadcast; use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Queue; +use InvalidArgumentException; use Orchestra\Testbench\TestCase; class BroadcastManagerTest extends TestCase @@ -50,6 +54,36 @@ public function testUniqueEventsCanBeBroadcast() $lockKey = 'laravel_unique_job:'.UniqueBroadcastEvent::class.TestEventUnique::class; $this->assertFalse($this->app->get(Cache::class)->lock($lockKey, 10)->get()); } + + public function testThrowExceptionWhenUnknownStoreIsUsed() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Broadcast connection [alien_connection] is not defined.'); + + $userConfig = [ + 'broadcasting' => [ + 'connections' => [ + 'my_connection' => [ + 'driver' => 'pusher', + ], + ], + ], + ]; + + $app = $this->getApp($userConfig); + + $broadcastManager = new BroadcastManager($app); + + $broadcastManager->connection('alien_connection'); + } + + protected function getApp(array $userConfig) + { + $app = new Container; + $app->singleton('config', fn () => new Repository($userConfig)); + + return $app; + } } class TestEvent implements ShouldBroadcast