From 78e73e60a0a1efd41b5a9af1f43690fce820f920 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 27 Jun 2024 21:25:44 +0300 Subject: [PATCH] Fix incorrect array values addition to GET request in `PsrTelegramClient` --- CHANGELOG.md | 1 + src/Client/PsrTelegramClient.php | 7 ++++++- tests/Client/PsrTelegramClientTest.php | 7 +++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6659319..4ae7697 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ `Vjik\TelegramBot\Api\Type\Update` namespace. - Chg #30: Remove `TelegramRequestWithFilesInterface`. - Bug #48: Fix incorrect string values addition to POST request with files in `PsrTelegramClient`. +- Bug #53: Fix incorrect array values addition to GET request in `PsrTelegramClient`. ## 0.1.0 June 10, 2024 diff --git a/src/Client/PsrTelegramClient.php b/src/Client/PsrTelegramClient.php index 000aff3..dfc81e5 100644 --- a/src/Client/PsrTelegramClient.php +++ b/src/Client/PsrTelegramClient.php @@ -93,9 +93,14 @@ private function createPostRequest(TelegramRequestInterface $request): HttpReque private function createGetRequest(TelegramRequestInterface $request): HttpRequestInterface { + $queryParameters = []; + foreach ($request->getData() as $key => $value) { + $queryParameters[$key] = is_scalar($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR); + } + return $this->httpRequestFactory->createRequest( 'GET', - $this->apiUrlGenerator->generate($request->getApiMethod(), $request->getData()), + $this->apiUrlGenerator->generate($request->getApiMethod(), $queryParameters), ); } } diff --git a/tests/Client/PsrTelegramClientTest.php b/tests/Client/PsrTelegramClientTest.php index 6e23888..07fd653 100644 --- a/tests/Client/PsrTelegramClientTest.php +++ b/tests/Client/PsrTelegramClientTest.php @@ -33,7 +33,7 @@ public function testGet(): void $httpRequestFactory ->expects($this->once()) ->method('createRequest') - ->with('GET', 'https://api.telegram.org/bot04062024/getMe?key=value') + ->with('GET', 'https://api.telegram.org/bot04062024/getMe?key=value&array=%5B1%2C%22test%22%5D') ->willReturn($httpRequest); $client = new PsrTelegramClient( @@ -43,7 +43,10 @@ public function testGet(): void new StreamFactory(), ); - $response = $client->send(new TelegramRequest(HttpMethod::GET, 'getMe', ['key' => 'value'])); + $response = $client->send(new TelegramRequest(HttpMethod::GET, 'getMe', [ + 'key' => 'value', + 'array' => [1, 'test'], + ])); $this->assertSame(201, $response->statusCode); }