Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic message endpoints feature and improved test coverage. Fixes #43 #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ $client->send('Hello world!');
// or the Laravel facade
Slack::send('Hello world!');
```
#### Sending a message to a non-default endpoint
```php
// With an instantiated client
$client->endpoint('http://message.slack.endpoint')->send('Are we rich yet?');

// or the Laravel facade
Slack::endpoint('http://message.slack.endpoint')->send('Are we rich yet?');
```

#### Sending a message to a non-default channel
```php
Expand Down Expand Up @@ -286,4 +294,4 @@ $attachment->setFields($bigArrayOfFields);

## Contributing

If you're having problems, spot a bug, or have a feature suggestion, please log and issue on Github. If you'd like to have a crack yourself, fork the package and make a pull request. Please include tests for any added or changed functionality. If it's a bug, include a regression test.
If you're having problems, spot a bug, or have a feature suggestion, please log an issue on Github. If you'd like to have a crack yourself, fork the package and make a pull request. Please include tests for any added or changed functionality. If it's a bug, include a regression test.
6 changes: 5 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ public function createMessage()
{
$message = new Message($this);

$message->setEndpoint($this->getEndpoint());

$message->setChannel($this->getDefaultChannel());

$message->setUsername($this->getDefaultUsername());
Expand All @@ -354,7 +356,9 @@ public function sendMessage(Message $message)

$encoded = json_encode($payload, JSON_UNESCAPED_UNICODE);

$this->guzzle->post($this->endpoint, ['body' => $encoded]);
$endpoint = $message->getEndpoint();

$this->guzzle->post($endpoint, ['body' => $encoded]);
}

/**
Expand Down
50 changes: 48 additions & 2 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class Message {
*/
protected $text;

/**
* The target endpoint the message should be sent to
*
* @var string
*/
protected $endpoint;

/**
* The channel the message should be sent to
*
Expand Down Expand Up @@ -116,6 +123,34 @@ public function setText($text)
return $this;
}

/**
* Get the message endpoint
*
* @return string
*/
public function getEndpoint()
{
return $this->endpoint;
}

/**
* Set the message endpoint
*
* @param string $endpoint
* @return $this
*/
public function setEndpoint($endpoint)
{
if (empty($endpoint) || !is_string($endpoint))
{
throw new InvalidArgumentException('The message endpoint must be a non-empty string');
}

$this->endpoint = $endpoint;

return $this;
}

/**
* Get the channel we will post to
*
Expand Down Expand Up @@ -299,6 +334,17 @@ public function from($username)
return $this;
}

/**
* Change the endpoint the post will be made to
*
* @param string $endpoint
* @return $this
*/
public function endpoint($endpoint)
{
return $this->setEndpoint($endpoint);
}

