From 10305d1a4ee9d4f6d78bb9108ebb5b45a668cf69 Mon Sep 17 00:00:00 2001 From: Carl Sutherland Date: Fri, 22 Nov 2019 15:04:26 -0500 Subject: [PATCH 1/2] Support hydrating embedded threads. This pulls the code from https://github.com/helpscout/helpscout-api-php/pull/208 It keeps just the hydrating embedded threads while removing the part that added withEmbed to conversation filters. Co-authored-by: Ammeded by Matt Harrison --- src/Conversations/Conversation.php | 22 +++++++--- src/Conversations/ConversationFilters.php | 1 + tests/Conversations/ConversationTest.php | 52 +++++++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php index 0089558..24ab466 100644 --- a/src/Conversations/Conversation.php +++ b/src/Conversations/Conversation.php @@ -176,19 +176,19 @@ public function hydrate(array $data, array $embedded = []) $this->setThreadCount($data['threadCount'] ?? null); } + if (isset($embedded['threads'])) { + $this->hydrateThreads($embedded['threads']); + } + if (isset($data['threads'])) { // On some API calls these value is used to pass the thread count if (is_numeric($data['threads'])) { $this->setThreadCount($data['threads']); } elseif (is_array($data['threads'])) { - $this->threads = new Collection(); - $threadFactory = new ThreadFactory(); - foreach ($data['threads'] as $threadData) { - $thread = $threadFactory->make($threadData['type'], $threadData); - $this->threads->append($thread); - } + $this->hydrateThreads($data['threads']); } } + $this->setNumber($data['number'] ?? null); $this->setType($data['type'] ?? null); $this->setFolderId($data['folderId'] ?? null); @@ -281,6 +281,16 @@ public function hydrate(array $data, array $embedded = []) } } + protected function hydrateThreads(array $threads): void + { + $this->threads = new Collection(); + $threadFactory = new ThreadFactory(); + foreach ($threads as $threadData) { + $thread = $threadFactory->make($threadData['type'], $threadData); + $this->threads->append($thread); + } + } + /** * {@inheritdoc} */ diff --git a/src/Conversations/ConversationFilters.php b/src/Conversations/ConversationFilters.php index 4fbec5a..53a1b17 100644 --- a/src/Conversations/ConversationFilters.php +++ b/src/Conversations/ConversationFilters.php @@ -239,4 +239,5 @@ public function withQuery(string $query): ConversationFilters return $filters; } + } diff --git a/tests/Conversations/ConversationTest.php b/tests/Conversations/ConversationTest.php index 203a3ab..5412251 100644 --- a/tests/Conversations/ConversationTest.php +++ b/tests/Conversations/ConversationTest.php @@ -163,6 +163,58 @@ public function testHydrateWhenCustomerExistsButIsNull() $this->assertFalse($conversation->wasCreatedByUser()); } + public function testHydrateEmbedded() + { + $conversation = new Conversation(); + $embeddedFixation = [ + 'threads' => [ + [ + 'id' => 2198262392, + 'assignedTo' => null, + 'status' => 'active', + 'createdAt' => '2019-02-28T15:48:20Z', + 'createdBy' => [ + 'id' => 179783313, + 'firstName' => 'John', + 'lastName' => 'Smith', + 'email' => 'john@ourcompany.com', + 'type' => 'customer', + ], + 'source' => [ + 'type' => 'email', + 'via' => 'customer', + ], + 'actionType' => null, + 'actionSourceId' => 0, + 'fromMailbox' => null, + 'type' => 'customer', + 'state' => 'published', + 'customer' => [ + 'id' => 179783313, + 'firstName' => 'Casey', + 'lastName' => 'Lockwood', + 'email' => 'casey@helpscout.com', + 'type' => 'customer', + ], + 'body' => 'Are you still interested in a demo?', + 'to' => [ + 'customer-address@gmail.com', + ], + 'cc' => null, + 'bcc' => null, + 'attachments' => null, + ], + ], + ]; + $conversation->hydrate(['threads' => 1], $embeddedFixation); + + $this->assertSame(1, $conversation->getThreadCount()); + + $thread = $conversation->getThreads()[0]; + $this->assertInstanceOf(CustomerThread::class, $thread); + $this->assertSame(2198262392, $thread->getId()); + } + public function testExtractsCreatedByUser() { $conversation = new Conversation(); From ca8ae948096eefa8d50ca35291d0992ff6ba6179 Mon Sep 17 00:00:00 2001 From: Miguel Rosales Sueiro Date: Mon, 18 Dec 2023 20:05:42 +0100 Subject: [PATCH 2/2] Use fake emails in tests --- tests/Conversations/ConversationTest.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/Conversations/ConversationTest.php b/tests/Conversations/ConversationTest.php index 5412251..d44c6f9 100644 --- a/tests/Conversations/ConversationTest.php +++ b/tests/Conversations/ConversationTest.php @@ -191,15 +191,13 @@ public function testHydrateEmbedded() 'state' => 'published', 'customer' => [ 'id' => 179783313, - 'firstName' => 'Casey', - 'lastName' => 'Lockwood', - 'email' => 'casey@helpscout.com', + 'firstName' => 'John', + 'lastName' => 'Smith', + 'email' => 'john@ourcompany.com', 'type' => 'customer', ], 'body' => 'Are you still interested in a demo?', - 'to' => [ - 'customer-address@gmail.com', - ], + 'to' => [], 'cc' => null, 'bcc' => null, 'attachments' => null,