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

Drop PHP 5.4, throw an exception if JSON encoding fails #48

Merged
merged 2 commits into from Jun 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,15 +1,15 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1.0a1
- hhvm

matrix:
allow_failures:
- php: 7.0
- php: 7.1.0a1

before_script:
- composer install --prefer-source --no-interaction
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
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=5.4.0",
"php": ">=5.5.0",
"guzzlehttp/guzzle": "~6.0|~5.0|~4.0",
"ext-mbstring": "*"
},
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);
}
}