Skip to content

Commit

Permalink
Add sticky channel option
Browse files Browse the repository at this point in the history
With this option, all message will be sent to the default channel only.

This is useful for dev environment when you want to concentrate all test message to a specific test channel.
  • Loading branch information
soullivaneuh committed Aug 31, 2016
1 parent e9bab77 commit dc68506
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class Client
*/
protected $channel;

/**
* If set to true, all messages will be sent to the default channel,
* even if you specify another one during the runtime.
*
* This is useful for dev environment.
*
* @var bool
*/
protected $sticky_channel = false;

/**
* The default username to send messages as.
*
Expand Down Expand Up @@ -95,6 +105,10 @@ public function __construct($endpoint, array $attributes = [], Guzzle $guzzle =
$this->setDefaultChannel($attributes['channel']);
}

if (isset($attributes['sticky_channel'])) {
$this->setStickyChannel($attributes['sticky_channel']);
}

if (isset($attributes['username'])) {
$this->setDefaultUsername($attributes['username']);
}
Expand Down Expand Up @@ -181,6 +195,22 @@ public function setDefaultChannel($channel)
$this->channel = $channel;
}

/**
* @return bool
*/
public function isStickyChannel()
{
return $this->sticky_channel;
}

/**
* @param bool $sticky_channel
*/
public function setStickyChannel($sticky_channel)
{
$this->sticky_channel = $sticky_channel;
}

/**
* Get the default username messages will be created for.
*
Expand Down Expand Up @@ -385,7 +415,7 @@ public function preparePayload(Message $message)
{
$payload = [
'text' => $message->getText(),
'channel' => $message->getChannel(),
'channel' => $this->isStickyChannel() ? $this->channel : $message->getChannel(),
'username' => $message->getUsername(),
'link_names' => $this->getLinkNames() ? 1 : 0,
'unfurl_links' => $this->getUnfurlLinks(),
Expand Down
25 changes: 25 additions & 0 deletions tests/ClientFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,31 @@ public function testMessageWithAttachmentsAndActions()
$this->assertEquals($expectedHttpData, $payload);
}

public function testMessageWithStickyChannel()
{
$expectedHttpData = [
'username' => 'Archer',
'channel' => 'test',
'text' => 'Message',
'link_names' => 0,
'unfurl_links' => false,
'unfurl_media' => true,
'mrkdwn' => true,
'attachments' => [],
];

$client = new Client('http://fake.endpoint', [
'channel' => 'test',
'sticky_channel' => true,
]);

$message = $client->to('@regan')->from('Archer')->setText('Message');

$payload = $client->preparePayload($message);

$this->assertEquals($expectedHttpData, $payload);
}

public function testBadEncodingThrowsException()
{
$client = $this->getNetworkStubbedClient();
Expand Down

0 comments on commit dc68506

Please sign in to comment.