Skip to content
Draft
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
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
"php": "^7.3|^8.0",
"ext-curl": "*",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.2",
"psr/log": "^1.0|^2.0|^3.0"
"guzzlehttp/guzzle": "^7.8.2 || ^8.0",
"guzzlehttp/psr7": "^2.6.3 || ^3.0",
"guzzlehttp/promises": "^2.0.3 || ^3.0",
"psr/http-client": "^1.0",
"psr/log": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
Expand Down
24 changes: 16 additions & 8 deletions src/Pusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Pusher;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -721,7 +721,7 @@ public function get(string $path, array $params = [], $associative = false)
'X-Pusher-Library' => 'pusher-http-php ' . self::$VERSION
];

$response = $this->client->get(ltrim($path, '/'), [
$response = $this->client->request('GET', ltrim($path, '/'), [
'query' => $signature,
'http_errors' => false,
'headers' => $headers,
Expand Down Expand Up @@ -773,16 +773,16 @@ public function post(string $path, $body, array $params = [])
];

try {
$response = $this->client->post(ltrim($path, '/'), [
$response = $this->client->request('POST', ltrim($path, '/'), [
'query' => $params_with_signature,
'body' => $body,
'http_errors' => false,
'headers' => $headers,
'base_uri' => $this->channels_url_prefix(),
'timeout' => $this->settings['timeout']
]);
} catch (ConnectException $e) {
throw new ApiErrorException($e->getMessage());
} catch (NetworkExceptionInterface $e) {
throw new ApiErrorException($e->getMessage(), 0, $e);
}

$status = $response->getStatusCode();
Expand Down Expand Up @@ -824,7 +824,7 @@ public function postAsync(string $path, $body, array $params = []): PromiseInter
'X-Pusher-Library' => 'pusher-http-php ' . self::$VERSION
];

return $this->client->postAsync(ltrim($path, '/'), [
return $this->client->requestAsync('POST', ltrim($path, '/'), [
'query' => $params_with_signature,
'body' => $body,
'http_errors' => false,
Expand All @@ -846,8 +846,16 @@ public function postAsync(string $path, $body, array $params = []): PromiseInter
}

return $response_body;
}, function (ConnectException $e) {
throw new ApiErrorException($e->getMessage());
}, function ($reason) {
if ($reason instanceof NetworkExceptionInterface) {
throw new ApiErrorException($reason->getMessage(), 0, $reason);
}

if ($reason instanceof \Throwable) {
throw $reason;
}

throw new PusherException('HTTP request failed.');
});
}

Expand Down
25 changes: 25 additions & 0 deletions tests/unit/ChannelInfoUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace unit;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Pusher\Pusher;
use stdClass;

class ChannelInfoUnitTest extends TestCase
{
Expand Down Expand Up @@ -44,4 +47,26 @@ public function testTrailingColonNLChannelThrowsException(): void

$this->pusher->get_channel_info('test_channel\n:');
}

public function testGetChannelInfoUsesClientInterfaceRequest(): void
{
$httpClient = $this->createMock(ClientInterface::class);

$httpClient->expects(self::once())
->method('request')
->with(
'GET',
'apps/appid/channels/test_channel',
self::callback(function (array $options): bool {
return $options['http_errors'] === false
&& $options['base_uri'] === 'https://api-test1.pusher.com:443'
&& $options['headers']['Content-Type'] === 'application/json';
})
)
->willReturn(new Response(200, [], '{}'));

$pusher = new Pusher('auth-key', 'secret', 'appid', ['cluster' => 'test1'], $httpClient);

self::assertEquals(new stdClass(), $pusher->getChannelInfo('test_channel'));
}
}
146 changes: 145 additions & 1 deletion tests/unit/TerminateUserConnectionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
namespace unit;

use GuzzleHttp;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Promise\RejectedPromise;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;
use PHPUnit\Framework\TestCase;
use Pusher\ApiErrorException;
use Pusher\Pusher;
use Pusher\PusherException;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\RequestInterface;
use RuntimeException;
use stdClass;

class TerminateUserConnectionsUnitTest extends TestCase
Expand Down Expand Up @@ -76,4 +83,141 @@ public function testTerminateUserConectionsAsyncError(): void
$this->expectException(ApiErrorException::class);
$pusher->terminateUserConnectionsAsync("123")->wait();
}

public function testTerminateUserConnectionsUsesClientInterfaceRequest(): void
{
$httpClient = $this->createMock(ClientInterface::class);

$httpClient->expects(self::once())
->method('request')
->with(
'POST',
'apps/appid/users/123/terminate_connections',
self::callback(function (array $options): bool {
return $options['body'] === '{}'
&& $options['http_errors'] === false
&& $options['base_uri'] === 'https://api-test1.pusher.com:443'
&& $options['headers']['Content-Type'] === 'application/json';
})
)
->willReturn(new Response(200, [], '{}'));

$pusher = new Pusher('auth-key', 'secret', 'appid', ['cluster' => 'test1'], $httpClient);

self::assertEquals(new stdClass(), $pusher->terminateUserConnections('123'));
}

public function testTerminateUserConnectionsAsyncUsesClientInterfaceRequestAsync(): void
{
$httpClient = $this->createMock(ClientInterface::class);

$httpClient->expects(self::once())
->method('requestAsync')
->with(
'POST',
'apps/appid/users/123/terminate_connections',
self::callback(function (array $options): bool {
return $options['body'] === '{}'
&& $options['http_errors'] === false
&& $options['base_uri'] === 'https://api-test1.pusher.com:443'
&& $options['headers']['Content-Type'] === 'application/json';
})
)
->willReturn(new FulfilledPromise(new Response(200, [], '{}')));

$pusher = new Pusher('auth-key', 'secret', 'appid', ['cluster' => 'test1'], $httpClient);

self::assertEquals(new stdClass(), $pusher->terminateUserConnectionsAsync('123')->wait());
}

public function testTerminateUserConnectionsAsyncMapsNetworkExceptionToApiErrorException(): void
{
$httpClient = $this->createMock(ClientInterface::class);
$networkException = new class ('connection failed', new Request('POST', 'https://example.com')) extends RuntimeException implements NetworkExceptionInterface {
private $request;

public function __construct(string $message, RequestInterface $request)
{
parent::__construct($message);
$this->request = $request;
}

public function getRequest(): RequestInterface
{
return $this->request;
}
};

$httpClient->method('requestAsync')
->willReturn(new RejectedPromise($networkException));

$pusher = new Pusher('auth-key', 'secret', 'appid', ['cluster' => 'test1'], $httpClient);

try {
$pusher->terminateUserConnectionsAsync('123')->wait();
$this->fail('An exception should have been thrown.');
} catch (ApiErrorException $exception) {
self::assertSame('connection failed', $exception->getMessage());
self::assertSame($networkException, $exception->getPrevious());
}
}

public function testTerminateUserConnectionsAsyncMapsConnectExceptionToApiErrorException(): void
{
$httpClient = $this->createMock(ClientInterface::class);
$connectException = new ConnectException(
'connection failed',
new Request('POST', 'https://example.com')
);

$httpClient->method('requestAsync')
->willReturn(new RejectedPromise($connectException));

$pusher = new Pusher('auth-key', 'secret', 'appid', ['cluster' => 'test1'], $httpClient);

try {
$pusher->terminateUserConnectionsAsync('123')->wait();
$this->fail('An exception should have been thrown.');
} catch (ApiErrorException $exception) {
self::assertSame('connection failed', $exception->getMessage());
self::assertSame($connectException, $exception->getPrevious());
}
}

public function testTerminateUserConnectionsMapsConnectExceptionToApiErrorException(): void
{
$httpClient = $this->createMock(ClientInterface::class);
$connectException = new ConnectException(
'connection failed',
new Request('POST', 'https://example.com')
);

$httpClient->method('request')
->willThrowException($connectException);

$pusher = new Pusher('auth-key', 'secret', 'appid', ['cluster' => 'test1'], $httpClient);

try {
$pusher->terminateUserConnections('123');
$this->fail('An exception should have been thrown.');
} catch (ApiErrorException $exception) {
self::assertSame('connection failed', $exception->getMessage());
self::assertSame($connectException, $exception->getPrevious());
}
}

public function testTerminateUserConnectionsAsyncRethrowsNonConnectThrowable(): void
{
$httpClient = $this->createMock(ClientInterface::class);

$httpClient->method('requestAsync')
->willReturn(new RejectedPromise(new RuntimeException('boom')));

$pusher = new Pusher('auth-key', 'secret', 'appid', ['cluster' => 'test1'], $httpClient);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('boom');

$pusher->terminateUserConnectionsAsync('123')->wait();
}
}
Loading