diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 1429a22..44a48dd 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -60,7 +60,7 @@ jobs: run: | composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update composer require "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update - composer update --prefer-dist --no-interaction --no-progress + composer update --prefer-dist --no-interaction --no-progress --no-plugins - name: Execute tests run: vendor/bin/phpunit diff --git a/src/EventServiceProvider.php b/src/EventServiceProvider.php index cf96ed6..421423b 100644 --- a/src/EventServiceProvider.php +++ b/src/EventServiceProvider.php @@ -47,7 +47,7 @@ protected function registerSnsBroadcaster() * @param array $config * @return \Illuminate\Contracts\Broadcasting\Broadcaster */ - protected function createSnsDriver(array $config): \Illuminate\Contracts\Broadcasting\Broadcaster + public function createSnsDriver(array $config): \Illuminate\Contracts\Broadcasting\Broadcaster { $config = self::prepareConfigurationCredentials($config); @@ -81,9 +81,7 @@ protected function registerEventBridgeBroadcaster() { $this->app->resolving(BroadcastManager::class, function (BroadcastManager $manager) { $manager->extend('eventbridge', function (Container $app, array $config) { - return $this->createEventBridgeDriver(array_merge($config, [ - 'version' => '2015-10-07', - ])); + return $this->createEventBridgeDriver($config); }); }); } @@ -94,12 +92,12 @@ protected function registerEventBridgeBroadcaster() * @param array $config * @return \Illuminate\Contracts\Broadcasting\Broadcaster */ - protected function createEventBridgeDriver(array $config): \Illuminate\Contracts\Broadcasting\Broadcaster + public function createEventBridgeDriver(array $config): \Illuminate\Contracts\Broadcasting\Broadcaster { $config = self::prepareConfigurationCredentials($config); return new EventBridgeBroadcaster( - new EventBridgeClient($config), + new EventBridgeClient(array_merge($config, ['version' => '2015-10-07'])), $config['source'] ?? '' ); } @@ -112,10 +110,23 @@ protected function createEventBridgeDriver(array $config): \Illuminate\Contracts */ public static function prepareConfigurationCredentials(array $config): array { - if (Arr::has($config, ['key', 'secret'])) { + if (static::configHasCredentials($config)) { $config['credentials'] = Arr::only($config, ['key', 'secret', 'token']); } return $config; } + + /** + * Make sure some AWS credentials were provided to the configuration array. + * + * @param array $config + * @return bool + */ + private static function configHasCredentials(array $config): bool + { + return Arr::has($config, ['key', 'secret']) + && is_string(Arr::get($config, 'key')) + && is_string(Arr::get($config, 'secret')); + } } diff --git a/tests/Pub/Broadcasting/Broadcasters/EventBridgeBroadcasterTest.php b/tests/Pub/Broadcasting/Broadcasters/EventBridgeBroadcasterTest.php new file mode 100644 index 0000000..6903320 --- /dev/null +++ b/tests/Pub/Broadcasting/Broadcasters/EventBridgeBroadcasterTest.php @@ -0,0 +1,53 @@ +app))->createEventBridgeDriver([ + 'driver' => 'eventbridge', + 'key' => 'dummy-key', + 'secret' => 'dummy-secret', + 'region' => 'eu-west-1', + 'event_bus' => 'default', + 'source' => 'my-app', + ]); + + $this->assertInstanceOf(EventBridgeBroadcaster::class, $broadcaster); + } + + /** @test */ + public function it_supports_optional_aws_credentials() + { + $broadcaster = (new EventServiceProvider($this->app))->createEventBridgeDriver([ + 'driver' => 'eventbridge', + 'region' => 'eu-west-1', + 'event_bus' => 'default', + 'source' => 'my-app', + ]); + + $this->assertInstanceOf(EventBridgeBroadcaster::class, $broadcaster); + } + + /** @test */ + public function it_supports_null_aws_credentials() + { + $broadcaster = (new EventServiceProvider($this->app))->createEventBridgeDriver([ + 'driver' => 'eventbridge', + 'key' => null, + 'secret' => null, + 'region' => 'eu-west-1', + 'event_bus' => 'default', + 'source' => 'my-app', + ]); + + $this->assertInstanceOf(EventBridgeBroadcaster::class, $broadcaster); + } +} diff --git a/tests/Pub/Broadcasting/Broadcasters/SnsBroadcasterTest.php b/tests/Pub/Broadcasting/Broadcasters/SnsBroadcasterTest.php new file mode 100644 index 0000000..5e67573 --- /dev/null +++ b/tests/Pub/Broadcasting/Broadcasters/SnsBroadcasterTest.php @@ -0,0 +1,53 @@ +app))->createSnsDriver([ + 'driver' => 'sns', + 'key' => 'dummy-key', + 'secret' => 'dummy-secret', + 'arn-prefix' => 'aws:arn:12345:', + 'region' => 'eu-west-1', + ]); + + $this->assertInstanceOf(SnsBroadcaster::class, $broadcaster); + } + + /** @test */ + public function it_supports_optional_aws_credentials() + { + $broadcaster = (new EventServiceProvider($this->app))->createSnsDriver([ + 'driver' => 'sns', + 'arn-prefix' => 'aws:arn:12345:', + 'region' => 'eu-west-1', + ]); + + $this->assertInstanceOf(SnsBroadcaster::class, $broadcaster); + } + + /** @test */ + public function it_supports_null_aws_credentials() + { + $broadcaster = (new EventServiceProvider($this->app))->createSnsDriver([ + 'driver' => 'sns', + 'key' => null, + 'secret' => null, + 'arn-prefix' => 'aws:arn:12345:', + 'region' => 'eu-west-1', + ]); + + $this->assertInstanceOf(SnsBroadcaster::class, $broadcaster); + } +}