From 5d8d79cbcda72978da564d2d018cf42b7db46e5b Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 21 Jun 2019 17:51:57 +0300 Subject: [PATCH] Fix for file upload, improved tests --- src/Bot/Client.php | 2 +- src/Bot/HttpClient.php | 10 ++-------- tests/Bot/Test/TestCase.php | 17 +++++++++++++++++ tests/Bot/Tests/CommandsTest.php | 8 +++----- tests/Bot/Tests/DialogsTest.php | 4 ++-- tests/Bot/Tests/MessagesTest.php | 6 +++--- 6 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/Bot/Client.php b/src/Bot/Client.php index 1006032..31f513c 100644 --- a/src/Bot/Client.php +++ b/src/Bot/Client.php @@ -504,7 +504,7 @@ class Client */ public function uploadFile(string $filename) { - return $this->client->uploadFileViaForm($filename); + return $this->client->postFile($filename); } /** diff --git a/src/Bot/HttpClient.php b/src/Bot/HttpClient.php index dea1bc3..1f392c4 100644 --- a/src/Bot/HttpClient.php +++ b/src/Bot/HttpClient.php @@ -182,7 +182,7 @@ class HttpClient * * @throws \Exception */ - public function uploadFileViaForm(string $filename): UploadFileResponse + public function postFile(string $filename): UploadFileResponse { if (!file_exists($filename)) { throw new \InvalidArgumentException("File doesn't exist"); @@ -196,13 +196,7 @@ class HttpClient 'headers' => [ 'X-Bot-Token' => $this->token ], - 'multipart' => [ - [ - 'name' => basename($filename), - 'filename' => basename($filename), - 'contents' => fopen($filename, 'r') - ] - ] + 'body' => fopen($filename, 'r') ] ); } catch (GuzzleException $exception) { diff --git a/tests/Bot/Test/TestCase.php b/tests/Bot/Test/TestCase.php index 1ede82c..c05f10e 100644 --- a/tests/Bot/Test/TestCase.php +++ b/tests/Bot/Test/TestCase.php @@ -114,6 +114,23 @@ class TestCase extends BaseCase } } + /** + * @param int $statusCode + * @param array ...$errors + * + * @return Response + */ + public function getErrorsResponse(int $statusCode = 400, ...$errors) + { + $json = ['errors' => []]; + + foreach ($errors as $error) { + $json['errors'][] = is_string($error) ? $error : null; + } + + return $this->getResponse(json_encode(array_filter($json)), $statusCode); + } + /** * Generate and return empty response * diff --git a/tests/Bot/Tests/CommandsTest.php b/tests/Bot/Tests/CommandsTest.php index b4a9d78..91f059d 100644 --- a/tests/Bot/Tests/CommandsTest.php +++ b/tests/Bot/Tests/CommandsTest.php @@ -13,8 +13,6 @@ namespace RetailCrm\Mg\Bot\Tests; -use Exception; -use InvalidArgumentException; use RetailCrm\Common\Exception\InvalidJsonException; use RetailCrm\Mg\Bot\Model\Request\CommandEditRequest; use RetailCrm\Mg\Bot\Model\Response\ErrorOnlyResponse; @@ -38,13 +36,13 @@ class CommandsTest extends TestCase */ public function testCommandEditException() { - self::expectException(InvalidArgumentException::class); + self::expectException(InvalidJsonException::class); $client = self::getApiClient( null, null, false, - $this->getResponse('EOF',400) + $this->getResponse('EOF', 400) ); $request = new CommandEditRequest(); @@ -59,7 +57,7 @@ class CommandsTest extends TestCase */ public function testCommandDeleteException() { - self::expectException(Exception::class); + self::expectException(\Exception::class); $client = self::getApiClient(); diff --git a/tests/Bot/Tests/DialogsTest.php b/tests/Bot/Tests/DialogsTest.php index 61ed1d5..7cfa32c 100644 --- a/tests/Bot/Tests/DialogsTest.php +++ b/tests/Bot/Tests/DialogsTest.php @@ -41,7 +41,7 @@ class DialogsTest extends TestCase null, null, false, - $this->getResponse('{"errors":["incorrect dialog_id"]}', 400) + $this->getErrorsResponse(400, "incorrect dialog_id") ); $request = new DialogAssignRequest(); @@ -90,7 +90,7 @@ class DialogsTest extends TestCase null, null, false, - $this->getResponse('{"errors":["dialog #2131231231 not found"]}', 404) + $this->getErrorsResponse(404, "dialog #2131231231 not found") ); $client->dialogClose('2131231231'); diff --git a/tests/Bot/Tests/MessagesTest.php b/tests/Bot/Tests/MessagesTest.php index c7b5099..52c2400 100644 --- a/tests/Bot/Tests/MessagesTest.php +++ b/tests/Bot/Tests/MessagesTest.php @@ -39,9 +39,9 @@ class MessagesTest extends TestCase null, null, false, - $this->getResponse( - '{"errors":["chat_id is a required field"]}', - 400 + $this->getErrorsResponse( + 400, + 'chat_id is a required field' ) );