Skip to content

Commit

Permalink
Merge pull request #11 from railsware/feature/attachments
Browse files Browse the repository at this point in the history
Support attachment endpoints
  • Loading branch information
gaalferov authored May 8, 2023
2 parents 61b6f34 + 0e14ea3 commit 2246857
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
50 changes: 50 additions & 0 deletions examples/sandbox/attachments.php
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";
}
69 changes: 69 additions & 0 deletions src/Api/Sandbox/Attachment.php
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
)));
}
}
8 changes: 5 additions & 3 deletions src/MailtrapSandboxClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
];
}
120 changes: 120 additions & 0 deletions tests/Api/Sandbox/AttachmentTest.php
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"
]
];
}
}
2 changes: 2 additions & 0 deletions tests/MailtrapTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 2246857

Please sign in to comment.