diff --git a/examples/contact-lists/all.php b/examples/contact-lists/all.php index edc7bc1..9058654 100644 --- a/examples/contact-lists/all.php +++ b/examples/contact-lists/all.php @@ -30,6 +30,21 @@ } +/** + * Get all Contact Lists filtered by name (case-insensitive prefix match). + * + * GET https://mailtrap.io/api/accounts/{account_id}/contacts/lists?search=news + */ +try { + $response = $contacts->getAllContactLists('news'); // Replace 'news' with your desired name prefix + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), PHP_EOL; +} + + /** * Get a specific Contact List by ID. * diff --git a/src/Api/General/Contact.php b/src/Api/General/Contact.php index 4a5b0ca..30c9e9e 100644 --- a/src/Api/General/Contact.php +++ b/src/Api/General/Contact.php @@ -27,12 +27,18 @@ public function __construct(ConfigInterface $config, private int $accountId) /** * Get all Contact Lists. * + * @param string|null $search Filter contact lists by name (case-insensitive prefix match), or null to get all. * @return ResponseInterface */ - public function getAllContactLists(): ResponseInterface + public function getAllContactLists(?string $search = null): ResponseInterface { + $parameters = []; + if ($search !== null) { + $parameters['search'] = $search; + } + return $this->handleResponse( - $this->httpGet($this->getBasePath() . '/lists') + $this->httpGet($this->getBasePath() . '/lists', $parameters) ); } diff --git a/tests/Api/General/ContactTest.php b/tests/Api/General/ContactTest.php index 70f00fe..5c3e38b 100644 --- a/tests/Api/General/ContactTest.php +++ b/tests/Api/General/ContactTest.php @@ -59,6 +59,26 @@ public function testGetAllContactLists(): void $this->assertArrayHasKey('id', $responseData[0]); } + public function testGetAllContactListsWithSearch(): void + { + $search = 'news'; + + $this->contact->expects($this->once()) + ->method('httpGet') + ->with( + AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/lists', + ['search' => $search] + ) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedContactLists()))); + + $response = $this->contact->getAllContactLists($search); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertCount(2, $responseData); + $this->assertArrayHasKey('id', $responseData[0]); + } + public function testGetContactList(): void { $contactListId = 1;