From f1d63786e2379c53c77450aebbebf49979ae94ec Mon Sep 17 00:00:00 2001 From: maknz Date: Sat, 28 May 2016 17:29:13 +1200 Subject: [PATCH 1/2] Throw an exception if JSON encoding fails --- src/Client.php | 5 +++++ tests/ClientFunctionalTest.php | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Client.php b/src/Client.php index c989462..fad18e1 100644 --- a/src/Client.php +++ b/src/Client.php @@ -3,6 +3,7 @@ namespace Maknz\Slack; use GuzzleHttp\Client as Guzzle; +use RuntimeException; class Client { @@ -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]); } diff --git a/tests/ClientFunctionalTest.php b/tests/ClientFunctionalTest.php index 3f76f9c..8117379 100644 --- a/tests/ClientFunctionalTest.php +++ b/tests/ClientFunctionalTest.php @@ -127,4 +127,24 @@ public function testMessageWithAttachmentsAndFields() $this->assertEquals($expectedHttpData, $payload); } + + public function testBadEncodingThrowsException() + { + $client = $this->getNetworkStubbedClient(); + + $this->setExpectedException('RuntimeException', '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); + } } From a9638546244dfa1d56070ee0cd4f9482635aef98 Mon Sep 17 00:00:00 2001 From: maknz Date: Sat, 28 May 2016 17:55:08 +1200 Subject: [PATCH 2/2] Drop PHP 5.4 support --- .travis.yml | 1 - README.md | 2 +- tests/ClientFunctionalTest.php | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 198804d..9f1e79a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.4 - 5.5 - 5.6 - 7.0 diff --git a/README.md b/README.md index 1bec528..087b51f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tests/ClientFunctionalTest.php b/tests/ClientFunctionalTest.php index 8117379..9b618d7 100644 --- a/tests/ClientFunctionalTest.php +++ b/tests/ClientFunctionalTest.php @@ -132,7 +132,7 @@ public function testBadEncodingThrowsException() { $client = $this->getNetworkStubbedClient(); - $this->setExpectedException('RuntimeException', 'JSON encoding error'); + $this->setExpectedException(RuntimeException::class, 'JSON encoding error'); // Force encoding to ISO-8859-1 so we know we're providing malformed // encoding to json_encode