From af1d93c125d9fc11e07309ff4a9187e455066505 Mon Sep 17 00:00:00 2001 From: Timofey <57951811+smotim@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:52:48 +0500 Subject: [PATCH 1/2] feat: support Guzzle 8 and PSR-7 3 --- .github/workflows/ci.yml | 14 ++- composer.json | 8 +- src/Laravel/MaxBotManager.php | 6 +- tests/GuzzleCompatibilityTest.php | 118 ++++++++++++++++++ .../Commands/PollingStartCommandTest.php | 2 +- .../Commands/WebhookListCommandTest.php | 2 +- .../Commands/WebhookSubscribeCommandTest.php | 2 +- .../WebhookUnsubscribeCommandTest.php | 2 +- tests/Laravel/MaxBotManagerTest.php | 1 + 9 files changed, 140 insertions(+), 15 deletions(-) create mode 100644 tests/GuzzleCompatibilityTest.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index accba3d..8399348 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -31,11 +31,17 @@ 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 - uses: actions/checkout@v2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -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 diff --git a/composer.json b/composer.json index 5110af4..7be95f0 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Laravel/MaxBotManager.php b/src/Laravel/MaxBotManager.php index 83fcfbf..cb2caa2 100644 --- a/src/Laravel/MaxBotManager.php +++ b/src/Laravel/MaxBotManager.php @@ -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'), ) ); diff --git a/tests/GuzzleCompatibilityTest.php b/tests/GuzzleCompatibilityTest.php new file mode 100644 index 0000000..1a2f92d --- /dev/null +++ b/tests/GuzzleCompatibilityTest.php @@ -0,0 +1,118 @@ +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'); + } +} diff --git a/tests/Laravel/Commands/PollingStartCommandTest.php b/tests/Laravel/Commands/PollingStartCommandTest.php index 0050b06..c9133c8 100644 --- a/tests/Laravel/Commands/PollingStartCommandTest.php +++ b/tests/Laravel/Commands/PollingStartCommandTest.php @@ -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); } diff --git a/tests/Laravel/Commands/WebhookListCommandTest.php b/tests/Laravel/Commands/WebhookListCommandTest.php index 8e54143..7a624e6 100644 --- a/tests/Laravel/Commands/WebhookListCommandTest.php +++ b/tests/Laravel/Commands/WebhookListCommandTest.php @@ -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); } diff --git a/tests/Laravel/Commands/WebhookSubscribeCommandTest.php b/tests/Laravel/Commands/WebhookSubscribeCommandTest.php index 33cc26b..8009fd0 100644 --- a/tests/Laravel/Commands/WebhookSubscribeCommandTest.php +++ b/tests/Laravel/Commands/WebhookSubscribeCommandTest.php @@ -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); } diff --git a/tests/Laravel/Commands/WebhookUnsubscribeCommandTest.php b/tests/Laravel/Commands/WebhookUnsubscribeCommandTest.php index f304310..5c40447 100644 --- a/tests/Laravel/Commands/WebhookUnsubscribeCommandTest.php +++ b/tests/Laravel/Commands/WebhookUnsubscribeCommandTest.php @@ -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); } diff --git a/tests/Laravel/MaxBotManagerTest.php b/tests/Laravel/MaxBotManagerTest.php index 88f6396..d9f903c 100644 --- a/tests/Laravel/MaxBotManagerTest.php +++ b/tests/Laravel/MaxBotManagerTest.php @@ -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(), From f85391a164cc6b3e89c31c4d128c69b429840228 Mon Sep 17 00:00:00 2001 From: Timofey <57951811+smotim@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:59:36 +0500 Subject: [PATCH 2/2] chore(ci): preserve the existing checkout action --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8399348..b799156 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@v2 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -41,7 +41,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@v2 - name: Setup PHP uses: shivammathur/setup-php@v2