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)
|
public function uploadFile(string $filename)
|
||||||
{
|
{
|
||||||
return $this->client->uploadFileViaForm($filename);
|
return $this->client->postFile($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -182,7 +182,7 @@ class HttpClient
|
|||||||
*
|
*
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function uploadFileViaForm(string $filename): UploadFileResponse
|
public function postFile(string $filename): UploadFileResponse
|
||||||
{
|
{
|
||||||
if (!file_exists($filename)) {
|
if (!file_exists($filename)) {
|
||||||
throw new \InvalidArgumentException("File doesn't exist");
|
throw new \InvalidArgumentException("File doesn't exist");
|
||||||
@ -196,13 +196,7 @@ class HttpClient
|
|||||||
'headers' => [
|
'headers' => [
|
||||||
'X-Bot-Token' => $this->token
|
'X-Bot-Token' => $this->token
|
||||||
],
|
],
|
||||||
'multipart' => [
|
'body' => fopen($filename, 'r')
|
||||||
[
|
|
||||||
'name' => basename($filename),
|
|
||||||
'filename' => basename($filename),
|
|
||||||
'contents' => fopen($filename, 'r')
|
|
||||||
]
|
|
||||||
]
|
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
} catch (GuzzleException $exception) {
|
} 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
|
* Generate and return empty response
|
||||||
*
|
*
|
||||||
|
@ -13,8 +13,6 @@
|
|||||||
|
|
||||||
namespace RetailCrm\Mg\Bot\Tests;
|
namespace RetailCrm\Mg\Bot\Tests;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use RetailCrm\Common\Exception\InvalidJsonException;
|
use RetailCrm\Common\Exception\InvalidJsonException;
|
||||||
use RetailCrm\Mg\Bot\Model\Request\CommandEditRequest;
|
use RetailCrm\Mg\Bot\Model\Request\CommandEditRequest;
|
||||||
use RetailCrm\Mg\Bot\Model\Response\ErrorOnlyResponse;
|
use RetailCrm\Mg\Bot\Model\Response\ErrorOnlyResponse;
|
||||||
@ -38,7 +36,7 @@ class CommandsTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testCommandEditException()
|
public function testCommandEditException()
|
||||||
{
|
{
|
||||||
self::expectException(InvalidArgumentException::class);
|
self::expectException(InvalidJsonException::class);
|
||||||
|
|
||||||
$client = self::getApiClient(
|
$client = self::getApiClient(
|
||||||
null,
|
null,
|
||||||
@ -59,7 +57,7 @@ class CommandsTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testCommandDeleteException()
|
public function testCommandDeleteException()
|
||||||
{
|
{
|
||||||
self::expectException(Exception::class);
|
self::expectException(\Exception::class);
|
||||||
|
|
||||||
$client = self::getApiClient();
|
$client = self::getApiClient();
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ class DialogsTest extends TestCase
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
false,
|
false,
|
||||||
$this->getResponse('{"errors":["incorrect dialog_id"]}', 400)
|
$this->getErrorsResponse(400, "incorrect dialog_id")
|
||||||
);
|
);
|
||||||
|
|
||||||
$request = new DialogAssignRequest();
|
$request = new DialogAssignRequest();
|
||||||
@ -90,7 +90,7 @@ class DialogsTest extends TestCase
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
false,
|
false,
|
||||||
$this->getResponse('{"errors":["dialog #2131231231 not found"]}', 404)
|
$this->getErrorsResponse(404, "dialog #2131231231 not found")
|
||||||
);
|
);
|
||||||
|
|
||||||
$client->dialogClose('2131231231');
|
$client->dialogClose('2131231231');
|
||||||
|
@ -39,9 +39,9 @@ class MessagesTest extends TestCase
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
false,
|
false,
|
||||||
$this->getResponse(
|
$this->getErrorsResponse(
|
||||||
'{"errors":["chat_id is a required field"]}',
|
400,
|
||||||
400
|
'chat_id is a required field'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user