Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #19 from djunehor/issue-16
Browse files Browse the repository at this point in the history
  • Loading branch information
gpressutto5 authored Apr 22, 2021
2 parents 47748bd + 92d88de commit ce302be
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ class User extends Model
\Slack::to(User::where('verified', true))->send('Sending message to all verified users!');
```

- Send message by specifying webhook:

```php
\Slack::to('#finance')->webhook('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX')->send('Hey, finance channel! A new order was created just now!');
```

## Testing

When testing you can easily mock the Slack service by calling
Expand Down
31 changes: 24 additions & 7 deletions src/Slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,25 @@ public function __construct(array $config)
$this->config = $config;
}

/**
* Allows user specify webhook to use
* for current instance.
*
* @param string $url
*
* @return $this
*/
public function webhook(string $url): self
{
$this->anonymousNotifiable = Notification::route('slack', $url);

return $this;
}

/**
* Set the recipients of the message.
*
* @param object|array|string $recipient
* @param object|array|string $recipient
*
* @return $this
*/
Expand All @@ -59,13 +74,15 @@ public function to($recipient): self

$recipients = is_array($recipient) ? $recipient : func_get_args();

$this->recipients = array_map(function ($recipient) {
if (is_object($recipient)) {
return $recipient->slack_channel;
}
$this->recipients = array_map(
function ($recipient) {
if (is_object($recipient)) {
return $recipient->slack_channel;
}

return $recipient;
}, $recipients);
return $recipient;
}, $recipients
);

return $this;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/SendMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ public function testSendMessageToAChannel()
$this->assertEquals('RANDOM', $slackMessageSent->content);
}

public function testSendMessageToAChannelWithSpecifiedWebHook()
{
$notification = Notification::fake();

\Slack::to('#random')->webhook('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX')->send('RANDOM');

$notification->assertSentTo(new AnonymousNotifiable(), SimpleSlack::class, 1);
$slackMessageSent = $notification->sent(new AnonymousNotifiable(), SimpleSlack::class)->first()->toSlack();
$this->assertEquals('#random', $slackMessageSent->channel);
$this->assertEquals('RANDOM', $slackMessageSent->content);
}

public function testSendMessageToAUserWithSpecifiedConfig()
{
$notification = Notification::fake();

\Pressutto\LaravelSlack\Facades\Slack::to('#fashion')->webhook('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX')->send('RANDOM');

$notification->assertSentTo(new AnonymousNotifiable(), SimpleSlack::class, 1);
$slackMessageSent = $notification->sent(new AnonymousNotifiable(), SimpleSlack::class)->first()->toSlack();
$this->assertEquals('#fashion', $slackMessageSent->channel);
$this->assertEquals('RANDOM', $slackMessageSent->content);
}

public function testSendMessageToAnUser()
{
$notification = Notification::fake();
Expand Down

0 comments on commit ce302be

Please sign in to comment.