Skip to content

Commit

Permalink
Merge a963854 into 693789b
Browse files Browse the repository at this point in the history
  • Loading branch information
maknz committed May 28, 2016
2 parents 693789b + a963854 commit 4826f19
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,7 +1,6 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@ A simple PHP package for sending messages to [Slack](https://slack.com) with [in

## Requirements

* PHP 5.4 or greater. Compatible with PHP7 and hhvm.
* PHP 5.5, 5.6, 7.0 or HHVM

## Installation

Expand Down
5 changes: 5 additions & 0 deletions src/Client.php
Expand Up @@ -3,6 +3,7 @@
namespace Maknz\Slack;

use GuzzleHttp\Client as Guzzle;
use RuntimeException;

class Client
{
Expand Down Expand Up @@ -371,6 +372,10 @@ public function sendMessage(Message $message)

$encoded = json_encode($payload, JSON_UNESCAPED_UNICODE);

if ($encoded === false) {
throw new RuntimeException(sprintf('JSON encoding error %s: %s', json_last_error(), json_last_error_msg()));
}

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

Expand Down
20 changes: 20 additions & 0 deletions tests/ClientFunctionalTest.php
Expand Up @@ -127,4 +127,24 @@ public function testMessageWithAttachmentsAndFields()

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

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

$this->setExpectedException(RuntimeException::class, 'JSON encoding error');

// Force encoding to ISO-8859-1 so we know we're providing malformed
// encoding to json_encode
$client->send(mb_convert_encoding('æøå', 'ISO-8859-1', 'UTF-8'));
}

protected function getNetworkStubbedClient()
{
$guzzle = Mockery::mock('GuzzleHttp\Client');

$guzzle->shouldReceive('post');

return new Client('http://fake.endpoint', [], $guzzle);
}
}

0 comments on commit 4826f19

Please sign in to comment.