diff --git a/config/request-forwarder.php b/config/request-forwarder.php index 8771df6..36db5d3 100644 --- a/config/request-forwarder.php +++ b/config/request-forwarder.php @@ -2,8 +2,8 @@ // config for Moneo/RequestForwarder return [ - // decides which webhook to use if no webhook name is specified while use middleware - 'default_webhook_name' => 'default', + // decides which webhook to use if no webhook group name is specified while use middleware + 'default_webhook_group_name' => 'default', 'webhooks' => [ 'default' => [ diff --git a/src/Exceptions/WebhookGroupNameNotFoundException.php b/src/Exceptions/WebhookGroupNameNotFoundException.php new file mode 100644 index 0000000..a715441 --- /dev/null +++ b/src/Exceptions/WebhookGroupNameNotFoundException.php @@ -0,0 +1,7 @@ +url(), $request->toArray(), $webhookName); + ProcessRequestForwarder::dispatch($request->url(), $request->toArray(), $webhookGroupName); } /** - * @throws WebhookNameNotFoundException + * @throws WebhookGroupNameNotFoundException */ - public function triggerHooks(string $url, array $params, ?string $webhookName = null): void + public function triggerHooks(string $url, array $params, ?string $webhookGroupName = null): void { - foreach ($this->getWebhookTargets($webhookName) as $webhook) { + foreach ($this->getWebhookTargets($webhookGroupName) as $webhook) { try { /** @var ProviderInterface $provider */ $providerClass = $webhook['provider'] ?? DefaultProvider::class; @@ -38,20 +38,23 @@ public function triggerHooks(string $url, array $params, ?string $webhookName = } /** - * @throws WebhookNameNotFoundException + * @throws WebhookGroupNameNotFoundException */ - private function getWebhookInfo(?string $webhookName = null): array + private function getWebhookInfo(?string $webhookGroupName = null): array { - $webhookName = $webhookName ?? config('request-forwarder.default_webhook_name'); + if (null === $webhookGroupName || '' === trim($webhookGroupName)) { + $webhookGroupName = config('request-forwarder.default_webhook_group_name'); + } - return $this->webhooks[$webhookName] ?? throw new WebhookNameNotFoundException('Webhook name called '.$webhookName.' is not defined in the config file'); + return $this->webhooks[$webhookGroupName] ?? throw new WebhookGroupNameNotFoundException('Webhook Group Name called '.$webhookGroupName.' is not defined in the config file'); } /** - * @throws WebhookNameNotFoundException + * // todo: DTO for return type + * @throws WebhookGroupNameNotFoundException */ - private function getWebhookTargets(?string $webhookName = null): array + private function getWebhookTargets(?string $webhookGroupName = null): array { - return $this->getWebhookInfo($webhookName)['targets']; + return $this->getWebhookInfo($webhookGroupName)['targets']; } } diff --git a/tests/RequestForwarderTest.php b/tests/RequestForwarderTest.php index 3bfad43..e2e9c68 100644 --- a/tests/RequestForwarderTest.php +++ b/tests/RequestForwarderTest.php @@ -2,6 +2,7 @@ use Illuminate\Support\Facades\Http; +use Moneo\RequestForwarder\RequestForwarder; use function Pest\Laravel\get; use function Pest\Laravel\post; @@ -19,3 +20,64 @@ get('/middleware')->assertStatus(200); Http::assertSentCount(4); }); + +it('must have valid default webhook group name in config file', function () { + $config = config('request-forwarder'); + expect($config['default_webhook_group_name'])->toBe('default'); +}); + +it('validates config file configuration is right', function () { + $config = config('request-forwarder'); + expect($config['webhooks'])->toBeArray(); + foreach (array_keys($config['webhooks']) as $webhookGroupName) { + expect($webhookGroupName)->toBeString(); + expect($config['webhooks'][$webhookGroupName])->toBeArray(); + expect($config['webhooks'][$webhookGroupName]['targets'])->toBeArray(); + foreach ($config['webhooks'][$webhookGroupName]['targets'] as $target) { + expect($target['url'])->toBeString(); + expect($target['method'])->toBeString(); + if (array_key_exists('provider', $target)) { + $provider = $target['provider']; + expect($provider)->toBeString(); + + $providerClass = new \ReflectionClass($provider); + expect($providerClass->implementsInterface(\Moneo\RequestForwarder\Providers\ProviderInterface::class))->toBeTrue(); + } + } + } +}); + +it('test private getWebhookInfo method returns valid data in RequestForwarder.php', function () { + $requestForwarder = app()->make(RequestForwarder::class); + $method = new ReflectionMethod(RequestForwarder::class, 'getWebhookInfo'); + $method->setAccessible('public'); + $getWebhookInfoNullParameterReturnedData = $method->invoke($requestForwarder); + + expect($getWebhookInfoNullParameterReturnedData)->toBeArray(); + + $getWebhookInfoEmptyStringParameterReturnedData = $method->invoke($requestForwarder, ''); + + expect($getWebhookInfoEmptyStringParameterReturnedData)->toBeArray(); +}); + +it('test getWebhookTargets method returns valid data in RequestForwarder.php', function () { + $requestForwarder = app()->make(RequestForwarder::class); + $method = new ReflectionMethod(RequestForwarder::class, 'getWebhookTargets'); + $method->setAccessible('public'); + $getWebhookTargetsReturnedData = $method->invoke($requestForwarder); + + expect($getWebhookTargetsReturnedData)->toBeArray(); +}); + +it('must throw WebhookGroupNameNotFoundException when use wrong webhook group name on defined route', function () { + Http::fake(); + $testResponse = get('/wrong-webhook-group-name-use-of-middleware', ['Accept' => 'application/json']); + + $testResponse->assertStatus(500); + + $testResponseData = $testResponse->json(); + + expect($testResponseData['exception'])->toBe(Moneo\RequestForwarder\Exceptions\WebhookGroupNameNotFoundException::class); +}); + + diff --git a/tests/TestCase.php b/tests/TestCase.php index 0e30741..0519753 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,7 +3,6 @@ namespace Moneo\RequestForwarder\Tests; use Illuminate\Support\Facades\Route; -use Moneo\RequestForwarder\RequestForwarderMiddleware; use Moneo\RequestForwarder\RequestForwarderServiceProvider; use Orchestra\Testbench\TestCase as Orchestra; @@ -26,6 +25,7 @@ protected function getPackageProviders($app) public function getEnvironmentSetUp($app) { config()->set('database.default', 'testing'); + config()->set('app.debug', true); } protected function registerTestRoutes(): void @@ -34,9 +34,12 @@ protected function registerTestRoutes(): void Route::any('/', fn () => 'No Middleware') ->name('no-middleware'); - Route::middleware(RequestForwarderMiddleware::class) + Route::middleware('request-forwarder') ->any('/middleware', fn () => 'With Middleware') ->name('middleware'); + + Route::middleware('request-forwarder:wrong-webhook-name') + ->any('/wrong-webhook-group-name-use-of-middleware', fn () => 'With Middleware, But Wrong Webhook Group Name'); }); } }