Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/Laravel/MaxBotManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ public function handleWebhook(Request $request): JsonResponse|Response
/** @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(
$request->getMethod(),
$request->getUri(),
$headers,
$request->getContent(),
$request->getProtocolVersion() ?? '1.1',
str_replace('HTTP/', '', $request->getProtocolVersion() ?? '1.1'),
)
);

Expand Down
118 changes: 118 additions & 0 deletions tests/GuzzleCompatibilityTest.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
2 changes: 1 addition & 1 deletion tests/Laravel/Commands/PollingStartCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function setUp(): void
$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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Laravel/Commands/WebhookListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function setUp(): void
$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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Laravel/Commands/WebhookSubscribeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function setUp(): void
$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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Laravel/Commands/WebhookUnsubscribeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function setUp(): void
$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);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Laravel/MaxBotManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function handleWebhookReturns200OnSuccess(): void
$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(),
Expand Down
Loading