From d0730558ffd026357aeb9a2ee8b52ae115a378d9 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Thu, 4 May 2023 11:54:48 +0200 Subject: [PATCH 1/4] attachments > add 2 endpoints --- src/Api/Sandbox/Attachment.php | 54 ++++++++++++++++++++++++++++++++++ src/MailtrapSandboxClient.php | 8 +++-- 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/Api/Sandbox/Attachment.php diff --git a/src/Api/Sandbox/Attachment.php b/src/Api/Sandbox/Attachment.php new file mode 100644 index 0000000..7671d93 --- /dev/null +++ b/src/Api/Sandbox/Attachment.php @@ -0,0 +1,54 @@ +handleResponse($this->httpGet(sprintf( + '%s/api/accounts/%s/inboxes/%s/messages/%s/attachments', + $this->getHost(), + $accountId, + $inboxId, + $messageId + ))); + } + + /** + * Get message single attachment by id. + * + * @param int $accountId + * @param int $inboxId + * @param int $messageId + * @param int $attachmentId + * + * @return ResponseInterface + */ + public function getMessageAttachment(int $accountId, int $inboxId, int $messageId, int $attachmentId): ResponseInterface + { + return $this->handleResponse($this->httpGet(sprintf( + '%s/api/accounts/%s/inboxes/%s/messages/%s/attachments/%s', + $this->getHost(), + $accountId, + $inboxId, + $messageId, + $attachmentId + ))); + } +} diff --git a/src/MailtrapSandboxClient.php b/src/MailtrapSandboxClient.php index 9c1ecc7..a282ec4 100644 --- a/src/MailtrapSandboxClient.php +++ b/src/MailtrapSandboxClient.php @@ -7,9 +7,10 @@ use Mailtrap\Api; /** - * @method Api\Sandbox\Emails emails - * @method Api\Sandbox\Project projects - * @method Api\Sandbox\Inbox inboxes + * @method Api\Sandbox\Emails emails + * @method Api\Sandbox\Project projects + * @method Api\Sandbox\Inbox inboxes + * @method Api\Sandbox\Attachment attachments * * Class MailtrapSandboxClient */ @@ -19,5 +20,6 @@ final class MailtrapSandboxClient extends AbstractMailtrapClient 'emails' => Api\Sandbox\Emails::class, 'projects' => Api\Sandbox\Project::class, 'inboxes' => Api\Sandbox\Inbox::class, + 'attachments' => Api\Sandbox\Attachment::class ]; } From f07438ca03c5d285cf7980b77bceb42270ff1c32 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Thu, 4 May 2023 11:59:38 +0200 Subject: [PATCH 2/4] attachments > add SandboxInterface --- src/Api/Sandbox/Attachment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Api/Sandbox/Attachment.php b/src/Api/Sandbox/Attachment.php index 7671d93..020fb02 100644 --- a/src/Api/Sandbox/Attachment.php +++ b/src/Api/Sandbox/Attachment.php @@ -8,7 +8,7 @@ /** * Class Attachment */ -class Attachment extends AbstractApi +class Attachment extends AbstractApi implements SandboxInterface { /** * Get message attachments by inboxId and messageId. From f6867d3704c7c906688061ccdee4fcd787cce2bc Mon Sep 17 00:00:00 2001 From: gaalferov Date: Thu, 4 May 2023 12:09:11 +0200 Subject: [PATCH 3/4] attachments > add example file --- examples/sandbox/attachments.php | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/sandbox/attachments.php diff --git a/examples/sandbox/attachments.php b/examples/sandbox/attachments.php new file mode 100644 index 0000000..65a69e7 --- /dev/null +++ b/examples/sandbox/attachments.php @@ -0,0 +1,48 @@ +sandbox()->attachments()->getMessageAttachments($accountId, $inboxId, $messageId); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + +/** + * Get single attachment + * + * GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/attachments/{attachment_id} + */ +try { + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); + $inboxId = getenv('MAILTRAP_INBOX_ID'); + $messageId = getenv('MAILTRAP_MESSAGE_ID'); + $attachmentId = getenv('MAILTRAP_MESSAGE_ATTACHMENT_ID'); + + $response = $mailtrap->sandbox()->attachments()->getMessageAttachment($accountId, $inboxId, $messageId, $attachmentId); + + // print the response body (array) + var_dump(ResponseHelper::toArray($response)); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} From 0e14ea3937c71d3f5c894293f5eb99345bef1e01 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Thu, 4 May 2023 14:03:29 +0200 Subject: [PATCH 4/4] attachments > * add tests * add attachmentType param (optional) * update CHANGELOG --- CHANGELOG.md | 4 + examples/sandbox/attachments.php | 4 +- src/Api/Sandbox/Attachment.php | 39 ++++++--- tests/Api/Sandbox/AttachmentTest.php | 120 +++++++++++++++++++++++++++ tests/MailtrapTestCase.php | 2 + 5 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 tests/Api/Sandbox/AttachmentTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e150167..af237b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.6.0] - 2023-05-05 + +- Support sandbox attachment endpoints. Examples [here](examples/sandbox/attachments.php) + ## [1.5.0] - 2023-05-04 - Support sandbox inbox endpoints. Examples [here](examples/sandbox/inboxes.php) diff --git a/examples/sandbox/attachments.php b/examples/sandbox/attachments.php index 65a69e7..ed07ffb 100644 --- a/examples/sandbox/attachments.php +++ b/examples/sandbox/attachments.php @@ -19,8 +19,10 @@ $accountId = getenv('MAILTRAP_ACCOUNT_ID'); $inboxId = getenv('MAILTRAP_INBOX_ID'); $messageId = getenv('MAILTRAP_MESSAGE_ID'); + // optional (null|string) + $attachmentType = 'inline'; - $response = $mailtrap->sandbox()->attachments()->getMessageAttachments($accountId, $inboxId, $messageId); + $response = $mailtrap->sandbox()->attachments()->getMessageAttachments($accountId, $inboxId, $messageId, $attachmentType); // print the response body (array) var_dump(ResponseHelper::toArray($response)); diff --git a/src/Api/Sandbox/Attachment.php b/src/Api/Sandbox/Attachment.php index 020fb02..c819055 100644 --- a/src/Api/Sandbox/Attachment.php +++ b/src/Api/Sandbox/Attachment.php @@ -13,21 +13,36 @@ class Attachment extends AbstractApi implements SandboxInterface /** * Get message attachments by inboxId and messageId. * - * @param int $accountId - * @param int $inboxId - * @param int $messageId + * @param int $accountId + * @param int $inboxId + * @param int $messageId + * @param string|null $attachmentType * * @return ResponseInterface */ - public function getMessageAttachments(int $accountId, int $inboxId, int $messageId): ResponseInterface - { - return $this->handleResponse($this->httpGet(sprintf( - '%s/api/accounts/%s/inboxes/%s/messages/%s/attachments', - $this->getHost(), - $accountId, - $inboxId, - $messageId - ))); + public function getMessageAttachments( + int $accountId, + int $inboxId, + int $messageId, + string $attachmentType = null + ): ResponseInterface { + $parameters = []; + if (!empty($attachmentType)) { + $parameters = [ + 'attachment_type' => $attachmentType + ]; + } + + return $this->handleResponse($this->httpGet( + sprintf( + '%s/api/accounts/%s/inboxes/%s/messages/%s/attachments', + $this->getHost(), + $accountId, + $inboxId, + $messageId + ), + $parameters + )); } /** diff --git a/tests/Api/Sandbox/AttachmentTest.php b/tests/Api/Sandbox/AttachmentTest.php new file mode 100644 index 0000000..f460f9a --- /dev/null +++ b/tests/Api/Sandbox/AttachmentTest.php @@ -0,0 +1,120 @@ +attachment = $this->getMockBuilder(Attachment::class) + ->onlyMethods(['httpGet']) + ->setConstructorArgs([$this->getConfigMock()]) + ->getMock() + ; + } + + protected function tearDown(): void + { + $this->attachment = null; + + parent::tearDown(); + } + + public function testGetMessageAttachments(): void + { + $attachmentType = 'inline'; + $this->attachment->expects($this->once()) + ->method('httpGet') + ->with( + sprintf( + '%s/api/accounts/%s/inboxes/%s/messages/%s/attachments', + AbstractApi::DEFAULT_HOST, + self::FAKE_ACCOUNT_ID, + self::FAKE_INBOX_ID, + self::FAKE_MESSAGE_ID, + ), + [ + 'attachment_type' => $attachmentType + ] + ) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedData()))); + + $response = $this->attachment->getMessageAttachments( + self::FAKE_ACCOUNT_ID, + self::FAKE_INBOX_ID, + self::FAKE_MESSAGE_ID, + $attachmentType + ); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertCount(1, $responseData); + $this->assertArrayHasKey('filename', array_shift($responseData)); + } + + public function testGetMessageAttachment(): void + { + $this->attachment->expects($this->once()) + ->method('httpGet') + ->with(sprintf( + '%s/api/accounts/%s/inboxes/%s/messages/%s/attachments/%s', + AbstractApi::DEFAULT_HOST, + self::FAKE_ACCOUNT_ID, + self::FAKE_INBOX_ID, + self::FAKE_MESSAGE_ID, + self::FAKE_MESSAGE_ATTACHMENT_ID + )) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedData()))); + + $response = $this->attachment->getMessageAttachment( + self::FAKE_ACCOUNT_ID, + self::FAKE_INBOX_ID, + self::FAKE_MESSAGE_ID, + self::FAKE_MESSAGE_ATTACHMENT_ID + ); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertCount(1, $responseData); + $this->assertArrayHasKey('filename', array_shift($responseData)); + } + + private function getExpectedData(): array + { + return [ + [ + "id" => self::FAKE_MESSAGE_ATTACHMENT_ID, + "message_id" => self::FAKE_MESSAGE_ID, + "filename" => "test.csv", + "attachment_type" => "inline", + "content_type" => "plain/text", + "content_id" => null, + "transfer_encoding" => null, + "attachment_size" => 0, + "created_at" => "2022-06-02T19:25:54.827Z", + "updated_at" => "2022-06-02T19:25:54.827Z", + "attachment_human_size" => "0 Bytes", + "download_path" => "/api/accounts/3831/inboxes/4394/messages/457/attachments/67/download" + ] + ]; + } +} diff --git a/tests/MailtrapTestCase.php b/tests/MailtrapTestCase.php index 6167306..a95c705 100644 --- a/tests/MailtrapTestCase.php +++ b/tests/MailtrapTestCase.php @@ -20,6 +20,8 @@ abstract class MailtrapTestCase extends TestCase public const FAKE_ACCOUNT_ACCESS_ID = 1000001; public const FAKE_PROJECT_ID = 2436; public const FAKE_INBOX_ID = 4015; + public const FAKE_MESSAGE_ID = 457; + public const FAKE_MESSAGE_ATTACHMENT_ID = 67; protected function getHttpClientMock(): ClientInterface {