-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from railsware/feature/attachments
Support attachment endpoints
- Loading branch information
Showing
6 changed files
with
250 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
use Mailtrap\Config; | ||
use Mailtrap\Helper\ResponseHelper; | ||
use Mailtrap\MailtrapClient; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
// your API token from here https://mailtrap.io/api-tokens | ||
$apiKey = getenv('MAILTRAP_API_KEY'); | ||
$mailtrap = new MailtrapClient(new Config($apiKey)); | ||
|
||
/** | ||
* Get attachments | ||
* | ||
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/attachments | ||
*/ | ||
try { | ||
$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, $attachmentType); | ||
|
||
// 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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Mailtrap\Api\Sandbox; | ||
|
||
use Mailtrap\Api\AbstractApi; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Class Attachment | ||
*/ | ||
class Attachment extends AbstractApi implements SandboxInterface | ||
{ | ||
/** | ||
* Get message attachments by inboxId and 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, | ||
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 | ||
)); | ||
} | ||
|
||
/** | ||
* 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 | ||
))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace Mailtrap\Tests\Api\Sandbox; | ||
|
||
use Mailtrap\Api\AbstractApi; | ||
use Mailtrap\Api\Sandbox\Attachment; | ||
use Mailtrap\Exception\HttpClientException; | ||
use Mailtrap\Helper\ResponseHelper; | ||
use Mailtrap\Tests\MailtrapTestCase; | ||
use Nyholm\Psr7\Response; | ||
|
||
/** | ||
* @covers Attachment | ||
* | ||
* Class ProjectTest | ||
*/ | ||
class AttachmentTest extends MailtrapTestCase | ||
{ | ||
/** | ||
* @var Attachment | ||
*/ | ||
private $attachment; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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" | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters