mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-11-25 21:24:22 +00:00
Merge pull request #49 from smotim/feat/guzzle-8-support
feat: support Guzzle 8 and PSR-7 3
This commit is contained in:
@@ -31,7 +31,13 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
php-versions: [ "8.3", "8.4", "8.5" ]
|
||||
name: PHP ${{ matrix.php-versions }} Test on ubuntu-latest
|
||||
guzzle-versions: [ "^7.0", "^8.0" ]
|
||||
include:
|
||||
- guzzle-versions: "^7.0"
|
||||
laravel-version: "^12.0"
|
||||
- guzzle-versions: "^8.0"
|
||||
laravel-version: "^13.0"
|
||||
name: PHP ${{ matrix.php-versions }} / Guzzle ${{ matrix.guzzle-versions }}
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
@@ -44,7 +50,7 @@ jobs:
|
||||
coverage: xdebug
|
||||
|
||||
- name: Install composer dependencies
|
||||
run: composer install --prefer-dist --no-interaction
|
||||
run: composer update --prefer-dist --no-interaction --with "guzzlehttp/guzzle:${{ matrix.guzzle-versions }}" --with "laravel/framework:${{ matrix.laravel-version }}"
|
||||
|
||||
- name: PHPUnit
|
||||
run: ./vendor/bin/phpunit --configuration=phpunit.xml --coverage-text
|
||||
|
||||
+4
-4
@@ -23,8 +23,8 @@
|
||||
"require": {
|
||||
"php": ">=8.3",
|
||||
"ext-json": "*",
|
||||
"guzzlehttp/guzzle": "^6.5.8||^7.0",
|
||||
"guzzlehttp/psr7": "^1.8||^2.0",
|
||||
"guzzlehttp/guzzle": "^6.5.8||^7.0||^8.0",
|
||||
"guzzlehttp/psr7": "^1.8||^2.0||^3.0",
|
||||
"psr/http-client": "^1.0",
|
||||
"psr/http-factory": "^1.0",
|
||||
"psr/http-message": "^1.0||^2.0",
|
||||
@@ -33,10 +33,10 @@
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^3.77",
|
||||
"bushlanov-dev/php-coverage-badger": "^2.1",
|
||||
"laravel/framework": "^12.0",
|
||||
"laravel/framework": "^12.0||^13.0",
|
||||
"mikey179/vfsstream": "^1.6",
|
||||
"mockery/mockery": "^1.6",
|
||||
"orchestra/testbench": "^10.0",
|
||||
"orchestra/testbench": "^10.0||^11.0",
|
||||
"php-mock/php-mock-phpunit": "^2.13",
|
||||
"phpstan/phpstan": "^2.1",
|
||||
"phpunit/phpunit": "^12.0",
|
||||
|
||||
@@ -58,9 +58,9 @@ readonly class MaxBotManager
|
||||
/** @var WebhookHandler $webhookHandler */
|
||||
$webhookHandler = $this->container->make(WebhookHandler::class);
|
||||
|
||||
$headers = array_map(function ($values) {
|
||||
$headers = array_filter(array_map(function ($values) {
|
||||
return array_filter($values, fn($value) => $value !== null);
|
||||
}, $request->headers->all());
|
||||
}, $request->headers->all()));
|
||||
|
||||
$webhookHandler->handle(
|
||||
new ServerRequest(
|
||||
@@ -68,7 +68,7 @@ readonly class MaxBotManager
|
||||
$request->getUri(),
|
||||
$headers,
|
||||
$request->getContent(),
|
||||
$request->getProtocolVersion() ?? '1.1',
|
||||
str_replace('HTTP/', '', $request->getProtocolVersion() ?? '1.1'),
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Tests;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Client;
|
||||
use BushlanovDev\MaxMessengerBot\Exceptions\NetworkException;
|
||||
use BushlanovDev\MaxMessengerBot\Exceptions\RateLimitExceededException;
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Middleware;
|
||||
use GuzzleHttp\Psr7\HttpFactory;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[CoversClass(Client::class)]
|
||||
final class GuzzleCompatibilityTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function sendsJsonThroughTheRealGuzzlePsr18Client(): void
|
||||
{
|
||||
$history = [];
|
||||
$client = $this->client([new Response(200, [], '{"message_id":"message-1"}')], $history);
|
||||
|
||||
self::assertSame(['message_id' => 'message-1'], $client->request('POST', '/messages', ['chat_id' => 123], ['text' => 'Hello']));
|
||||
self::assertCount(1, $history);
|
||||
$request = $history[0]['request'];
|
||||
self::assertSame('POST', $request->getMethod());
|
||||
self::assertSame('chat_id=123', $request->getUri()->getQuery());
|
||||
self::assertSame('test-token', $request->getHeaderLine('Authorization'));
|
||||
self::assertSame('application/json; charset=utf-8', $request->getHeaderLine('Content-Type'));
|
||||
self::assertSame('{"text":"Hello"}', (string) $request->getBody());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function preservesHttpErrorsForTheSdkToHandle(): void
|
||||
{
|
||||
$history = [];
|
||||
$client = $this->client([new Response(429, [], '{"message":"Too many requests"}')], $history);
|
||||
|
||||
$this->expectException(RateLimitExceededException::class);
|
||||
$client->request('POST', '/messages', [], ['text' => 'Hello']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function wrapsTransportFailuresWithoutRetryingAnOutboundMessage(): void
|
||||
{
|
||||
$history = [];
|
||||
$failure = new ConnectException('Connection timed out', new Request('POST', 'https://example.test/messages'));
|
||||
$client = $this->client([$failure, new Response(200, [], '{"success":true}')], $history);
|
||||
|
||||
try {
|
||||
$client->request('POST', '/messages', [], ['text' => 'Hello']);
|
||||
self::fail('The network failure must reach the caller.');
|
||||
} catch (NetworkException $exception) {
|
||||
self::assertSame($failure, $exception->getPrevious());
|
||||
self::assertCount(1, $history);
|
||||
}
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function uploadsAMultipartStreamUsingTheRealPsr7Factory(): void
|
||||
{
|
||||
$history = [];
|
||||
$client = $this->client([new Response(200, [], '{"token":"upload-1"}')], $history);
|
||||
$file = fopen('php://temp', 'w+');
|
||||
fwrite($file, 'file contents');
|
||||
rewind($file);
|
||||
|
||||
try {
|
||||
self::assertSame('{"token":"upload-1"}', $client->multipartUpload('https://example.test/upload', $file, 'ticket.pdf'));
|
||||
} finally {
|
||||
fclose($file);
|
||||
}
|
||||
|
||||
$request = $history[0]['request'];
|
||||
self::assertStringStartsWith('multipart/form-data; boundary=', $request->getHeaderLine('Content-Type'));
|
||||
self::assertStringContainsString('filename="ticket.pdf"', (string) $request->getBody());
|
||||
self::assertStringContainsString('file contents', (string) $request->getBody());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function uploadsEveryResumableChunkExactlyOnce(): void
|
||||
{
|
||||
$history = [];
|
||||
$client = $this->client([new Response(200), new Response(200, [], '{"token":"upload-1"}')], $history);
|
||||
$file = fopen('php://temp', 'w+');
|
||||
fwrite($file, 'abcdef');
|
||||
rewind($file);
|
||||
|
||||
try {
|
||||
self::assertSame('{"token":"upload-1"}', $client->resumableUpload('https://example.test/upload', $file, 'video.mp4', 6, 3));
|
||||
} finally {
|
||||
fclose($file);
|
||||
}
|
||||
|
||||
self::assertCount(2, $history);
|
||||
self::assertSame('bytes 0-2/6', $history[0]['request']->getHeaderLine('Content-Range'));
|
||||
self::assertSame('bytes 3-5/6', $history[1]['request']->getHeaderLine('Content-Range'));
|
||||
self::assertSame('abc', (string) $history[0]['request']->getBody());
|
||||
self::assertSame('def', (string) $history[1]['request']->getBody());
|
||||
}
|
||||
|
||||
private function client(array $queue, array &$history): Client
|
||||
{
|
||||
$stack = HandlerStack::create(new MockHandler($queue));
|
||||
$stack->push(Middleware::history($history));
|
||||
$factory = new HttpFactory();
|
||||
|
||||
return new Client('test-token', new GuzzleClient(['handler' => $stack]), $factory, $factory, 'https://example.test');
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ final class PollingStartCommandTest extends TestCase
|
||||
$this->command->setLaravel($this->container);
|
||||
|
||||
$application = new ConsoleApplication();
|
||||
$application->add($this->command);
|
||||
$application->addCommand($this->command);
|
||||
$commandInApp = $application->find('maxbot:polling:start');
|
||||
$this->tester = new CommandTester($commandInApp);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ final class WebhookListCommandTest extends TestCase
|
||||
$this->command->setLaravel($this->container);
|
||||
|
||||
$application = new ConsoleApplication();
|
||||
$application->add($this->command);
|
||||
$application->addCommand($this->command);
|
||||
$commandInApp = $application->find('maxbot:webhook:list');
|
||||
$this->tester = new CommandTester($commandInApp);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ final class WebhookSubscribeCommandTest extends TestCase
|
||||
$this->command->setLaravel($this->container);
|
||||
|
||||
$application = new ConsoleApplication();
|
||||
$application->add($this->command);
|
||||
$application->addCommand($this->command);
|
||||
$commandInApp = $application->find('maxbot:webhook:subscribe');
|
||||
$this->tester = new CommandTester($commandInApp);
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ final class WebhookUnsubscribeCommandTest extends TestCase
|
||||
$this->command->setLaravel($this->container);
|
||||
|
||||
$application = new ConsoleApplication();
|
||||
$application->add($this->command);
|
||||
$application->addCommand($this->command);
|
||||
$commandInApp = $application->find('maxbot:webhook:unsubscribe');
|
||||
$this->tester = new CommandTester($commandInApp);
|
||||
}
|
||||
|
||||
@@ -103,6 +103,7 @@ final class MaxBotManagerTest extends TestCase
|
||||
$this->container->instance(WebhookHandler::class, $webhookHandler);
|
||||
|
||||
$request = Request::create('/webhook', 'POST', content: '{"update_type":"message_created"}');
|
||||
$request->headers->set('X-Optional-Header', []);
|
||||
|
||||
$realUpdate = new MessageCreatedUpdate(
|
||||
time(),
|
||||
|
||||
Reference in New Issue
Block a user