From b961d83b2b2800eb622bc83d8fad43a834f8893e Mon Sep 17 00:00:00 2001 From: Wouter van Marrum Date: Mon, 20 May 2024 11:18:13 +0200 Subject: [PATCH 1/4] feat(conversations): Added "missing" functionality to get truncated threads. --- src/Conversations/ConversationFilters.php | 17 ++++++ .../Conversations/ConversationFiltersTest.php | 4 +- .../ConversationIntegrationTest.php | 18 ++++++ tests/Payloads/ConversationPayloads.php | 56 +++++++++++++++++-- 4 files changed, 89 insertions(+), 6 deletions(-) diff --git a/src/Conversations/ConversationFilters.php b/src/Conversations/ConversationFilters.php index 53a1b17c..b5382a99 100644 --- a/src/Conversations/ConversationFilters.php +++ b/src/Conversations/ConversationFilters.php @@ -66,6 +66,11 @@ class ConversationFilters */ private $query; + /** + * @var string + */ + public $embed; + public function getParams(): array { $params = [ @@ -78,6 +83,7 @@ public function getParams(): array 'sortField' => $this->sortField, 'sortOrder' => $this->sortOrder, 'query' => $this->query, + 'embed' => $this->embed, ]; if (\is_array($this->tag)) { @@ -240,4 +246,15 @@ public function withQuery(string $query): ConversationFilters return $filters; } + public function withEmbed(string $embed): ConversationFilters + { + Assert::oneOf($embed, [ + 'threads', + ]); + + $filters = clone $this; + $filters->embed = $embed; + + return $filters; + } } diff --git a/tests/Conversations/ConversationFiltersTest.php b/tests/Conversations/ConversationFiltersTest.php index 31934bbe..e76739bd 100644 --- a/tests/Conversations/ConversationFiltersTest.php +++ b/tests/Conversations/ConversationFiltersTest.php @@ -32,7 +32,8 @@ public function testGetParams() ->sortField('createdAt') ->sortOrder('asc') ->withQuery('query') - ->byCustomField(123, 'blue'); + ->byCustomField(123, 'blue') + ->withEmbed('threads'); $this->assertSame([ 'mailbox' => 1, @@ -44,6 +45,7 @@ public function testGetParams() 'sortField' => 'createdAt', 'sortOrder' => 'asc', 'query' => 'query', + 'embed' => 'threads', 'tag' => 'testing', 'customFieldsByIds' => '123:blue', ], $filters->getParams()); diff --git a/tests/Conversations/ConversationIntegrationTest.php b/tests/Conversations/ConversationIntegrationTest.php index c6b71241..84c2f73a 100644 --- a/tests/Conversations/ConversationIntegrationTest.php +++ b/tests/Conversations/ConversationIntegrationTest.php @@ -230,6 +230,24 @@ public function testGetConversationPreloadsThreads() ]); } + public function testGetConversationWithEmbedThreads() + { + $this->stubResponse($this->getResponse(200, ConversationPayloads::getConversations(1, 10))); + + $filters = (new ConversationFilters()) + ->withEmbed('threads'); + + $conversations = $this->client->conversations()->list($filters); + + $this->assertCount(10, $conversations); + $this->assertInstanceOf(Conversation::class, $conversations[0]); + $this->assertInstanceOf(CustomerThread::class, $conversations[0]['_embedded']['threads'][0]); + + $this->verifyMultipleRequests([ + ['GET', 'https://api.helpscout.net/v2/conversations/1'], + ]); + } + public function testListConversations() { $this->stubResponse( diff --git a/tests/Payloads/ConversationPayloads.php b/tests/Payloads/ConversationPayloads.php index d589ca76..02e4f84e 100644 --- a/tests/Payloads/ConversationPayloads.php +++ b/tests/Payloads/ConversationPayloads.php @@ -11,15 +11,15 @@ public static function getConversation(int $id): string return json_encode(static::conversation($id)); } - public static function getConversations(int $pageNumber, int $totalElements): string + public static function getConversations(int $pageNumber, int $totalElements, bool $embedThread = false): string { $pageSize = 10; $pageElements = min($totalElements, $pageSize); $totalPages = ceil($totalElements / $pageSize); // Create embedded resources - $conversations = array_map(function ($id) { - return static::conversation($id); + $conversations = array_map(function ($id) use ($embedThread) { + return static::conversation($id, $embedThread); }, range(1, $pageElements)); $data = [ @@ -60,9 +60,9 @@ public static function getConversations(int $pageNumber, int $totalElements): st return json_encode($data); } - private static function conversation(int $id): array + private static function conversation(int $id, bool $embedThread = false): array { - return [ + $conversation = [ 'id' => $id, 'number' => 15473, 'threads' => 2, @@ -144,6 +144,52 @@ private static function conversation(int $id): array ], ], ]; + + if ($embedThread) { + $conversation['_embedded']['threads'] = [ + [ + '_embedded' => [ + 'threads' => [ + [ + 'id' => 1, + 'type' => 'lineitem', + 'status' => 'closed', + 'action' => [ + 'type' => 'default', + 'text' => 'John marked as Closed', + 'assiciatedEntities' => [], + ], + 'source' => [ + 'type' => 'web', + 'via' => 'user', + ], + 'createdBy' => [ + 'id' => 1, + 'type' => 'user', + 'first' => 'John', + 'last' => 'Doe', + 'email' => 'john.doe@example.com', + 'to' => [], + 'cc' => [], + 'bcc' => [], + 'createdAt' => '2017-04-21T14:39:56Z', + ], + '_embedded' => [ + 'attachments' => [], + ], + '_links' => [ + 'createdByUser' => [ + 'href' => 'https://api.helpscout.net/v2/users/1', + ] + ] + ], + ], + ], + ], + ]; + } + + return $conversation; } public static function getThreads(int $conversationId): string From 97696157c8cd18c27cdd4baa12ed74c138abcc5b Mon Sep 17 00:00:00 2001 From: Wouter van Marrum Date: Mon, 20 May 2024 13:45:12 +0200 Subject: [PATCH 2/4] feat(conversations): Fixed test logic to be setup as expected by the core code. --- .../ConversationIntegrationTest.php | 10 +-- tests/Payloads/ConversationPayloads.php | 81 +++++++++++-------- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/tests/Conversations/ConversationIntegrationTest.php b/tests/Conversations/ConversationIntegrationTest.php index 84c2f73a..278c05b7 100644 --- a/tests/Conversations/ConversationIntegrationTest.php +++ b/tests/Conversations/ConversationIntegrationTest.php @@ -232,7 +232,7 @@ public function testGetConversationPreloadsThreads() public function testGetConversationWithEmbedThreads() { - $this->stubResponse($this->getResponse(200, ConversationPayloads::getConversations(1, 10))); + $this->stubResponse($this->getResponse(200, ConversationPayloads::getConversations(1, 10, true))); $filters = (new ConversationFilters()) ->withEmbed('threads'); @@ -240,12 +240,10 @@ public function testGetConversationWithEmbedThreads() $conversations = $this->client->conversations()->list($filters); $this->assertCount(10, $conversations); - $this->assertInstanceOf(Conversation::class, $conversations[0]); - $this->assertInstanceOf(CustomerThread::class, $conversations[0]['_embedded']['threads'][0]); + $this->assertInstanceOf(Conversation::class, $firstConversation = $conversations[0]); + $this->assertInstanceOf(CustomerThread::class, $firstConversation->getThreads()->toArray()[0]); - $this->verifyMultipleRequests([ - ['GET', 'https://api.helpscout.net/v2/conversations/1'], - ]); + $this->verifySingleRequest('https://api.helpscout.net/v2/conversations?embed=threads'); } public function testListConversations() diff --git a/tests/Payloads/ConversationPayloads.php b/tests/Payloads/ConversationPayloads.php index 02e4f84e..e89cc597 100644 --- a/tests/Payloads/ConversationPayloads.php +++ b/tests/Payloads/ConversationPayloads.php @@ -148,47 +148,58 @@ private static function conversation(int $id, bool $embedThread = false): array if ($embedThread) { $conversation['_embedded']['threads'] = [ [ + 'id' => 1, + 'type' => 'customer', + 'status' => 'active', + 'state' => 'published', + 'action' => [ + 'type' => 'default', + 'associatedEntities' => [], + ], + 'body' => 'This is a test', + 'source' => [ + 'type' => 'email', + 'via' => 'user', + ], + 'customer' => [ + 'id' => 472611182, + 'first' => 'John', + 'last' => 'Doe', + 'photoUrl' => 'https://d33v4339jhl8k0.cloudfront.net/customer-avatar/05.png', + 'email' => 'john.doe@example.com', + ], + 'createdBy' => [ + 'id' => 1, + 'type' => 'user', + 'first' => 'John', + 'last' => 'Doe', + 'email' => 'john.doe@example.com', + 'to' => [], + 'cc' => [], + 'bcc' => [], + 'createdAt' => '2017-04-21T14:39:56Z', + ], + 'assignedTo' => [ + 'id' => 12, + 'first' => 'Help', + 'last' => 'Scout', + 'email' => 'none@nowhere.com', + ], + 'savedReplyId' => 0, '_embedded' => [ - 'threads' => [ - [ - 'id' => 1, - 'type' => 'lineitem', - 'status' => 'closed', - 'action' => [ - 'type' => 'default', - 'text' => 'John marked as Closed', - 'assiciatedEntities' => [], - ], - 'source' => [ - 'type' => 'web', - 'via' => 'user', - ], - 'createdBy' => [ - 'id' => 1, - 'type' => 'user', - 'first' => 'John', - 'last' => 'Doe', - 'email' => 'john.doe@example.com', - 'to' => [], - 'cc' => [], - 'bcc' => [], - 'createdAt' => '2017-04-21T14:39:56Z', - ], - '_embedded' => [ - 'attachments' => [], - ], - '_links' => [ - 'createdByUser' => [ - 'href' => 'https://api.helpscout.net/v2/users/1', - ] - ] - ], - ], + 'attachments' => [], ], + '_links' => [ + 'createdByUser' => [ + 'href' => 'https://api.helpscout.net/v2/users/1', + ] + ] ], ]; } +// var_dump($conversation); + return $conversation; } From bf290019cb449e882e0a530790bc81cb9f990c60 Mon Sep 17 00:00:00 2001 From: Miguel Rosales Sueiro Date: Tue, 11 Jun 2024 18:35:21 +0200 Subject: [PATCH 3/4] Remove leftover comment --- tests/Payloads/ConversationPayloads.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/Payloads/ConversationPayloads.php b/tests/Payloads/ConversationPayloads.php index e89cc597..cd96a945 100644 --- a/tests/Payloads/ConversationPayloads.php +++ b/tests/Payloads/ConversationPayloads.php @@ -18,9 +18,7 @@ public static function getConversations(int $pageNumber, int $totalElements, boo $totalPages = ceil($totalElements / $pageSize); // Create embedded resources - $conversations = array_map(function ($id) use ($embedThread) { - return static::conversation($id, $embedThread); - }, range(1, $pageElements)); + $conversations = array_map(fn($id) => static::conversation($id, $embedThread), range(1, $pageElements)); $data = [ '_embedded' => [ @@ -198,8 +196,6 @@ private static function conversation(int $id, bool $embedThread = false): array ]; } -// var_dump($conversation); - return $conversation; } From 8548471717d06518cf65b7975a4efa6f6dd0dd1c Mon Sep 17 00:00:00 2001 From: Miguel Rosales Sueiro Date: Tue, 11 Jun 2024 18:38:10 +0200 Subject: [PATCH 4/4] Put back traditional closure for compatibility --- tests/Payloads/ConversationPayloads.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Payloads/ConversationPayloads.php b/tests/Payloads/ConversationPayloads.php index cd96a945..e38c8014 100644 --- a/tests/Payloads/ConversationPayloads.php +++ b/tests/Payloads/ConversationPayloads.php @@ -18,7 +18,9 @@ public static function getConversations(int $pageNumber, int $totalElements, boo $totalPages = ceil($totalElements / $pageSize); // Create embedded resources - $conversations = array_map(fn($id) => static::conversation($id, $embedThread), range(1, $pageElements)); + $conversations = array_map(function ($id) use ($embedThread) { + return static::conversation($id, $embedThread); + }, range(1, $pageElements)); $data = [ '_embedded' => [