1
0
mirror of synced 2024-11-22 03:46:02 +03:00
This commit is contained in:
Pavel 2019-06-19 12:49:55 +03:00
parent 42669a71a4
commit 8d034be059
6 changed files with 46 additions and 11 deletions

View File

@ -166,7 +166,7 @@ class HttpClient
* therefore they are not handled as exceptions * therefore they are not handled as exceptions
*/ */
if (in_array($statusCode, [403, 404, 500])) { if (in_array($statusCode, [403, 404, 500])) {
throw new \Exception($errorMessage); throw new \InvalidArgumentException($errorMessage);
} }
if ($statusCode == 503) { if ($statusCode == 503) {
@ -213,7 +213,7 @@ class HttpClient
$errors = $validator->validate($class); $errors = $validator->validate($class);
} }
if ($errors->count() > 0) { if ((is_object($errors) && $errors->count() > 0) || is_string($errors)) {
$message = (string) $errors; $message = (string) $errors;
throw new InvalidArgumentException($message); throw new InvalidArgumentException($message);
} }

View File

@ -59,4 +59,11 @@ class CommandEditRequest extends LazyJsonMapper
'name' => 'string', 'name' => 'string',
'description' => 'string' 'description' => 'string'
]; ];
public function validate()
{
if (empty($this->getName())) {
return 'Empty name';
}
}
} }

View File

@ -58,6 +58,6 @@ class CommonFields extends LazyJsonMapper
*/ */
public function isError() public function isError()
{ {
return !empty($this->getErrors()); return (bool) !empty($this->getErrors());
} }
} }

View File

@ -15,7 +15,9 @@ namespace RetailCrm\Mg\Bot\Tests;
use Exception; use Exception;
use InvalidArgumentException; use InvalidArgumentException;
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\Test\TestCase; use RetailCrm\Mg\Bot\Test\TestCase;
/** /**
@ -38,7 +40,12 @@ class CommandsTest extends TestCase
{ {
self::expectException(InvalidArgumentException::class); self::expectException(InvalidArgumentException::class);
$client = self::getApiClient(); $client = self::getApiClient(
null,
null,
false,
$this->getResponse('EOF',400)
);
$request = new CommandEditRequest(); $request = new CommandEditRequest();
$request->setDescription("qwerty"); $request->setDescription("qwerty");
@ -67,7 +74,12 @@ class CommandsTest extends TestCase
*/ */
public function testCommandEdit() public function testCommandEdit()
{ {
$client = self::getApiClient(); $client = self::getApiClient(
null,
null,
false,
$this->getJsonResponse('commandEdit')
);
$request = new CommandEditRequest(); $request = new CommandEditRequest();
$request->setBotId(1); $request->setBotId(1);
@ -76,7 +88,8 @@ class CommandsTest extends TestCase
$response = $client->commandEdit($request); $response = $client->commandEdit($request);
self::assertTrue($response->isSuccessful() == true); self::assertTrue($response instanceof ErrorOnlyResponse);
self::assertTrue(!$response->isError());
} }
/** /**
@ -86,10 +99,15 @@ class CommandsTest extends TestCase
*/ */
public function testCommandDelete() public function testCommandDelete()
{ {
$client = self::getApiClient(); $client = self::getApiClient(
null,
null,
false,
$this->getJsonResponse('commandEdit')
);
$response = $client->commandDelete("show_payment_types"); $response = $client->commandDelete("show_payment_types");
self::assertTrue($response->isSuccessful() == true); self::assertTrue($response->isError() == false);
} }
} }

View File

@ -35,15 +35,24 @@ class MessagesTest extends TestCase
*/ */
public function testMessageSend() public function testMessageSend()
{ {
$client = self::getApiClient(); $client = self::getApiClient(
null,
null,
false,
$this->getResponse(
'{"errors":["chat_id is a required field"]}',
400
)
);
$request = new MessageSendRequest(); $request = new MessageSendRequest();
$request->setChatId(0); $request->setChatId(0);
$request->setScope(Constants::MESSAGE_SCOPE_PUBLIC); $request->setScope(Constants::MESSAGE_SCOPE_PUBLIC);
$request->setContent("Hello"); $request->setContent("Hello");
$request = $client->messageSend($request); $response = $client->messageSend($request);
self::assertEquals($request->getStatusCode(), 400); self::assertTrue($response->isError());
self::assertEquals(1, count($response->getErrors()));
} }
} }

View File

@ -0,0 +1 @@
{"id":6,"name":"show_payment_types","description":"Get available payment types","created_at":"2019-06-19T09:02:35Z","updated_at":"2019-06-19T09:39:00Z"}