From 4f1537307472556371ad9deb041a16a72c153df6 Mon Sep 17 00:00:00 2001 From: Alex Komarichev Date: Wed, 28 Feb 2024 14:30:01 +0300 Subject: [PATCH 1/5] working with 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 | 60 +++++++++++++++++ 4 files changed, 258 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 @@ public function dialogClose(string $request) 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..b22e616 --- /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; + } +} \ No newline at end of file diff --git a/src/Bot/Model/Request/DialogTagRequest.php b/src/Bot/Model/Request/DialogTagRequest.php new file mode 100644 index 0000000..26e45c6 --- /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; + } + +} \ No newline at end of file diff --git a/tests/Bot/Tests/DialogsTest.php b/tests/Bot/Tests/DialogsTest.php index 08ee4d3..de990b3 100644 --- a/tests/Bot/Tests/DialogsTest.php +++ b/tests/Bot/Tests/DialogsTest.php @@ -14,7 +14,9 @@ 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,62 @@ public function testDialogClose() self::assertTrue($response->isSuccessful()); self::assertEmpty($response->getErrors()); } + + /** + * @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 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()); + } } From f5f165f9299749681eeb5aee4ef6b155eb561a36 Mon Sep 17 00:00:00 2001 From: Alex Komarichev Date: Wed, 28 Feb 2024 20:51:07 +0300 Subject: [PATCH 2/5] More tests --- tests/Bot/Tests/DialogsTest.php | 53 +++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/Bot/Tests/DialogsTest.php b/tests/Bot/Tests/DialogsTest.php index de990b3..9bc6063 100644 --- a/tests/Bot/Tests/DialogsTest.php +++ b/tests/Bot/Tests/DialogsTest.php @@ -148,6 +148,37 @@ public function testDialogClose() self::assertEmpty($response->getErrors()); } + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogAddTagError() + { + self::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); + + $response = $client->dialogAddTag($request); + self::assertEmpty($response->getErrors()); + } + /** * @group("dialogs") * @throws \Exception @@ -179,6 +210,28 @@ public function testDialogAddTag() self::assertEmpty($response->getErrors()); } + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogDeleteTagError() + { + $this->expectException(\TypeError::class); + + $client = self::getApiClient( + null, + null, + false, + ); + + $tags[0] = new Tag(); + + $request = new DialogTagRequest(); + $request->setTags($tags); + + $client->dialogDeleteTag($request); + } + /** * @group("dialogs") * @throws \Exception From bfe8f6604c7977e8926174614c2294eb03f96ff0 Mon Sep 17 00:00:00 2001 From: Alex Komarichev Date: Thu, 29 Feb 2024 11:24:24 +0300 Subject: [PATCH 3/5] Little code fix --- tests/Bot/Tests/DialogsTest.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/Bot/Tests/DialogsTest.php b/tests/Bot/Tests/DialogsTest.php index 9bc6063..1819f39 100644 --- a/tests/Bot/Tests/DialogsTest.php +++ b/tests/Bot/Tests/DialogsTest.php @@ -218,11 +218,7 @@ public function testDialogDeleteTagError() { $this->expectException(\TypeError::class); - $client = self::getApiClient( - null, - null, - false, - ); + $client = self::getApiClient(); $tags[0] = new Tag(); From 5c2d029d3a9b84410acd123acf228b83aa6cb040 Mon Sep 17 00:00:00 2001 From: Alex Komarichev Date: Thu, 29 Feb 2024 11:32:35 +0300 Subject: [PATCH 4/5] More tests --- tests/Bot/Tests/DialogsTest.php | 79 ++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/tests/Bot/Tests/DialogsTest.php b/tests/Bot/Tests/DialogsTest.php index 1819f39..61dbe7f 100644 --- a/tests/Bot/Tests/DialogsTest.php +++ b/tests/Bot/Tests/DialogsTest.php @@ -152,9 +152,9 @@ public function testDialogClose() * @group("dialogs") * @throws \Exception */ - public function testDialogAddTagError() + public function testDialogAddTagColorError() { - self::expectException(NotFoundException::class); + $this->expectException(NotFoundException::class); $client = self::getApiClient( null, @@ -175,8 +175,52 @@ public function testDialogAddTagError() $request->setDialogId(60); $request->setTags($tags); - $response = $client->dialogAddTag($request); - self::assertEmpty($response->getErrors()); + $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); } /** @@ -214,7 +258,32 @@ public function testDialogAddTag() * @group("dialogs") * @throws \Exception */ - public function testDialogDeleteTagError() + 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); From 1a433d3bf8e0ed36ee3f8cd000afa449ce2f6daf Mon Sep 17 00:00:00 2001 From: Alex Komarichev Date: Thu, 29 Feb 2024 11:57:13 +0300 Subject: [PATCH 5/5] Blank line at the end --- src/Bot/Model/Entity/Tag.php | 2 +- src/Bot/Model/Request/DialogTagRequest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bot/Model/Entity/Tag.php b/src/Bot/Model/Entity/Tag.php index b22e616..6b6ff39 100644 --- a/src/Bot/Model/Entity/Tag.php +++ b/src/Bot/Model/Entity/Tag.php @@ -75,4 +75,4 @@ public function setColorCode(string $colorCode): void { $this->colorCode = $colorCode; } -} \ No newline at end of file +} diff --git a/src/Bot/Model/Request/DialogTagRequest.php b/src/Bot/Model/Request/DialogTagRequest.php index 26e45c6..6b133af 100644 --- a/src/Bot/Model/Request/DialogTagRequest.php +++ b/src/Bot/Model/Request/DialogTagRequest.php @@ -75,4 +75,4 @@ public function setTags(array $tags): void $this->tags = $tags; } -} \ No newline at end of file +}