diff --git a/src/Conversations/ConversationFilters.php b/src/Conversations/ConversationFilters.php index 53a1b17..b5382a9 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 31934bb..e76739b 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 c6b7124..278c05b 100644 --- a/tests/Conversations/ConversationIntegrationTest.php +++ b/tests/Conversations/ConversationIntegrationTest.php @@ -230,6 +230,22 @@ public function testGetConversationPreloadsThreads() ]); } + public function testGetConversationWithEmbedThreads() + { + $this->stubResponse($this->getResponse(200, ConversationPayloads::getConversations(1, 10, true))); + + $filters = (new ConversationFilters()) + ->withEmbed('threads'); + + $conversations = $this->client->conversations()->list($filters); + + $this->assertCount(10, $conversations); + $this->assertInstanceOf(Conversation::class, $firstConversation = $conversations[0]); + $this->assertInstanceOf(CustomerThread::class, $firstConversation->getThreads()->toArray()[0]); + + $this->verifySingleRequest('https://api.helpscout.net/v2/conversations?embed=threads'); + } + public function testListConversations() { $this->stubResponse( diff --git a/tests/Payloads/ConversationPayloads.php b/tests/Payloads/ConversationPayloads.php index d589ca7..e38c801 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,61 @@ private static function conversation(int $id): 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' => [ + 'attachments' => [], + ], + '_links' => [ + 'createdByUser' => [ + 'href' => 'https://api.helpscout.net/v2/users/1', + ] + ] + ], + ]; + } + + return $conversation; } public static function getThreads(int $conversationId): string