Skip to content

Commit 4459a87

Browse files
committed
Allow sending notifications via slack.
1 parent 58ea99e commit 4459a87

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

src/Notifications/Slack.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Binarcode\LaravelDeveloper\Notifications;
44

5+
use App\Notifications\OrderPlacedNotification;
56
use Binarcode\LaravelDeveloper\Dtos\DevNotificationDto;
67
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
78
use Binarcode\LaravelDeveloper\Telescope\TelescopeDev;
89
use Binarcode\LaravelDeveloper\Telescope\TelescopeException;
10+
use Illuminate\Notifications\Notification;
911
use Illuminate\Support\Collection;
1012
use Illuminate\Support\Facades\Notification as NotificationFacade;
1113
use Throwable;
@@ -68,7 +70,7 @@ private function send($item)
6870
if ($item instanceof Throwable) {
6971
if ($this->persist) {
7072
$dto = DevNotificationDto::makeFromExceptionLog(
71-
tap(ExceptionLog::makeFromException($item), fn (ExceptionLog $log) => $log->save())
73+
tap(ExceptionLog::makeFromException($item), fn(ExceptionLog $log) => $log->save())
7274
);
7375

7476
if ($this->telescope && TelescopeDev::allow()) {
@@ -91,14 +93,20 @@ private function send($item)
9193
$dto = $dto::makeFromExceptionLog($item);
9294
}
9395

96+
if ($item instanceof Notification) {
97+
NotificationFacade::route('slack', $this->guessChannel())->notify(
98+
$item
99+
);
100+
}
101+
94102
$notification = new $class($dto);
95103

96104

97105
if (is_callable($cb = static::$notifyUsing)) {
98106
return call_user_func($cb, $notification);
99107
}
100108

101-
NotificationFacade::route('slack', $this->channel ?? config('developer.slack_dev_hook'))->notify(
109+
NotificationFacade::route('slack', $this->guessChannel())->notify(
102110
$notification
103111
);
104112

@@ -116,4 +124,9 @@ public static function notifyUsing(?callable $notificator)
116124
{
117125
Slack::$notifyUsing = $notificator;
118126
}
127+
128+
private function guessChannel(): ?string
129+
{
130+
return $this->channel ?? config('developer.slack_dev_hook');
131+
}
119132
}

tests/Fixtures/DummyNotification.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Tests\Fixtures;
4+
5+
use App\Domains\Discounts\Models\DiscountUses;
6+
use App\Domains\Items\Models\Item;
7+
use App\Domains\Orders\Models\Order;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Notifications\Messages\SlackMessage;
11+
use Illuminate\Notifications\Notification;
12+
13+
class DummyNotification extends Notification implements ShouldQueue
14+
{
15+
use Queueable;
16+
17+
18+
public function via($notifiable)
19+
{
20+
return ['slack'];
21+
}
22+
23+
public function toSlack()
24+
{
25+
return (new SlackMessage())
26+
->from('test')
27+
->content("test")
28+
->attachment(function ($att) {
29+
$att->title("Title")->content("Content");
30+
});
31+
}
32+
}

tests/Helpers/SlackHelperTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
66
use Binarcode\LaravelDeveloper\Notifications\DevNotification;
77
use Binarcode\LaravelDeveloper\Notifications\Slack;
8+
use Binarcode\LaravelDeveloper\Tests\Fixtures\DummyNotification;
89
use Binarcode\LaravelDeveloper\Tests\TestCase;
910
use Exception;
1011
use Illuminate\Notifications\AnonymousNotifiable;
1112
use Illuminate\Support\Facades\Notification;
13+
use Illuminate\Notifications\Notification as NotificationBase;
1214

1315
class SlackHelperTest extends TestCase
1416
{
@@ -48,8 +50,25 @@ public function test_slack_helper_can_send_throwable_to_slack()
4850

4951
$uuid = ExceptionLog::latest()->first()->id;
5052

51-
Notification::assertSentTo(new AnonymousNotifiable, DevNotification::class, function (DevNotification $class) use ($uuid) {
52-
return $class->notificationDto->attachment_link === "app.test/{$uuid}";
53-
});
53+
Notification::assertSentTo(new AnonymousNotifiable, DevNotification::class,
54+
function (DevNotification $class) use ($uuid) {
55+
return $class->notificationDto->attachment_link === "app.test/{$uuid}";
56+
});
57+
}
58+
59+
public function test_slack_helper_can_send_notifications_to_slack()
60+
{
61+
Notification::fake();
62+
63+
config([
64+
'developer.exception_log_base_url' => 'app.test/{id}',
65+
'developer.slack_dev_hook' => 'https://test.com',
66+
]);
67+
68+
$this->assertInstanceOf(Slack::class, slack(
69+
new DummyNotification()
70+
)->persist());
71+
72+
Notification::assertSentTo(new AnonymousNotifiable, DummyNotification::class);
5473
}
5574
}

0 commit comments

Comments
 (0)