From 8d034be0592f1eb0f72a0636b50152ac7ca557f4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 19 Jun 2019 12:49:55 +0300 Subject: [PATCH] Tests --- src/Bot/HttpClient.php | 4 +-- src/Bot/Model/Request/CommandEditRequest.php | 7 +++++ src/Bot/Model/Response/CommonFields.php | 2 +- tests/Bot/Tests/CommandsTest.php | 28 ++++++++++++++++---- tests/Bot/Tests/MessagesTest.php | 15 ++++++++--- tests/Resources/commandEdit.json | 1 + 6 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 tests/Resources/commandEdit.json diff --git a/src/Bot/HttpClient.php b/src/Bot/HttpClient.php index 2f5073a..616fd4e 100644 --- a/src/Bot/HttpClient.php +++ b/src/Bot/HttpClient.php @@ -166,7 +166,7 @@ class HttpClient * therefore they are not handled as exceptions */ if (in_array($statusCode, [403, 404, 500])) { - throw new \Exception($errorMessage); + throw new \InvalidArgumentException($errorMessage); } if ($statusCode == 503) { @@ -213,7 +213,7 @@ class HttpClient $errors = $validator->validate($class); } - if ($errors->count() > 0) { + if ((is_object($errors) && $errors->count() > 0) || is_string($errors)) { $message = (string) $errors; throw new InvalidArgumentException($message); } diff --git a/src/Bot/Model/Request/CommandEditRequest.php b/src/Bot/Model/Request/CommandEditRequest.php index fe41cc5..98775e1 100644 --- a/src/Bot/Model/Request/CommandEditRequest.php +++ b/src/Bot/Model/Request/CommandEditRequest.php @@ -59,4 +59,11 @@ class CommandEditRequest extends LazyJsonMapper 'name' => 'string', 'description' => 'string' ]; + + public function validate() + { + if (empty($this->getName())) { + return 'Empty name'; + } + } } diff --git a/src/Bot/Model/Response/CommonFields.php b/src/Bot/Model/Response/CommonFields.php index 63e2002..de38933 100644 --- a/src/Bot/Model/Response/CommonFields.php +++ b/src/Bot/Model/Response/CommonFields.php @@ -58,6 +58,6 @@ class CommonFields extends LazyJsonMapper */ public function isError() { - return !empty($this->getErrors()); + return (bool) !empty($this->getErrors()); } } \ No newline at end of file diff --git a/tests/Bot/Tests/CommandsTest.php b/tests/Bot/Tests/CommandsTest.php index db3c6d3..acafd7c 100644 --- a/tests/Bot/Tests/CommandsTest.php +++ b/tests/Bot/Tests/CommandsTest.php @@ -15,7 +15,9 @@ 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; use RetailCrm\Mg\Bot\Test\TestCase; /** @@ -38,7 +40,12 @@ class CommandsTest extends TestCase { self::expectException(InvalidArgumentException::class); - $client = self::getApiClient(); + $client = self::getApiClient( + null, + null, + false, + $this->getResponse('EOF',400) + ); $request = new CommandEditRequest(); $request->setDescription("qwerty"); @@ -67,7 +74,12 @@ class CommandsTest extends TestCase */ public function testCommandEdit() { - $client = self::getApiClient(); + $client = self::getApiClient( + null, + null, + false, + $this->getJsonResponse('commandEdit') + ); $request = new CommandEditRequest(); $request->setBotId(1); @@ -76,7 +88,8 @@ class CommandsTest extends TestCase $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() { - $client = self::getApiClient(); + $client = self::getApiClient( + null, + null, + false, + $this->getJsonResponse('commandEdit') + ); $response = $client->commandDelete("show_payment_types"); - self::assertTrue($response->isSuccessful() == true); + self::assertTrue($response->isError() == false); } } diff --git a/tests/Bot/Tests/MessagesTest.php b/tests/Bot/Tests/MessagesTest.php index a46b0d5..72c33c9 100644 --- a/tests/Bot/Tests/MessagesTest.php +++ b/tests/Bot/Tests/MessagesTest.php @@ -35,15 +35,24 @@ class MessagesTest extends TestCase */ 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->setChatId(0); $request->setScope(Constants::MESSAGE_SCOPE_PUBLIC); $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())); } } diff --git a/tests/Resources/commandEdit.json b/tests/Resources/commandEdit.json new file mode 100644 index 0000000..6f28ef9 --- /dev/null +++ b/tests/Resources/commandEdit.json @@ -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"} \ No newline at end of file