Fix for file upload, improved tests
This commit is contained in:
parent
527476be18
commit
5d8d79cbcd
@ -504,7 +504,7 @@ class Client
|
||||
*/
|
||||
public function uploadFile(string $filename)
|
||||
{
|
||||
return $this->client->uploadFileViaForm($filename);
|
||||
return $this->client->postFile($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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');
|
||||
|
@ -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'
|
||||
)
|
||||
);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user