From 149aa0518b384b45bab5fd5a6ad3eb76d72e4bc2 Mon Sep 17 00:00:00 2001 From: Ben Kuhl Date: Tue, 9 Apr 2019 10:07:43 -0400 Subject: [PATCH 1/5] Updating tags now accepts Collection, TagsCollection or an array --- src/Conversations/ConversationsEndpoint.php | 23 +++++++--- .../ConversationIntegrationTest.php | 44 ++++++++++++++++++- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/Conversations/ConversationsEndpoint.php b/src/Conversations/ConversationsEndpoint.php index 9350ece8..b31eb86a 100644 --- a/src/Conversations/ConversationsEndpoint.php +++ b/src/Conversations/ConversationsEndpoint.php @@ -5,6 +5,7 @@ namespace HelpScout\Api\Conversations; use HelpScout\Api\Endpoint; +use HelpScout\Api\Entity\Collection; use HelpScout\Api\Entity\PagedCollection; use HelpScout\Api\Entity\Patch; use HelpScout\Api\Http\Hal\HalPagedResources; @@ -65,13 +66,25 @@ public function updateCustomFields(int $conversationId, array $customFields): vo * Updates the tags for a given conversation. * Omitted tags are removed. * - * @param int $conversationId - * @param array $tags + * @param int $conversationId + * @param array|Collection|TagsCollection $tags */ - public function updateTags(int $conversationId, array $tags): void + public function updateTags(int $conversationId, $tags): void { - $tagsCollection = new TagsCollection(); - $tagsCollection->setTags($tags); + if ($tags instanceof TagsCollection) { + $tagsCollection = $tags; + } else { + if ($tags instanceof Collection) { + $tagNames = []; + foreach ($tags as $tag) { + $tagNames[] = $tag->getName(); + } + $tags = $tagNames; + } + + $tagsCollection = new TagsCollection(); + $tagsCollection->setTags($tags); + } $this->restClient->updateResource( $tagsCollection, diff --git a/tests/Conversations/ConversationIntegrationTest.php b/tests/Conversations/ConversationIntegrationTest.php index e8ad4341..eeeff0e4 100644 --- a/tests/Conversations/ConversationIntegrationTest.php +++ b/tests/Conversations/ConversationIntegrationTest.php @@ -13,6 +13,7 @@ use HelpScout\Api\Customers\Customer; use HelpScout\Api\Entity\Collection; use HelpScout\Api\Mailboxes\Mailbox; +use HelpScout\Api\Tags\Tag; use HelpScout\Api\Tags\TagsCollection; use HelpScout\Api\Tests\ApiClientIntegrationTestCase; use HelpScout\Api\Tests\Payloads\ConversationPayloads; @@ -430,7 +431,7 @@ public function testUpdatesCustomFields() ); } - public function testUpdatesTags() + public function testUpdatesTagsWithArrayOfTagNames() { $this->stubResponse( $this->getResponse(204) @@ -446,4 +447,45 @@ public function testUpdatesTags() $tags->extract() ); } + + public function testUpdatesTagsWithTagCollection() + { + $this->stubResponse( + $this->getResponse(204) + ); + + $tags = new TagsCollection(); + $tags->setTags(['Support']); + + $this->client->conversations()->updateTags(14, $tags); + + $this->verifyRequestWithData( + 'https://api.helpscout.net/v2/conversations/14/tags', + 'PUT', + $tags->extract() + ); + } + + public function testUpdatesTagsWithEntityCollectionOfTags() + { + $this->stubResponse( + $this->getResponse(204) + ); + + $tag = new Tag(); + $tag->setName('Support'); + $tags = new Collection([$tag]); + + $this->client->conversations()->updateTags(14, $tags); + + $this->verifyRequestWithData( + 'https://api.helpscout.net/v2/conversations/14/tags', + 'PUT', + [ + 'tags' => [ + 'Support', + ], + ] + ); + } } From 40e9f43256f3785480e88ca0667631cdab9bdcda Mon Sep 17 00:00:00 2001 From: Ben Kuhl Date: Tue, 9 Apr 2019 13:02:20 -0400 Subject: [PATCH 2/5] Updating custom fields now accepts Collection, CustomFieldsCollection or an array --- src/Conversations/ConversationsEndpoint.php | 18 +++++++++---- .../ConversationIntegrationTest.php | 25 +++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Conversations/ConversationsEndpoint.php b/src/Conversations/ConversationsEndpoint.php index b31eb86a..067b5e83 100644 --- a/src/Conversations/ConversationsEndpoint.php +++ b/src/Conversations/ConversationsEndpoint.php @@ -48,13 +48,21 @@ public function create(Conversation $conversation): ?int /** * Updates the custom field values for a given conversation. Ommitted fields are removed. * - * @param int $conversationId - * @param CustomField[] $customFields + * @param int $conversationId + * @param CustomField[]|Collection $customFields */ - public function updateCustomFields(int $conversationId, array $customFields): void + public function updateCustomFields(int $conversationId, $customFields): void { - $customFieldsCollection = new CustomFieldsCollection(); - $customFieldsCollection->setCustomFields($customFields); + if ($customFields instanceof CustomFieldsCollection) { + $customFieldsCollection = $customFields; + } else { + if ($customFields instanceof Collection) { + $customFields = $customFields->toArray(); + } + + $customFieldsCollection = new CustomFieldsCollection(); + $customFieldsCollection->setCustomFields($customFields); + } $this->restClient->updateResource( $customFieldsCollection, diff --git a/tests/Conversations/ConversationIntegrationTest.php b/tests/Conversations/ConversationIntegrationTest.php index eeeff0e4..f557904a 100644 --- a/tests/Conversations/ConversationIntegrationTest.php +++ b/tests/Conversations/ConversationIntegrationTest.php @@ -431,6 +431,31 @@ public function testUpdatesCustomFields() ); } + public function testUpdatesCustomFieldsWithEntityCollectionOfCustomFields() + { + $this->stubResponse( + $this->getResponse(204) + ); + + $customField = new CustomField(); + $customField->setId(10524); + $customField->setValue(new \DateTime('today')); + + $customFields = new Collection([$customField]); + + $this->client->conversations()->updateCustomFields(12, $customFields); + + $fields = new CustomFieldsCollection(); + + $fields->setCustomFields($customFields->toArray()); + + $this->verifyRequestWithData( + 'https://api.helpscout.net/v2/conversations/12/fields', + 'PUT', + $fields->extract() + ); + } + public function testUpdatesTagsWithArrayOfTagNames() { $this->stubResponse( From a29ff4bc862d87dffb292720b0712d9a630f6c8b Mon Sep 17 00:00:00 2001 From: Ben Kuhl Date: Tue, 9 Apr 2019 13:03:03 -0400 Subject: [PATCH 3/5] Adding CustomFieldsCollection as acceptable parameter type --- src/Conversations/ConversationsEndpoint.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Conversations/ConversationsEndpoint.php b/src/Conversations/ConversationsEndpoint.php index 067b5e83..3268a97a 100644 --- a/src/Conversations/ConversationsEndpoint.php +++ b/src/Conversations/ConversationsEndpoint.php @@ -48,8 +48,8 @@ public function create(Conversation $conversation): ?int /** * Updates the custom field values for a given conversation. Ommitted fields are removed. * - * @param int $conversationId - * @param CustomField[]|Collection $customFields + * @param int $conversationId + * @param CustomField[]|Collection|CustomFieldsCollection $customFields */ public function updateCustomFields(int $conversationId, $customFields): void { From ed3697d810bc4b5c2b1027bdbd6e2dd14e6228f7 Mon Sep 17 00:00:00 2001 From: Ben Kuhl Date: Tue, 9 Apr 2019 13:33:31 -0400 Subject: [PATCH 4/5] phpcs --- src/Conversations/ConversationsEndpoint.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Conversations/ConversationsEndpoint.php b/src/Conversations/ConversationsEndpoint.php index 3268a97a..6d1e30f9 100644 --- a/src/Conversations/ConversationsEndpoint.php +++ b/src/Conversations/ConversationsEndpoint.php @@ -48,8 +48,8 @@ public function create(Conversation $conversation): ?int /** * Updates the custom field values for a given conversation. Ommitted fields are removed. * - * @param int $conversationId - * @param CustomField[]|Collection|CustomFieldsCollection $customFields + * @param int $conversationId + * @param CustomField[]|Collection|CustomFieldsCollection $customFields */ public function updateCustomFields(int $conversationId, $customFields): void { @@ -74,8 +74,8 @@ public function updateCustomFields(int $conversationId, $customFields): void * Updates the tags for a given conversation. * Omitted tags are removed. * - * @param int $conversationId - * @param array|Collection|TagsCollection $tags + * @param int $conversationId + * @param array|Collection|TagsCollection $tags */ public function updateTags(int $conversationId, $tags): void { From 48d5618c63259d652e5a7a5809170be291868b57 Mon Sep 17 00:00:00 2001 From: Ben Kuhl Date: Tue, 9 Apr 2019 14:18:33 -0400 Subject: [PATCH 5/5] phpcs --- src/Conversations/ConversationsEndpoint.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Conversations/ConversationsEndpoint.php b/src/Conversations/ConversationsEndpoint.php index 6d1e30f9..6150b8ee 100644 --- a/src/Conversations/ConversationsEndpoint.php +++ b/src/Conversations/ConversationsEndpoint.php @@ -48,8 +48,8 @@ public function create(Conversation $conversation): ?int /** * Updates the custom field values for a given conversation. Ommitted fields are removed. * - * @param int $conversationId - * @param CustomField[]|Collection|CustomFieldsCollection $customFields + * @param int $conversationId + * @param CustomField[]|array|Collection|CustomFieldsCollection $customFields */ public function updateCustomFields(int $conversationId, $customFields): void {