From 35ca05eb83992a9496ea41767dfaf0677f28741b Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 18 Jun 2019 13:56:31 +0300 Subject: [PATCH] Finished test case, another method of entity validation --- src/Bot/HttpClient.php | 12 ++++--- .../Model/Entity/Message/MessageDelivery.php | 1 + tests/Bot/Tests/ClientListTest.php | 32 ++++++++++++++--- tests/Resources/customers.json | 34 +++++++++++++++++++ tests/Resources/dialogs.json | 34 +++++++++++++++++++ tests/Resources/users.json | 23 +++++++++++++ 6 files changed, 127 insertions(+), 9 deletions(-) create mode 100644 tests/Resources/customers.json create mode 100644 tests/Resources/dialogs.json create mode 100644 tests/Resources/users.json diff --git a/src/Bot/HttpClient.php b/src/Bot/HttpClient.php index 491da2e..2f5073a 100644 --- a/src/Bot/HttpClient.php +++ b/src/Bot/HttpClient.php @@ -203,11 +203,15 @@ class HttpClient */ private function validateRequest($class) { - $validator = Validation::createValidatorBuilder() - ->enableAnnotationMapping() - ->getValidator(); + if (method_exists($class, 'validate')) { + $errors = $class->validate(); + } else { + $validator = Validation::createValidatorBuilder() + ->enableAnnotationMapping() + ->getValidator(); - $errors = $validator->validate($class); + $errors = $validator->validate($class); + } if ($errors->count() > 0) { $message = (string) $errors; diff --git a/src/Bot/Model/Entity/Message/MessageDelivery.php b/src/Bot/Model/Entity/Message/MessageDelivery.php index 3b0d48c..6769dcc 100644 --- a/src/Bot/Model/Entity/Message/MessageDelivery.php +++ b/src/Bot/Model/Entity/Message/MessageDelivery.php @@ -14,6 +14,7 @@ namespace RetailCrm\Mg\Bot\Model\Entity\Message; use LazyJsonMapper\LazyJsonMapper; +use Symfony\Component\Validator\Validation; /** * PHP version 7.0 diff --git a/tests/Bot/Tests/ClientListTest.php b/tests/Bot/Tests/ClientListTest.php index f31604d..969a267 100644 --- a/tests/Bot/Tests/ClientListTest.php +++ b/tests/Bot/Tests/ClientListTest.php @@ -17,8 +17,10 @@ use RetailCrm\Mg\Bot\Model\Constants; use RetailCrm\Mg\Bot\Model\Entity\Channel; use RetailCrm\Mg\Bot\Model\Entity\Chat\Chat; use RetailCrm\Mg\Bot\Model\Entity\Chat\ChatMember; +use RetailCrm\Mg\Bot\Model\Entity\Customer; use RetailCrm\Mg\Bot\Model\Entity\Dialog; use RetailCrm\Mg\Bot\Model\Entity\Message\Message; +use RetailCrm\Mg\Bot\Model\Entity\User; use RetailCrm\Mg\Bot\Model\Request; use RetailCrm\Mg\Bot\Model\Response; use RetailCrm\Mg\Bot\Test\TestCase; @@ -171,7 +173,12 @@ class ClientListTest extends TestCase */ public function testUsers() { - $client = self::getApiClient(); + $client = self::getApiClient( + null, + null, + false, + $this->getJsonResponse('users') + ); $request = new Request\UsersRequest(); $request->setActive(1); @@ -179,7 +186,8 @@ class ClientListTest extends TestCase $response = $client->users($request); - print_r($response); + self::assertEquals(2, $response->count()); + self::assertTrue($response[0] instanceof User); } /** @@ -188,13 +196,21 @@ class ClientListTest extends TestCase */ public function testDialogs() { - $client = self::getApiClient(); + $client = self::getApiClient( + null, + null, + false, + $this->getJsonResponse('dialogs') + ); $request = new Request\DialogsRequest(); $request->setActive(1); $request->setAssign(1); $response = $client->dialogs($request); + + self::assertEquals(2, $response->count()); + self::assertTrue($response[0] instanceof Dialog); } /** @@ -203,12 +219,18 @@ class ClientListTest extends TestCase */ public function testCustomers() { - $client = self::getApiClient(); + $client = self::getApiClient( + null, + null, + false, + $this->getJsonResponse('customers') + ); $request = new Request\CustomersRequest(); $response = $client->customers($request); - print_r($response); + self::assertEquals(2, $response->count()); + self::assertTrue($response[0] instanceof Customer); } } diff --git a/tests/Resources/customers.json b/tests/Resources/customers.json new file mode 100644 index 0000000..280b478 --- /dev/null +++ b/tests/Resources/customers.json @@ -0,0 +1,34 @@ +[ + { + "id": 39, + "external_id": "333296016", + "channel_id": 3, + "username": "user1", + "first_name": "User1", + "last_name": "Lastname1", + "created_at": "2019-06-11T12:49:26.935824Z", + "updated_at": "2019-06-11T14:36:20.840304Z", + "avatar_url": "", + "profile_url": "", + "country": "", + "language": "en", + "phone": "", + "email": "" + }, + { + "id": 38, + "external_id": "2272664739519803", + "channel_id": 3, + "username": "user2", + "first_name": "User2", + "last_name": "Lastname2", + "created_at": "2019-06-11T07:34:16.079453Z", + "updated_at": "2019-06-13T08:07:14.898472Z", + "avatar_url": "", + "profile_url": "", + "country": "", + "language": "ru", + "phone": "", + "email": "" + } +] \ No newline at end of file diff --git a/tests/Resources/dialogs.json b/tests/Resources/dialogs.json new file mode 100644 index 0000000..b7e750b --- /dev/null +++ b/tests/Resources/dialogs.json @@ -0,0 +1,34 @@ +[ + { + "id": 60, + "chat_id": 30, + "begin_message_id": 3540, + "ending_message_id": 3625, + "created_at": "2019-06-11T12:49:26.955306Z", + "updated_at": "2019-06-14T14:40:28.711974Z", + "closed_at": "2019-06-14T14:40:28.684683Z", + "is_assigned": true, + "responsible": { + "type": "user", + "id": 12, + "assigned_at": "2019-06-11T12:49:34.716716Z" + }, + "is_active": true + }, + { + "id": 59, + "chat_id": 29, + "begin_message_id": 3483, + "ending_message_id": 3626, + "created_at": "2019-06-11T07:34:16.101228Z", + "updated_at": "2019-06-16T08:10:28.659389Z", + "closed_at": "2019-06-16T08:10:28.645607Z", + "is_assigned": true, + "responsible": { + "type": "user", + "id": 12, + "assigned_at": "2019-06-11T08:40:14.136916Z" + }, + "is_active": true + } +] \ No newline at end of file diff --git a/tests/Resources/users.json b/tests/Resources/users.json new file mode 100644 index 0000000..4a47fa8 --- /dev/null +++ b/tests/Resources/users.json @@ -0,0 +1,23 @@ +[ + { + "id": 1, + "external_id": "11", + "username": "User 1", + "first_name": "User", + "created_at": "2018-10-02T10:10:10.066295Z", + "updated_at": "2019-06-17T07:40:26.613546Z", + "is_online": false, + "is_active": true, + "avatar_url": "" + }, + { + "id": 2, + "external_id": "22", + "username": "User 2", + "first_name": "User", + "created_at": "2018-09-06T13:19:25.01842Z", + "updated_at": "2019-06-07T11:36:52.342065Z", + "is_online": false, + "is_active": true + } +] \ No newline at end of file