diff --git a/src/Notifications/Slack.php b/src/Notifications/Slack.php index f67123c..58b0a8c 100644 --- a/src/Notifications/Slack.php +++ b/src/Notifications/Slack.php @@ -2,10 +2,12 @@ namespace Binarcode\LaravelDeveloper\Notifications; +use App\Notifications\OrderPlacedNotification; use Binarcode\LaravelDeveloper\Dtos\DevNotificationDto; use Binarcode\LaravelDeveloper\Models\ExceptionLog; use Binarcode\LaravelDeveloper\Telescope\TelescopeDev; use Binarcode\LaravelDeveloper\Telescope\TelescopeException; +use Illuminate\Notifications\Notification; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Notification as NotificationFacade; use Throwable; @@ -68,7 +70,7 @@ private function send($item) if ($item instanceof Throwable) { if ($this->persist) { $dto = DevNotificationDto::makeFromExceptionLog( - tap(ExceptionLog::makeFromException($item), fn (ExceptionLog $log) => $log->save()) + tap(ExceptionLog::makeFromException($item), fn(ExceptionLog $log) => $log->save()) ); if ($this->telescope && TelescopeDev::allow()) { @@ -91,6 +93,12 @@ private function send($item) $dto = $dto::makeFromExceptionLog($item); } + if ($item instanceof Notification) { + NotificationFacade::route('slack', $this->guessChannel())->notify( + $item + ); + } + $notification = new $class($dto); @@ -98,7 +106,7 @@ private function send($item) return call_user_func($cb, $notification); } - NotificationFacade::route('slack', $this->channel ?? config('developer.slack_dev_hook'))->notify( + NotificationFacade::route('slack', $this->guessChannel())->notify( $notification ); @@ -116,4 +124,9 @@ public static function notifyUsing(?callable $notificator) { Slack::$notifyUsing = $notificator; } + + private function guessChannel(): ?string + { + return $this->channel ?? config('developer.slack_dev_hook'); + } } diff --git a/tests/Fixtures/DummyNotification.php b/tests/Fixtures/DummyNotification.php new file mode 100644 index 0000000..f4ff878 --- /dev/null +++ b/tests/Fixtures/DummyNotification.php @@ -0,0 +1,32 @@ +from('test') + ->content("test") + ->attachment(function ($att) { + $att->title("Title")->content("Content"); + }); + } +} diff --git a/tests/Helpers/SlackHelperTest.php b/tests/Helpers/SlackHelperTest.php index 2e0cee8..e92c95b 100644 --- a/tests/Helpers/SlackHelperTest.php +++ b/tests/Helpers/SlackHelperTest.php @@ -5,10 +5,12 @@ use Binarcode\LaravelDeveloper\Models\ExceptionLog; use Binarcode\LaravelDeveloper\Notifications\DevNotification; use Binarcode\LaravelDeveloper\Notifications\Slack; +use Binarcode\LaravelDeveloper\Tests\Fixtures\DummyNotification; use Binarcode\LaravelDeveloper\Tests\TestCase; use Exception; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Support\Facades\Notification; +use Illuminate\Notifications\Notification as NotificationBase; class SlackHelperTest extends TestCase { @@ -48,8 +50,25 @@ public function test_slack_helper_can_send_throwable_to_slack() $uuid = ExceptionLog::latest()->first()->id; - Notification::assertSentTo(new AnonymousNotifiable, DevNotification::class, function (DevNotification $class) use ($uuid) { - return $class->notificationDto->attachment_link === "app.test/{$uuid}"; - }); + Notification::assertSentTo(new AnonymousNotifiable, DevNotification::class, + function (DevNotification $class) use ($uuid) { + return $class->notificationDto->attachment_link === "app.test/{$uuid}"; + }); + } + + public function test_slack_helper_can_send_notifications_to_slack() + { + Notification::fake(); + + config([ + 'developer.exception_log_base_url' => 'app.test/{id}', + 'developer.slack_dev_hook' => 'https://test.com', + ]); + + $this->assertInstanceOf(Slack::class, slack( + new DummyNotification() + )->persist()); + + Notification::assertSentTo(new AnonymousNotifiable, DummyNotification::class); } }