Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
semihkeskindev committed Feb 25, 2024
1 parent f62a048 commit 255e981
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 24 deletions.
4 changes: 2 additions & 2 deletions config/request-forwarder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
7 changes: 7 additions & 0 deletions src/Exceptions/WebhookGroupNameNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Moneo\RequestForwarder\Exceptions;

class WebhookGroupNameNotFoundException extends \Exception
{
}
7 changes: 0 additions & 7 deletions src/Exceptions/WebhookNameNotFoundException.php

This file was deleted.

29 changes: 16 additions & 13 deletions src/RequestForwarder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Http\Client\Factory;
use Illuminate\Http\Request;
use Moneo\RequestForwarder\Exceptions\WebhookNameNotFoundException;
use Moneo\RequestForwarder\Exceptions\WebhookGroupNameNotFoundException;
use Moneo\RequestForwarder\Providers\DefaultProvider;
use Moneo\RequestForwarder\Providers\ProviderInterface;

Expand All @@ -16,17 +16,17 @@ public function __construct(
) {
}

public function sendAsync(Request $request, ?string $webhookName = null): void
public function sendAsync(Request $request, ?string $webhookGroupName = null): void
{
ProcessRequestForwarder::dispatch($request->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;
Expand All @@ -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'];
}
}
62 changes: 62 additions & 0 deletions tests/RequestForwarderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Support\Facades\Http;

use Moneo\RequestForwarder\RequestForwarder;
use function Pest\Laravel\get;
use function Pest\Laravel\post;

Expand All @@ -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);
});


7 changes: 5 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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');
});
}
}

0 comments on commit 255e981

Please sign in to comment.