/**
* Change the channel the post will be made to
*
Expand Down Expand Up @@ -345,7 +391,7 @@ public function attach($attachment)
$attachmentObject = new Attachment($attachment);

if ( ! isset($attachment['mrkdwn_in']))
{
{
$attachmentObject->setMarkdownFields($this->getMarkdownInAttachments());
}

Expand Down Expand Up @@ -410,4 +456,4 @@ public function send($text = null)
$this->client->sendMessage($this);
}

}
}
13 changes: 12 additions & 1 deletion tests/AttachmentUnitTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Maknz\Slack\Client;
use Maknz\Slack\Attachment;
use Maknz\Slack\AttachmentField;

Expand Down Expand Up @@ -134,6 +133,18 @@ public function testAddFieldAsObject()
$this->assertSame($f, $fields[0]);
}

public function testAddFieldInvalidArgumentException()
{
$a = new Attachment([
'fallback' => 'Fallback',
'text' => 'Text'
]);

$this->setExpectedException('InvalidArgumentException');

$a->addField('this is wrong');
}

public function testSetFields()
{
$a = new Attachment([
Expand Down
23 changes: 23 additions & 0 deletions tests/ClientFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ public function testPlainMessage()

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

public function testMessageWithEmoji()
{
$expectedHttpData = [
'username' => 'Archer',
'channel' => '@regan',
'text' => 'Message',
'link_names' => false,
'unfurl_links' => false,
'unfurl_media' => true,
'mrkdwn' => true,
'attachments' => [],
'icon_emoji' => ':ghost:'
];

$client = new Client('http://fake.endpoint');

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

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

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

public function testMessageWithAttachments()
{
Expand Down
62 changes: 61 additions & 1 deletion tests/ClientUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public function testInstantiationWithDefaults()
$this->assertSame($defaults['markdown_in_attachments'], $client->getMarkdownInAttachments());
}

public function testSetEndpoint()
{
$client = new Client('http://fake.endpoint');

$endpoint = 'http://bogus.endpoint';

$client->setEndpoint($endpoint);

$this->assertSame($endpoint, $client->getEndpoint());
}

public function testCreateMessage()
{
$defaults = [
Expand All @@ -59,6 +70,8 @@ public function testCreateMessage()

$this->assertInstanceOf('Maknz\Slack\Message', $message);

$this->assertSame($client->getEndpoint(), $message->getEndpoint());

$this->assertSame($client->getDefaultChannel(), $message->getChannel());

$this->assertSame($client->getDefaultUsername(), $message->getUsername());
Expand All @@ -76,5 +89,52 @@ public function testWildcardCallToMessage()

$this->assertSame('@regan', $message->getChannel());
}

public function testSendMessage()
{
$endpoint = 'http://fake.endpoint';

$body = '{"text":"Message","channel":"@regan","username":"Archer","link_names":0,"unfurl_links":false,"unfurl_media":true,"mrkdwn":true,"attachments":[]}';

$messageMock = $this->getMessage();
$messageMock->shouldReceive('getEndpoint')->once()->andReturn($endpoint);

$guzzleMock = \Mockery::mock('GuzzleHttp\Client');
$guzzleMock->shouldReceive('post')->once()->with($endpoint, ['body' => $body]);

$client = new Client($endpoint, [], $guzzleMock);
$client->sendMessage($messageMock);
}

public function testSendMessageWithEndpoint()
{
$endpoint = 'http://fake.endpoint';
$messageEndpoint = 'http://bogus.endpoint';

$body = '{"text":"Message","channel":"@regan","username":"Archer","link_names":0,"unfurl_links":false,"unfurl_media":true,"mrkdwn":true,"attachments":[]}';

$messageMock = $this->getMessage();
$messageMock->shouldReceive('getEndpoint')->once()->andReturn($messageEndpoint);

$guzzleMock = \Mockery::mock('GuzzleHttp\Client');
$guzzleMock->shouldReceive('post')->once()->with($messageEndpoint, ['body' => $body]);

$client = new Client($endpoint, [], $guzzleMock);
$client->sendMessage($messageMock);
}

protected function getMessage()
{
$messageMock = \Mockery::mock('Maknz\Slack\Message');

$messageMock->shouldReceive('getText')->once()->andReturn('Message');
$messageMock->shouldReceive('getChannel')->once()->andReturn('@regan');
$messageMock->shouldReceive('getUsername')->once()->andReturn('Archer');
$messageMock->shouldReceive('getAllowMarkdown')->once()->andReturn(true);
$messageMock->shouldReceive('getIcon')->once()->andReturn(null);
$messageMock->shouldReceive('getAttachments')->once()->andReturn([]);

return $messageMock;
}

}
}
110 changes: 108 additions & 2 deletions tests/MessageUnitTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Maknz\Slack\Client;
use Maknz\Slack\Message;
use Maknz\Slack\Attachment;

Expand Down Expand Up @@ -158,7 +157,27 @@ public function testSetAttachmentsWipesExistingAttachments()

$this->assertEquals('a', $message->getAttachments()[0]->getFallback());
}

public function testAttachInvalidArgumentException()
{
$message = $this->getMessage();

$this->setExpectedException('InvalidArgumentException');

$message->attach('this is wrong');
}

public function testWithIconToEmoji()
{
$message = $this->getMessage();

$message->withIcon(':ghost:');

$this->assertEquals(Message::ICON_TYPE_EMOJI, $message->getIconType());

$this->assertEquals(':ghost:', $message->getIcon());
}

public function testSetIconToEmoji()
{
$message = $this->getMessage();
Expand All @@ -180,10 +199,97 @@ public function testSetIconToUrl()

$this->assertEquals('http://www.fake.com/someimage.png', $message->getIcon());
}

public function testEndpoint()
{
$message = $this->getMessage();

$endpoint = 'http://fake.endpoint';

$fluent = $message->endpoint($endpoint);

$this->assertSame($endpoint, $message->getEndpoint());
$this->assertTrue($fluent instanceof \Maknz\Slack\Message);
}

public function testSetEndpoint()
{
$message = $this->getMessage();

$endpoint = 'http://fake.endpoint';

$fluent = $message->setEndpoint($endpoint);

$this->assertSame($endpoint, $message->getEndpoint());
$this->assertTrue($fluent instanceof \Maknz\Slack\Message);
}

public function testSetEndpointEmptyEndpointException()
{
$message = $this->getMessage();

$endpoint = '';

$this->setExpectedException('InvalidArgumentException');

$message->setEndpoint($endpoint);
}

public function testSetEndpointNonStringEndpointException()
{
$message = $this->getMessage();

$endpoint = 12345;

$this->setExpectedException('InvalidArgumentException');

$message->setEndpoint($endpoint);
}

public function testEnableMarkdown()
{
$message = $this->getMessage();

$message->enableMarkdown();

$this->assertTrue($message->getAllowMarkdown());
}

public function testDisableMarkdown()
{
$message = $this->getMessage();

$message->disableMarkdown();

$this->assertFalse($message->getAllowMarkdown());
}

public function testSend()
{
$clientMock = \Mockery::mock('Maknz\Slack\Client');
$clientMock->shouldReceive('sendMessage')->once();

$message = new Message($clientMock);

$message->send();
}

public function testSendWithText()
{
$text = 'some text';

$clientMock = \Mockery::mock('Maknz\Slack\Client');
$clientMock->shouldReceive('sendMessage')->once();

$message = \Mockery::mock('Maknz\Slack\Message[setText]', [$clientMock]);
$message->shouldReceive('setText')->once()->with($text);

$message->send($text);
}

protected function getMessage()
{
return new Message(Mockery::mock('Maknz\Slack\Client'));
}

}
}