From 6682b9b02ce20bace3c5980f84310672a6894bd6 Mon Sep 17 00:00:00 2001 From: Chupocabra <71826921+Chupocabra@users.noreply.github.com> Date: Thu, 29 Feb 2024 12:01:40 +0300 Subject: [PATCH] Add working with dialog tags --- src/Bot/Client.php | 42 +++++ src/Bot/Model/Entity/Tag.php | 78 +++++++++ src/Bot/Model/Request/DialogTagRequest.php | 78 +++++++++ tests/Bot/Tests/DialogsTest.php | 178 +++++++++++++++++++++ 4 files changed, 376 insertions(+) create mode 100644 src/Bot/Model/Entity/Tag.php create mode 100644 src/Bot/Model/Request/DialogTagRequest.php diff --git a/src/Bot/Client.php b/src/Bot/Client.php index 24ee1be..fe0b59d 100644 --- a/src/Bot/Client.php +++ b/src/Bot/Client.php @@ -313,6 +313,48 @@ class Client return $adapter->getResponseModel($response); } + /** + * Add tag to dialog + * + * @param Model\Request\DialogTagRequest $request Request parameters + * + * @return \RetailCrm\Mg\Bot\Model\ModelInterface + * @throws \Exception + */ + public function dialogAddTag(Model\Request\DialogTagRequest $request) + { + $response = $this->client->makeRequest( + sprintf("/dialogs/%d/tags/add", $request->getDialogId()), + HttpClient::METHOD_PATCH, + $request + ); + + $adapter = new ModelAdapter(ErrorOnlyResponse::class); + + return $adapter->getResponseModel($response); + } + + /** + * Delete tag from dialog + * + * @param Model\Request\DialogTagRequest $request Request parameters + * + * @return \RetailCrm\Mg\Bot\Model\ModelInterface + * @throws \Exception + */ + public function dialogDeleteTag(Model\Request\DialogTagRequest $request) + { + $response = $this->client->makeRequest( + sprintf("/dialogs/%d/tags/delete", $request->getDialogId()), + HttpClient::METHOD_PATCH, + $request + ); + + $adapter = new ModelAdapter(ErrorOnlyResponse::class); + + return $adapter->getResponseModel($response); + } + /** * Returns filtered members list * diff --git a/src/Bot/Model/Entity/Tag.php b/src/Bot/Model/Entity/Tag.php new file mode 100644 index 0000000..6b6ff39 --- /dev/null +++ b/src/Bot/Model/Entity/Tag.php @@ -0,0 +1,78 @@ +name; + } + + /** + * @param string $name + * @return void + */ + public function setName(string $name): void + { + $this->name = $name; + } + + /** + * @return string|null + */ + public function getColorCode(): ?string + { + return $this->colorCode; + } + + /** + * @param string $colorCode + * @return void + */ + public function setColorCode(string $colorCode): void + { + $this->colorCode = $colorCode; + } +} diff --git a/src/Bot/Model/Request/DialogTagRequest.php b/src/Bot/Model/Request/DialogTagRequest.php new file mode 100644 index 0000000..6b133af --- /dev/null +++ b/src/Bot/Model/Request/DialogTagRequest.php @@ -0,0 +1,78 @@ +dialogId; + } + + /** + * @param int $dialogId + * @return void + */ + public function setDialogId(int $dialogId): void + { + $this->dialogId = $dialogId; + } + + /** + * @return Tag[] + */ + public function getTags(): array + { + return $this->tags; + } + + /** + * @param Tag[] $tags + * @return void + */ + public function setTags(array $tags): void + { + $this->tags = $tags; + } + +} diff --git a/tests/Bot/Tests/DialogsTest.php b/tests/Bot/Tests/DialogsTest.php index 08ee4d3..61dbe7f 100644 --- a/tests/Bot/Tests/DialogsTest.php +++ b/tests/Bot/Tests/DialogsTest.php @@ -14,7 +14,9 @@ namespace RetailCrm\Mg\Bot\Tests; use InvalidArgumentException; use RetailCrm\Common\Exception\NotFoundException; use RetailCrm\Mg\Bot\Model\Entity\Responsible; +use RetailCrm\Mg\Bot\Model\Entity\Tag; use RetailCrm\Mg\Bot\Model\Request\DialogAssignRequest; +use RetailCrm\Mg\Bot\Model\Request\DialogTagRequest; use RetailCrm\Mg\Bot\Model\Response\ErrorOnlyResponse; use RetailCrm\Mg\Bot\Test\TestCase; @@ -145,4 +147,180 @@ class DialogsTest extends TestCase self::assertTrue($response->isSuccessful()); self::assertEmpty($response->getErrors()); } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogAddTagColorError() + { + $this->expectException(NotFoundException::class); + + $client = self::getApiClient( + null, + null, + false, + $this->getErrorsResponse(404, + "'color_code can contain only the following values: " . + "light-red; light-blue; light-green; light-orange; light-gray; " . + "light-grayish-blue; red; blue; green; orange; gray; grayish-blue'" + ) + ); + + $tags[0] = new Tag(); + $tags[0]->setName('tag1'); + $tags[0]->setColorCode('qwerty'); + + $request = new DialogTagRequest(); + $request->setDialogId(60); + $request->setTags($tags); + + $client->dialogAddTag($request); + } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogAddTagDialogError() + { + $this->expectException(NotFoundException::class); + + $client = self::getApiClient( + null, + null, + false, + $this->getErrorsResponse(404, "dialog #123456789 not found") + ); + + $tags[0] = new Tag(); + $tags[0]->setName('tag1'); + + $request = new DialogTagRequest(); + $request->setDialogId(123456789); + $request->setTags($tags); + + $client->dialogAddTag($request); + } + + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogAddTagEmptyTagError() + { + $this->expectException(\TypeError::class); + + $client = self::getApiClient(); + + $tags[0] = new Tag(); + + $request = new DialogTagRequest(); + $request->setDialogId(60); + $request->setTags($tags); + + $client->dialogAddTag($request); + } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogAddTag() + { + $client = self::getApiClient( + null, + null, + false, + $this->getResponse('{}') + ); + + $tags[0] = new Tag(); + $tags[0]->setName('tag1'); + $tags[0]->setColorCode('red'); + + $tags[1] = new Tag(); + $tags[1]->setName('tag2'); + + $request = new DialogTagRequest(); + $request->setDialogId(60); + $request->setTags($tags); + + $response = $client->dialogAddTag($request); + + self::assertInstanceOF(ErrorOnlyResponse::class, $response); + self::assertTrue($response->isSuccessful()); + self::assertEmpty($response->getErrors()); + } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogDeleteTagDialogError() + { + $this->expectException(NotFoundException::class); + + $client = self::getApiClient( + null, + null, + false, + $this->getErrorsResponse(404, "dialog #123456789 not found") + ); + + $tags[0] = new Tag(); + $tags[0]->setName('tag1'); + + $request = new DialogTagRequest(); + $request->setDialogId(123456789); + $request->setTags($tags); + + $client->dialogAddTag($request); + } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogDeleteTagEmptyTagError() + { + $this->expectException(\TypeError::class); + + $client = self::getApiClient(); + + $tags[0] = new Tag(); + + $request = new DialogTagRequest(); + $request->setTags($tags); + + $client->dialogDeleteTag($request); + } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogDeleteTag() + { + $client = self::getApiClient( + null, + null, + false, + $this->getResponse('{}') + ); + + $tags[0] = new Tag(); + $tags[0]->setName('tag1'); + + $request = new DialogTagRequest(); + $request->setDialogId(60); + $request->setTags($tags); + + $response = $client->dialogDeleteTag($request); + + self::assertInstanceOF(ErrorOnlyResponse::class, $response); + self::assertTrue($response->isSuccessful()); + self::assertEmpty($response->getErrors()); + } }