diff --git a/src/Bot/Model/Entity/Chat/Chat.php b/src/Bot/Model/Entity/Chat/Chat.php index 1d12ed4..ab9215b 100644 --- a/src/Bot/Model/Entity/Chat/Chat.php +++ b/src/Bot/Model/Entity/Chat/Chat.php @@ -13,6 +13,7 @@ namespace RetailCrm\Mg\Bot\Model\Entity\Chat; +use JMS\Serializer\Annotation as Serializer; use RetailCrm\Mg\Bot\Model\Entity\Channel\Channel; use RetailCrm\Mg\Bot\Model\Entity\CommonFields; use JMS\Serializer\Annotation\Accessor; @@ -55,6 +56,7 @@ class Chat * * @Type("RetailCrm\Mg\Bot\Model\Entity\Channel\Channel") * @Accessor(getter="getChannel",setter="setChannel") + * @SkipWhenEmpty() */ private $channel; diff --git a/src/Bot/Model/Entity/CommonFields.php b/src/Bot/Model/Entity/CommonFields.php index f669c31..7636fa1 100644 --- a/src/Bot/Model/Entity/CommonFields.php +++ b/src/Bot/Model/Entity/CommonFields.php @@ -49,10 +49,10 @@ trait CommonFields private $createdAt; /** - * @var \DateTime $createdAt + * @var \DateTime $updatedAt * * @Type("DateTime<'Y-m-d\TH:i:s\.u\Z'>") - * @Accessor(getter="getCreatedAt",setter="setCreatedAt") + * @Accessor(getter="getUpdatedAt",setter="setUpdatedAt") * @SkipWhenEmpty() */ private $updatedAt; diff --git a/src/Bot/Model/Entity/Dialog.php b/src/Bot/Model/Entity/Dialog.php index 5110df3..0116c8f 100644 --- a/src/Bot/Model/Entity/Dialog.php +++ b/src/Bot/Model/Entity/Dialog.php @@ -13,7 +13,6 @@ namespace RetailCrm\Mg\Bot\Model\Entity; -use RetailCrm\Mg\Bot\Model\Entity\CommonFields; use JMS\Serializer\Annotation\Accessor; use JMS\Serializer\Annotation\SkipWhenEmpty; use JMS\Serializer\Annotation\Type; diff --git a/src/Bot/Model/Entity/Responsible.php b/src/Bot/Model/Entity/Responsible.php index 763c59e..c39a189 100644 --- a/src/Bot/Model/Entity/Responsible.php +++ b/src/Bot/Model/Entity/Responsible.php @@ -30,9 +30,9 @@ use JMS\Serializer\Annotation\Type; class Responsible { /** - * @var \DateTime $assignedAt + * @var string $assignedAt * - * @Type("DateTime<'Y-m-d\TH:i:s\.u\Z'>") + * @Type("string") * @Accessor(getter="getAssignedAt",setter="setAssignedAt") * @SkipWhenEmpty() */ @@ -57,17 +57,17 @@ class Responsible private $type; /** - * @return \DateTime + * @return string */ - public function getAssignedAt(): \DateTime + public function getAssignedAt(): string { return $this->assignedAt; } /** - * @param \DateTime $assignedAt + * @param string $assignedAt */ - public function setAssignedAt(\DateTime $assignedAt) + public function setAssignedAt(string $assignedAt) { $this->assignedAt = $assignedAt; } diff --git a/src/Bot/Model/Request/DialogAssignRequest.php b/src/Bot/Model/Request/DialogAssignRequest.php index 3837ee1..f46d7ed 100644 --- a/src/Bot/Model/Request/DialogAssignRequest.php +++ b/src/Bot/Model/Request/DialogAssignRequest.php @@ -33,8 +33,7 @@ class DialogAssignRequest * @var int $dialogId * * @Type("int") - * @Accessor(getter="getDialogId,setter="setDialogId") - * @SkipWhenEmpty + * @Accessor(getter="getDialogId", setter="setDialogId") */ private $dialogId; @@ -42,8 +41,8 @@ class DialogAssignRequest * @var int $userId * * @Type("int") - * @Accessor(getter="getUserId",setter="setUserId") - * @SkipWhenEmpty + * @Accessor(getter="getUserId", setter="setUserId") + * @SkipWhenEmpty() */ private $userId; @@ -51,7 +50,7 @@ class DialogAssignRequest * @var int $botId * * @Type("int") - * @Accessor(getter="getBotId",setter="setBotId") + * @Accessor(getter="getBotId", setter="setBotId") * @SkipWhenEmpty() */ private $botId; @@ -67,7 +66,7 @@ class DialogAssignRequest /** * @param int $dialogId */ - public function setDialogId(int $dialogId) + public function setDialogId($dialogId) { $this->dialogId = $dialogId; } diff --git a/src/Bot/Model/Response/AssignResponse.php b/src/Bot/Model/Response/AssignResponse.php index fd7b0c6..73ac86f 100644 --- a/src/Bot/Model/Response/AssignResponse.php +++ b/src/Bot/Model/Response/AssignResponse.php @@ -53,7 +53,7 @@ class AssignResponse /** * @var Responsible $previousResponsible * - * @Type("Responsible") + * @Type("RetailCrm\Mg\Bot\Model\Entity\Responsible") * @Accessor(getter="getPreviousResponsible",setter="setPreviousResponsible") * @SkipWhenEmpty() */ @@ -62,7 +62,7 @@ class AssignResponse /** * @var Responsible $responsible * - * @Type("Responsible") + * @Type("RetailCrm\Mg\Bot\Model\Entity\Responsible") * @Accessor(getter="getResponsible",setter="setResponsible") * @SkipWhenEmpty() */ @@ -71,7 +71,7 @@ class AssignResponse /** * @return bool */ - public function isReassign(): bool + public function getIsReassign(): bool { return $this->isReassign; } diff --git a/src/Bot/Model/Response/ListResponse.php b/src/Bot/Model/Response/ListResponse.php index 7032fd2..f4b47c6 100644 --- a/src/Bot/Model/Response/ListResponse.php +++ b/src/Bot/Model/Response/ListResponse.php @@ -54,11 +54,8 @@ class ListResponse implements \Iterator, \ArrayAccess, \Countable { $this->errors = $data['errors']; } else { foreach($data as $item) { - if (substr($responseType, 0, 1) == '\\') { - $responseType = substr($responseType, 1); - } - - $this->items[] = Serializer::deserialize($item, $responseType, Serializer::S_ARRAY); + $this->items[] = + Serializer::deserialize($item, $responseType, Serializer::S_ARRAY); } } } diff --git a/src/Serializer.php b/src/Serializer.php index 77e5cc7..f9e3556 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -76,10 +76,12 @@ class Serializer switch ($from) { case self::S_ARRAY: - $deserialized = $serializer->fromArray(array_filter($data), self::normalizeNamespace($entityType), $context); + $deserialized = + $serializer->fromArray(array_filter($data), self::normalizeNamespace($entityType), $context); break; case self::S_JSON: - $deserialized = $serializer->deserialize($data, self::normalizeNamespace($entityType), $from, $context); + $deserialized = + $serializer->deserialize($data, self::normalizeNamespace($entityType), $from, $context); break; } @@ -112,9 +114,9 @@ class Serializer private static function normalizeNamespace(string $namespace) { if (substr($namespace, 0, 1) == '\\') { - return substr($namespace, 1); - } else { - return $namespace; + $namespace = substr($namespace, 1); } + + return $namespace; } } diff --git a/tests/Bot/Tests/CommandsTest.php b/tests/Bot/Tests/CommandsTest.php index 15a4311..b4a9d78 100644 --- a/tests/Bot/Tests/CommandsTest.php +++ b/tests/Bot/Tests/CommandsTest.php @@ -3,7 +3,7 @@ /** * PHP version 7.0 * - * Client Test + * Commands Test * * @package RetailCrm\Mg\Bot\Tests * @author retailCRM @@ -23,7 +23,7 @@ use RetailCrm\Mg\Bot\Test\TestCase; /** * PHP version 7.0 * - * Class ClientTest + * Class CommandsTest * * @package RetailCrm\Mg\Bot\Tests * @author retailCRM diff --git a/tests/Bot/Tests/DialogsTest.php b/tests/Bot/Tests/DialogsTest.php new file mode 100644 index 0000000..61ed1d5 --- /dev/null +++ b/tests/Bot/Tests/DialogsTest.php @@ -0,0 +1,118 @@ + + * @license https://opensource.org/licenses/MIT MIT License + * @link http://help.retailcrm.pro/docs/Developers + */ + +namespace RetailCrm\Mg\Bot\Tests; + +use InvalidArgumentException; +use RetailCrm\Mg\Bot\Model\Entity\Responsible; +use RetailCrm\Mg\Bot\Model\Request\DialogAssignRequest; +use RetailCrm\Mg\Bot\Model\Response\ErrorOnlyResponse; +use RetailCrm\Mg\Bot\Test\TestCase; + +/** + * PHP version 7.0 + * + * Class DialogsTest + * + * @package RetailCrm\Mg\Bot\Tests + * @author retailCRM + * @license https://opensource.org/licenses/MIT MIT License + * @link http://help.retailcrm.pro/docs/Developers + */ +class DialogsTest extends TestCase +{ + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogAssignError() + { + $client = self::getApiClient( + null, + null, + false, + $this->getResponse('{"errors":["incorrect dialog_id"]}', 400) + ); + + $request = new DialogAssignRequest(); + $request->setDialogId(-1); + + $response = $client->dialogAssign($request); + + self::assertTrue(!$response->isSuccessful()); + self::assertNotEmpty($response->getErrors()); + } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogAssign() + { + $client = self::getApiClient( + null, + null, + false, + $this->getJsonResponse('dialogReassigned') + ); + + $request = new DialogAssignRequest(); + $request->setDialogId(60); + $request->setUserId(4); + + $response = $client->dialogAssign($request); + + self::assertTrue($response->isSuccessful()); + self::assertTrue($response->getIsReassign()); + self::assertTrue($response->getPreviousResponsible() instanceof Responsible); + self::assertTrue($response->getResponsible() instanceof Responsible); + } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogCloseError() + { + self::expectException(InvalidArgumentException::class); + + $client = self::getApiClient( + null, + null, + false, + $this->getResponse('{"errors":["dialog #2131231231 not found"]}', 404) + ); + + $client->dialogClose('2131231231'); + } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogClose() + { + $client = self::getApiClient( + null, + null, + false, + $this->getResponse('{}') + ); + + $response = $client->dialogClose('62'); + + self::assertTrue($response instanceof ErrorOnlyResponse); + self::assertTrue($response->isSuccessful()); + self::assertEmpty($response->getErrors()); + } +} diff --git a/tests/Bot/Tests/MessagesTest.php b/tests/Bot/Tests/MessagesTest.php index c2ad240..c7b5099 100644 --- a/tests/Bot/Tests/MessagesTest.php +++ b/tests/Bot/Tests/MessagesTest.php @@ -3,7 +3,7 @@ /** * PHP version 7.0 * - * Client Test + * Messages Test * * @package RetailCrm\Mg\Bot\Tests * @author retailCRM diff --git a/tests/Resources/channels.json b/tests/Resources/channels.json index c47ec97..fb685d8 100644 --- a/tests/Resources/channels.json +++ b/tests/Resources/channels.json @@ -38,7 +38,7 @@ } }, "created_at": "2019-06-11T12:46:48.72241Z", - "updated_at": "2019-06-11T12:46:48.72241Z", + "updated_at": "2019-06-11T12:56:48.72241Z", "activated_at": "2019-06-11T12:46:48.722086Z", "deactivated_at": null, "is_active": true diff --git a/tests/Resources/dialogReassigned.json b/tests/Resources/dialogReassigned.json new file mode 100644 index 0000000..60f9aa0 --- /dev/null +++ b/tests/Resources/dialogReassigned.json @@ -0,0 +1 @@ +{"responsible":{"type":"user","id":4,"assigned_at":"2019-06-21T08:01:09Z"},"is_reassign":true,"previous_responsible":{"type":"user","id":12,"assigned_at":"2019-06-11T12:49:34Z"},"left_user_id":12} \ No newline at end of file