Skip to content

Commit

Permalink
Merge pull request #2 from JustSteveKing/feature/webhook-notification
Browse files Browse the repository at this point in the history
Creating a webhook channel
  • Loading branch information
JustSteveKing committed Apr 22, 2023
2 parents aac78af + d1dad4a commit ca9a59b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Channels/WebhookChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace JustSteveKing\Webhooks\Channels;

use Illuminate\Http\Client\Response;
use Illuminate\Notifications\Notification;
use JustSteveKing\Webhooks\Builder\PendingWebhook;

final class WebhookChannel
{
/**
* @param Notification $notification
* @return Response|null
*/
public function send(mixed $notifiable, Notification $notification): null|Response
{
/**
* @var PendingWebhook $webhook
*/
$webhook = $notification->toWebhook($notifiable);

return $webhook->send();
}
}
19 changes: 19 additions & 0 deletions tests/Channels/WebhookChannelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use JustSteveKing\Webhooks\Channels\WebhookChannel;
use JustSteveKing\Webhooks\Tests\Stubs\TestNotifiable;
use JustSteveKing\Webhooks\Tests\Stubs\TestNotification;

it('can send a notification', function (): void {
Http::fake();

$channel = new WebhookChannel();

$channel->send(new TestNotifiable(), new TestNotification());

Http::assertSentCount(1);
});
17 changes: 17 additions & 0 deletions tests/Stubs/TestNotifiable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace JustSteveKing\Webhooks\Tests\Stubs;

use Illuminate\Notifications\Notifiable;

final class TestNotifiable
{
use Notifiable;

public function routeNotificationForWebhook(): string
{
return 'https://some-url.com/';
}
}
21 changes: 21 additions & 0 deletions tests/Stubs/TestNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace JustSteveKing\Webhooks\Tests\Stubs;

use Illuminate\Notifications\Notification;
use JustSteveKing\Webhooks\Builder\PendingWebhook;
use JustSteveKing\Webhooks\Facades\Webhook;

final class TestNotification extends Notification
{
public function toWebhook(mixed $notifiable): PendingWebhook
{
return Webhook::to(
url: 'https://www.some-url.com/',
)->with(
payload: [],
);
}
}

0 comments on commit ca9a59b

Please sign in to comment.