From dd74bdbe5bdaa8db4d906be515a2471a10745a72 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Thu, 27 Apr 2023 18:30:13 +0200 Subject: [PATCH 1/8] inboxes > * get list * get inbox attributes * create * delete --- src/Api/Sandbox/Inbox.php | 76 +++++++++++++++++++++++++++++++++++ src/MailtrapSandboxClient.php | 2 + 2 files changed, 78 insertions(+) create mode 100644 src/Api/Sandbox/Inbox.php diff --git a/src/Api/Sandbox/Inbox.php b/src/Api/Sandbox/Inbox.php new file mode 100644 index 0000000..5589114 --- /dev/null +++ b/src/Api/Sandbox/Inbox.php @@ -0,0 +1,76 @@ +handleResponse($this->httpGet( + sprintf('%s/api/accounts/%s/inboxes', $this->getHost(), $accountId) + )); + } + + /** + * Get inbox attributes by inbox id. See the list of attributes in the example + * + * @param int $accountId + * @param int $inboxId + * + * @return ResponseInterface + */ + public function getInboxAttributes(int $accountId, int $inboxId): ResponseInterface + { + return $this->handleResponse($this->httpGet( + sprintf('%s/api/accounts/%s/inboxes/%s', $this->getHost(), $accountId, $inboxId) + )); + } + + /** + * Create an inbox in a project. + * + * @param int $accountId + * @param int $projectId + * @param string $inboxName + * + * @return ResponseInterface + */ + public function create(int $accountId, int $projectId, string $inboxName): ResponseInterface + { + return $this->handleResponse( + $this->httpPost( + sprintf('%s/api/accounts/%s/projects/%s/inboxes', $this->getHost(), $accountId, $projectId), + [], + ['inbox' => ['name' => $inboxName]] + ) + ); + } + + /** + * Delete an inbox with all its emails. + * + * @param int $accountId + * @param int $inboxId + * + * @return ResponseInterface + */ + public function delete(int $accountId, int $inboxId): ResponseInterface + { + return $this->handleResponse($this->httpDelete( + sprintf('%s/api/accounts/%s/inboxes/%s', $this->getHost(), $accountId, $inboxId) + )); + } +} diff --git a/src/MailtrapSandboxClient.php b/src/MailtrapSandboxClient.php index f17d56d..9c1ecc7 100644 --- a/src/MailtrapSandboxClient.php +++ b/src/MailtrapSandboxClient.php @@ -9,6 +9,7 @@ /** * @method Api\Sandbox\Emails emails * @method Api\Sandbox\Project projects + * @method Api\Sandbox\Inbox inboxes * * Class MailtrapSandboxClient */ @@ -17,5 +18,6 @@ final class MailtrapSandboxClient extends AbstractMailtrapClient public const API_MAPPING = [ 'emails' => Api\Sandbox\Emails::class, 'projects' => Api\Sandbox\Project::class, + 'inboxes' => Api\Sandbox\Inbox::class, ]; } From c9a3153c88337bf452a678d63013544f858ff7e5 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Sun, 30 Apr 2023 20:08:34 +0200 Subject: [PATCH 2/8] inboxes > update --- src/Api/Sandbox/Inbox.php | 34 ++++++++++++- src/Api/Sandbox/Project.php | 4 +- src/DTO/Request/Inbox.php | 49 +++++++++++++++++++ .../Permission/PermissionInterface.php | 11 ++--- src/DTO/Request/RequestInterface.php | 13 +++++ src/Exception/HttpClientException.php | 2 +- tests/Api/General/UserTest.php | 2 +- 7 files changed, 101 insertions(+), 14 deletions(-) create mode 100644 src/DTO/Request/Inbox.php create mode 100644 src/DTO/Request/RequestInterface.php diff --git a/src/Api/Sandbox/Inbox.php b/src/Api/Sandbox/Inbox.php index 5589114..b380498 100644 --- a/src/Api/Sandbox/Inbox.php +++ b/src/Api/Sandbox/Inbox.php @@ -2,13 +2,15 @@ namespace Mailtrap\Api\Sandbox; -use Mailtrap\Api\AbstractEmails; +use Mailtrap\Api\AbstractApi; +use Mailtrap\DTO\Request\Inbox as InboxRequest; +use Mailtrap\Exception\RuntimeException; use Psr\Http\Message\ResponseInterface; /** * Class Inbox */ -class Inbox extends AbstractEmails implements SandboxInterface +class Inbox extends AbstractApi implements SandboxInterface { /** * Get a list of inboxes. @@ -73,4 +75,32 @@ public function delete(int $accountId, int $inboxId): ResponseInterface sprintf('%s/api/accounts/%s/inboxes/%s', $this->getHost(), $accountId, $inboxId) )); } + + /** + * Update inbox name and/or inbox email username. + * + * @param int $accountId + * @param int $inboxId + * @param InboxRequest $updateInbox + * + * @return ResponseInterface + */ + public function update(int $accountId, int $inboxId, InboxRequest $updateInbox): ResponseInterface + { + return $this->handleResponse($this->httpPatch( + sprintf('%s/api/accounts/%s/inboxes/%s', $this->getHost(), $accountId, $inboxId), + [], + ['inbox' => $this->getUpdatePayload($updateInbox)] + )); + } + + private function getUpdatePayload(InboxRequest $updateInbox): array + { + $result = $updateInbox->toArray(); + if (empty($result)) { + throw new RuntimeException('At least one inbox parameter should be populated ("name" or "email_username")'); + } + + return $result; + } } diff --git a/src/Api/Sandbox/Project.php b/src/Api/Sandbox/Project.php index 6acb4c7..74869d7 100644 --- a/src/Api/Sandbox/Project.php +++ b/src/Api/Sandbox/Project.php @@ -4,13 +4,13 @@ namespace Mailtrap\Api\Sandbox; -use Mailtrap\Api\AbstractEmails; +use Mailtrap\Api\AbstractApi; use Psr\Http\Message\ResponseInterface; /** * Class Project */ -class Project extends AbstractEmails implements SandboxInterface +class Project extends AbstractApi implements SandboxInterface { /** * List projects and their inboxes to which the API token has access. diff --git a/src/DTO/Request/Inbox.php b/src/DTO/Request/Inbox.php new file mode 100644 index 0000000..1c4917d --- /dev/null +++ b/src/DTO/Request/Inbox.php @@ -0,0 +1,49 @@ +name = $name; + $this->emailUsername = $emailUsername; + } + + /** + * @return string|null + */ + public function getName(): ?string + { + return $this->name; + } + + /** + * @return string|null + */ + public function getEmailUsername(): ?string + { + return $this->emailUsername; + } + + public function toArray(): array + { + $array = []; + + if (!empty($this->getName())) { + $array['name'] = $this->getName(); + } + + if (!empty($this->getEmailUsername())) { + $array['email_username'] = $this->getEmailUsername(); + } + + return $array; + } +} diff --git a/src/DTO/Request/Permission/PermissionInterface.php b/src/DTO/Request/Permission/PermissionInterface.php index c66cdb8..aa00445 100644 --- a/src/DTO/Request/Permission/PermissionInterface.php +++ b/src/DTO/Request/Permission/PermissionInterface.php @@ -2,7 +2,9 @@ namespace Mailtrap\DTO\Request\Permission; -interface PermissionInterface +use Mailtrap\DTO\Request\RequestInterface; + +interface PermissionInterface extends RequestInterface { public const TYPE_ACCOUNT = 'account'; public const TYPE_BILLING = 'billing'; @@ -23,11 +25,4 @@ public function getResourceId(): string; * @return string */ public function getResourceType(): string; - - /** - * Get permission as array - * - * @return array - */ - public function toArray(): array; } diff --git a/src/DTO/Request/RequestInterface.php b/src/DTO/Request/RequestInterface.php new file mode 100644 index 0000000..0b6fe85 --- /dev/null +++ b/src/DTO/Request/RequestInterface.php @@ -0,0 +1,13 @@ + 'Bad request. Fix errors listed in response before retrying.', 401 => 'Unauthorized. Make sure you are sending correct credentials with the request before retrying.', 403 => 'Forbidden. Make sure domain verification process is completed or check your permissions.', - 404 => 'Not found.', + 404 => 'The requested entity does not found.', ]; public static function createFromResponse(ResponseInterface $response): HttpClientException diff --git a/tests/Api/General/UserTest.php b/tests/Api/General/UserTest.php index 21d88ce..e882ec4 100644 --- a/tests/Api/General/UserTest.php +++ b/tests/Api/General/UserTest.php @@ -163,7 +163,7 @@ public function test404InvalidRemove(): void $this->expectException(HttpClientException::class); $this->expectExceptionMessage( - 'Not found. Errors: Not Found.' + 'The requested entity does not found. Errors: Not Found.' ); $this->user->delete(self::FAKE_ACCOUNT_ID, self::FAKE_ACCOUNT_ACCESS_ID); From 1999af26d592aa6403fc0d0518ac0a2a0be1dfb2 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Sun, 30 Apr 2023 20:33:09 +0200 Subject: [PATCH 3/8] inboxes > * clean * markAsRead * resetSmtpCredentials * toggleEmailAddress * resetEmailAddress --- src/Api/Sandbox/Inbox.php | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/Api/Sandbox/Inbox.php b/src/Api/Sandbox/Inbox.php index b380498..a85ff20 100644 --- a/src/Api/Sandbox/Inbox.php +++ b/src/Api/Sandbox/Inbox.php @@ -94,6 +94,81 @@ public function update(int $accountId, int $inboxId, InboxRequest $updateInbox): )); } + /** + * Delete all messages (emails) from inbox. + * + * @param int $accountId + * @param int $inboxId + * + * @return ResponseInterface + */ + public function clean(int $accountId, int $inboxId): ResponseInterface + { + return $this->handleResponse($this->httpPatch( + sprintf('%s/api/accounts/%s/inboxes/%s/clean', $this->getHost(), $accountId, $inboxId) + )); + } + + /** + * Mark all messages in the inbox as read. + * + * @param int $accountId + * @param int $inboxId + * + * @return ResponseInterface + */ + public function markAsRead(int $accountId, int $inboxId): ResponseInterface + { + return $this->handleResponse($this->httpPatch( + sprintf('%s/api/accounts/%s/inboxes/%s/all_read', $this->getHost(), $accountId, $inboxId) + )); + } + + /** + * Reset SMTP credentials of the inbox. + * + * @param int $accountId + * @param int $inboxId + * + * @return ResponseInterface + */ + public function resetSmtpCredentials(int $accountId, int $inboxId): ResponseInterface + { + return $this->handleResponse($this->httpPatch( + sprintf('%s/api/accounts/%s/inboxes/%s/reset_credentials', $this->getHost(), $accountId, $inboxId) + )); + } + + /** + * Turn the email address of the inbox on/off. + * + * @param int $accountId + * @param int $inboxId + * + * @return ResponseInterface + */ + public function toggleEmailAddress(int $accountId, int $inboxId): ResponseInterface + { + return $this->handleResponse($this->httpPatch( + sprintf('%s/api/accounts/%s/inboxes/%s/toggle_email_username', $this->getHost(), $accountId, $inboxId) + )); + } + + /** + * Reset username of email address per inbox. + * + * @param int $accountId + * @param int $inboxId + * + * @return ResponseInterface + */ + public function resetEmailAddress(int $accountId, int $inboxId): ResponseInterface + { + return $this->handleResponse($this->httpPatch( + sprintf('%s/api/accounts/%s/inboxes/%s/reset_email_username', $this->getHost(), $accountId, $inboxId) + )); + } + private function getUpdatePayload(InboxRequest $updateInbox): array { $result = $updateInbox->toArray(); From 6d4d3042a5d6e33ec4f625a4170a14f3261ec620 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Sun, 30 Apr 2023 20:42:08 +0200 Subject: [PATCH 4/8] inboxes > examples --- examples/sandbox/inboxes.php | 198 +++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 examples/sandbox/inboxes.php diff --git a/examples/sandbox/inboxes.php b/examples/sandbox/inboxes.php new file mode 100644 index 0000000..a3b1467 --- /dev/null +++ b/examples/sandbox/inboxes.php @@ -0,0 +1,198 @@ +sandbox()->inboxes()->getList($accountId); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + + +/** + * Create an inbox + * + * POST https://mailtrap.io/api/accounts/{account_id}/projects/{project_id}/inboxes + */ +try { + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); + $projectId = getenv('MAILTRAP_PROJECT_ID'); + $inboxName = 'First inbox'; + + $response = $mailtrap->sandbox()->inboxes()->create($accountId, $projectId, $inboxName); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + + +/** + * Get inbox attributes + * + * GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id} + */ +try { + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); + $inboxId = getenv('MAILTRAP_INBOX_ID'); + + $response = $mailtrap->sandbox()->inboxes()->getInboxAttributes($accountId, $inboxId); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + + +/** + * Delete an inbox + * + * DELETE https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id} + */ +try { + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); + $inboxId = getenv('MAILTRAP_INBOX_ID'); + + $response = $mailtrap->sandbox()->inboxes()->delete($accountId, $inboxId); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + + +/** + * Reset email address + * + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/reset_email_username + */ +try { + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); + $inboxId = getenv('MAILTRAP_INBOX_ID'); + + $response = $mailtrap->sandbox()->inboxes()->resetEmailAddress($accountId, $inboxId); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + + +/** + * Enable/Disable email address + * + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/toggle_email_username + */ +try { + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); + $inboxId = getenv('MAILTRAP_INBOX_ID'); + + $response = $mailtrap->sandbox()->inboxes()->toggleEmailAddress($accountId, $inboxId); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + + +/** + * Reset credentials + * + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/reset_credentials + */ +try { + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); + $inboxId = getenv('MAILTRAP_INBOX_ID'); + + $response = $mailtrap->sandbox()->inboxes()->resetSmtpCredentials($accountId, $inboxId); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + + +/** + * Mark as read + * + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/all_read + */ +try { + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); + $inboxId = getenv('MAILTRAP_INBOX_ID'); + + $response = $mailtrap->sandbox()->inboxes()->markAsRead($accountId, $inboxId); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + + +/** + * Clean inbox + * + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/clean + */ +try { + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); + $inboxId = getenv('MAILTRAP_INBOX_ID'); + + $response = $mailtrap->sandbox()->inboxes()->clean($accountId, $inboxId); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + + +/** + * Update an inbox + * + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id} + */ +try { + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); + $inboxId = getenv('MAILTRAP_INBOX_ID'); + $newInboxName = 'New inbox name'; + $newEmailUsername = 'new-email-username'; + + $response = $mailtrap->sandbox()->inboxes()->update( + $accountId, + $inboxId, + new Inbox($newInboxName, $newEmailUsername) + ); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} From 65cb33b509924ab23372a81364577cb0f2b18c60 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Sun, 30 Apr 2023 20:56:25 +0200 Subject: [PATCH 5/8] inboxes > update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0535c2..6d80435 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [1.5.0] - 2023-05-03 + +- Support sandbox inbox endpoints. Examples [here](examples/sandbox/inboxes.php) + + ## [1.4.0] - 2023-04-20 - Support general permission endpoints. Examples [here](examples/general/permissions.php) From 3fe0eea4debc2987da7a981a9a6d6c029f3e6d8f Mon Sep 17 00:00:00 2001 From: gaalferov Date: Sun, 30 Apr 2023 21:05:57 +0200 Subject: [PATCH 6/8] inboxes > skip bridge folder from the psalm analyze --- psalm.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/psalm.xml b/psalm.xml index 09e0c65..0cc97c2 100644 --- a/psalm.xml +++ b/psalm.xml @@ -14,7 +14,7 @@ - + From 8a143e797065c76ef5d8381260c935fe7d68c543 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Wed, 3 May 2023 14:13:13 +0200 Subject: [PATCH 7/8] inboxes > add tests for inbox endpoints --- CHANGELOG.md | 2 +- tests/Api/Sandbox/InboxTest.php | 416 ++++++++++++++++++++++++++++++++ tests/MailtrapTestCase.php | 1 + 3 files changed, 418 insertions(+), 1 deletion(-) create mode 100644 tests/Api/Sandbox/InboxTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d80435..e150167 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## [1.5.0] - 2023-05-03 +## [1.5.0] - 2023-05-04 - Support sandbox inbox endpoints. Examples [here](examples/sandbox/inboxes.php) diff --git a/tests/Api/Sandbox/InboxTest.php b/tests/Api/Sandbox/InboxTest.php new file mode 100644 index 0000000..9677330 --- /dev/null +++ b/tests/Api/Sandbox/InboxTest.php @@ -0,0 +1,416 @@ +inbox = $this->getMockBuilder(Inbox::class) + ->onlyMethods(['httpGet', 'httpPost', 'httpPatch', 'httpDelete']) + ->setConstructorArgs([$this->getConfigMock()]) + ->getMock() + ; + } + + protected function tearDown(): void + { + $this->inbox = null; + + parent::tearDown(); + } + + public function testGetList(): void + { + $this->inbox->expects($this->once()) + ->method('httpGet') + ->with(AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/inboxes') + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedInboxData()))); + + $response = $this->inbox->getList(self::FAKE_ACCOUNT_ID); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertCount(2, $responseData); + $this->assertArrayHasKey('id', array_shift($responseData)); + } + + public function testGetInboxAttributes(): void + { + $this->inbox->expects($this->once()) + ->method('httpGet') + ->with(AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/inboxes/' . self::FAKE_INBOX_ID) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedInboxData()[0]))); + + $response = $this->inbox->getInboxAttributes(self::FAKE_ACCOUNT_ID, self::FAKE_INBOX_ID); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertArrayHasKey('id', $responseData); + $this->assertEquals(self::FAKE_INBOX_ID, $responseData['id']); + $this->assertEquals('Admin Inbox', $responseData['name']); + $this->assertCount(4, $responseData['permissions']); + } + + public function testValidCreate(): void + { + $expectedData = [ + "id" => self::FAKE_INBOX_ID, + "name" => "My new inbox", + "username" => "24a5f9a9947fc7", + "password" => "a41e3846e78543", + "max_size" => 600, + "status" => "active", + "email_username" => "d008137b46-4d932f", + "email_username_enabled" => false, + "sent_messages_count" => 0, + "forwarded_messages_count" => 0, + "used" => false, + "forward_from_email_address" => "a3336-i3866@forward.mailtrap.info", + "project_id" => 2424, + "domain" => "localhost", + "pop3_domain" => "localhost", + "email_domain" => "localhost", + "emails_count" => 0, + "emails_unread_count" => 0, + "last_message_sent_at" => null, + "smtp_ports" => [ + 25, + 465, + 587, + 2525 + ], + "pop3_ports" => [ + 1100, + 9950 + ], + "max_message_size" => 15728640, + "permissions" => [ + "can_read" => true, + "can_update" => true, + "can_destroy" => true, + "can_leave" => false + ] + ]; + + $this->inbox->expects($this->once()) + ->method('httpPost') + ->with( + AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/projects/' . self::FAKE_PROJECT_ID . '/inboxes', + [], + ['inbox' => ['name' => $expectedData['name']]] + ) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedData))); + + $response = $this->inbox->create(self::FAKE_ACCOUNT_ID, self::FAKE_PROJECT_ID, $expectedData['name']); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertArrayHasKey('id', $responseData); + $this->assertEquals(self::FAKE_INBOX_ID, $responseData['id']); + } + + public function testInvalidCreate(): void + { + $errorMsg = [ + "error" => [ + "name" => [ + "can't be blank", + ] + ] + ]; + $this->inbox->expects($this->once()) + ->method('httpPost') + ->with( + AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/projects/' . self::FAKE_PROJECT_ID . '/inboxes', + ) + ->willReturn(new Response(422, ['Content-Type' => 'application/json'], json_encode($errorMsg))); + + $this->expectException(HttpClientException::class); + $this->expectExceptionMessage( + 'Errors: name -> can\'t be blank.' + ); + + $this->inbox->create(self::FAKE_ACCOUNT_ID, self::FAKE_PROJECT_ID, 'a'); + } + + public function testValidDelete(): void + { + $this->inbox->expects($this->once()) + ->method('httpDelete') + ->with( + AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/inboxes/' . self::FAKE_INBOX_ID + ) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedInboxData()[0]))); + + $response = $this->inbox->delete(self::FAKE_ACCOUNT_ID, self::FAKE_INBOX_ID); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertArrayHasKey('id', $responseData); + $this->assertEquals(self::FAKE_INBOX_ID, $responseData['id']); + } + + public function testUpdate(): void + { + $newName = 'New inbox name'; + $emailUsername = 'new-email-username'; + $inboxRequest = new InboxRequest($newName, $emailUsername); + $expectedData = [ + "id" => self::FAKE_INBOX_ID, + "name" => $newName, + "username" => "24a5f9a9947fc7", + "password" => "a41e3846e78543", + "max_size" => 600, + "status" => "active", + "email_username" => $emailUsername, + "email_username_enabled" => false, + "sent_messages_count" => 0, + "forwarded_messages_count" => 0, + "used" => false, + "forward_from_email_address" => "a3336-i3866@forward.mailtrap.info", + "project_id" => 2424, + "domain" => "localhost", + "pop3_domain" => "localhost", + "email_domain" => "localhost", + "emails_count" => 0, + "emails_unread_count" => 0, + "last_message_sent_at" => null, + "smtp_ports" => [ + 25, + 465, + 587, + 2525 + ], + "pop3_ports" => [ + 1100, + 9950 + ], + "max_message_size" => 15728640, + "permissions" => [ + "can_read" => true, + "can_update" => true, + "can_destroy" => true, + "can_leave" => false + ] + ]; + + $this->inbox->expects($this->once()) + ->method('httpPatch') + ->with( + AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/inboxes/' . self::FAKE_INBOX_ID, + [], + ['inbox' => $inboxRequest->toArray()] + ) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedData))); + + $response = $this->inbox->update(self::FAKE_ACCOUNT_ID, self::FAKE_INBOX_ID, $inboxRequest); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertArrayHasKey('id', $responseData); + $this->assertEquals(self::FAKE_INBOX_ID, $responseData['id']); + $this->assertEquals($newName, $responseData['name']); + $this->assertEquals($emailUsername, $responseData['email_username']); + } + + public function testClean(): void + { + $this->inbox->expects($this->once()) + ->method('httpPatch') + ->with( + AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/inboxes/' . self::FAKE_INBOX_ID . '/clean', + ) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedInboxData()[0]))); + + $response = $this->inbox->clean(self::FAKE_ACCOUNT_ID, self::FAKE_INBOX_ID); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertArrayHasKey('id', $responseData); + $this->assertEquals(self::FAKE_INBOX_ID, $responseData['id']); + } + + public function testMarkAsRead(): void + { + $this->inbox->expects($this->once()) + ->method('httpPatch') + ->with( + AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/inboxes/' . self::FAKE_INBOX_ID . '/all_read', + ) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedInboxData()[0]))); + + $response = $this->inbox->markAsRead(self::FAKE_ACCOUNT_ID, self::FAKE_INBOX_ID); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertArrayHasKey('id', $responseData); + $this->assertEquals(self::FAKE_INBOX_ID, $responseData['id']); + } + + public function testResetSmtpCredentials(): void + { + $expectedData = $this->getExpectedInboxData()[0]; + $expectedData['username'] = 'new_username'; + $expectedData['password'] = 'new_password'; + $this->inbox->expects($this->once()) + ->method('httpPatch') + ->with( + AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/inboxes/' . self::FAKE_INBOX_ID . '/reset_credentials', + ) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedData))); + + $response = $this->inbox->resetSmtpCredentials(self::FAKE_ACCOUNT_ID, self::FAKE_INBOX_ID); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertArrayHasKey('id', $responseData); + $this->assertEquals(self::FAKE_INBOX_ID, $responseData['id']); + $this->assertEquals($expectedData['username'], $responseData['username']); + $this->assertEquals($expectedData['password'], $responseData['password']); + } + + public function testToggleEmailAddress(): void + { + $expectedData = $this->getExpectedInboxData()[0]; + $expectedData['email_username_enabled'] = true; + + $this->inbox->expects($this->once()) + ->method('httpPatch') + ->with( + AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/inboxes/' . self::FAKE_INBOX_ID . '/toggle_email_username', + ) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedData))); + + $response = $this->inbox->toggleEmailAddress(self::FAKE_ACCOUNT_ID, self::FAKE_INBOX_ID); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertArrayHasKey('id', $responseData); + $this->assertEquals(self::FAKE_INBOX_ID, $responseData['id']); + $this->assertEquals($expectedData['email_username_enabled'], $responseData['username']); + } + + public function testResetEmailAddress(): void + { + $expectedData = $this->getExpectedInboxData()[0]; + $expectedData['email_username'] = 'new_email_username'; + + $this->inbox->expects($this->once()) + ->method('httpPatch') + ->with( + AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/inboxes/' . self::FAKE_INBOX_ID . '/reset_email_username', + ) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedData))); + + $response = $this->inbox->resetEmailAddress(self::FAKE_ACCOUNT_ID, self::FAKE_INBOX_ID); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertArrayHasKey('id', $responseData); + $this->assertEquals(self::FAKE_INBOX_ID, $responseData['id']); + $this->assertEquals($expectedData['email_username'], $responseData['email_username']); + } + + private function getExpectedInboxData(): array + { + return [ + [ + "id" => self::FAKE_INBOX_ID, + "name" => "Admin Inbox", + "username" => "b3a87978452ae1", + "password" => "6be9fcfc613a7c", + "max_size" => 0, + "status" => "active", + "email_username" => "b7eae548c3-54c542", + "email_username_enabled" => false, + "sent_messages_count" => 52, + "forwarded_messages_count" => 0, + "used" => false, + "forward_from_email_address" => "a3538-i4088@forward.mailtrap.info", + "project_id" => 2293, + "domain" => "localhost", + "pop3_domain" => "localhost", + "email_domain" => "localhost", + "emails_count" => 0, + "emails_unread_count" => 0, + "last_message_sent_at" => null, + "smtp_ports" => [ + 25, + 465, + 587, + 2525 + ], + "pop3_ports" => [ + 1100, + 9950 + ], + "max_message_size" => 5242880, + "permissions" => [ + "can_read" => true, + "can_update" => true, + "can_destroy" => true, + "can_leave" => true + ] + ], + [ + "id" => 4089, + "name" => "Viewer Inbox", + "username" => "1d0aa0282f7712", + "password" => "5667a20f611ae7", + "max_size" => 0, + "status" => "active", + "email_username" => "f25402f8df-b77899", + "email_username_enabled" => false, + "sent_messages_count" => 527, + "forwarded_messages_count" => 0, + "used" => false, + "forward_from_email_address" => "a3538-i4089@forward.mailtrap.info", + "project_id" => 2293, + "domain" => "localhost", + "pop3_domain" => "localhost", + "email_domain" => "localhost", + "emails_count" => 0, + "emails_unread_count" => 0, + "last_message_sent_at" => null, + "smtp_ports" => [ + 25, + 465, + 587, + 2525 + ], + "pop3_ports" => [ + 1100, + 9950 + ], + "max_message_size" => 5242880, + "permissions" => [ + "can_read" => true, + "can_update" => false, + "can_destroy" => false, + "can_leave" => true + ] + ] + ]; + } +} diff --git a/tests/MailtrapTestCase.php b/tests/MailtrapTestCase.php index 7ec3c33..6167306 100644 --- a/tests/MailtrapTestCase.php +++ b/tests/MailtrapTestCase.php @@ -19,6 +19,7 @@ abstract class MailtrapTestCase extends TestCase public const FAKE_ACCOUNT_ID = 10001; public const FAKE_ACCOUNT_ACCESS_ID = 1000001; public const FAKE_PROJECT_ID = 2436; + public const FAKE_INBOX_ID = 4015; protected function getHttpClientMock(): ClientInterface { From 0cc0793d2350b7573a2e11128ac35099cc5fc332 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Thu, 4 May 2023 11:08:01 +0200 Subject: [PATCH 8/8] inboxes > fix error msg --- src/Exception/HttpClientException.php | 2 +- tests/Api/General/UserTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Exception/HttpClientException.php b/src/Exception/HttpClientException.php index c2ce416..5b9c2e1 100644 --- a/src/Exception/HttpClientException.php +++ b/src/Exception/HttpClientException.php @@ -17,7 +17,7 @@ class HttpClientException extends HttpException 400 => 'Bad request. Fix errors listed in response before retrying.', 401 => 'Unauthorized. Make sure you are sending correct credentials with the request before retrying.', 403 => 'Forbidden. Make sure domain verification process is completed or check your permissions.', - 404 => 'The requested entity does not found.', + 404 => 'The requested entity has not been found.', ]; public static function createFromResponse(ResponseInterface $response): HttpClientException diff --git a/tests/Api/General/UserTest.php b/tests/Api/General/UserTest.php index e882ec4..915b0ec 100644 --- a/tests/Api/General/UserTest.php +++ b/tests/Api/General/UserTest.php @@ -163,7 +163,7 @@ public function test404InvalidRemove(): void $this->expectException(HttpClientException::class); $this->expectExceptionMessage( - 'The requested entity does not found. Errors: Not Found.' + 'The requested entity has not been found. Errors: Not Found.' ); $this->user->delete(self::FAKE_ACCOUNT_ID, self::FAKE_ACCOUNT_ACCESS_ID);