Skip to content

Commit 2246857

Browse files
authored
Merge pull request #11 from railsware/feature/attachments
Support attachment endpoints
2 parents 61b6f34 + 0e14ea3 commit 2246857

File tree

6 files changed

+250
-3
lines changed

6 files changed

+250
-3
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [1.6.0] - 2023-05-05
2+
3+
- Support sandbox attachment endpoints. Examples [here](examples/sandbox/attachments.php)
4+
15
## [1.5.0] - 2023-05-04
26

37
- Support sandbox inbox endpoints. Examples [here](examples/sandbox/inboxes.php)

Diff for: examples/sandbox/attachments.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Mailtrap\Config;
4+
use Mailtrap\Helper\ResponseHelper;
5+
use Mailtrap\MailtrapClient;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
// your API token from here https://mailtrap.io/api-tokens
10+
$apiKey = getenv('MAILTRAP_API_KEY');
11+
$mailtrap = new MailtrapClient(new Config($apiKey));
12+
13+
/**
14+
* Get attachments
15+
*
16+
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/attachments
17+
*/
18+
try {
19+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
20+
$inboxId = getenv('MAILTRAP_INBOX_ID');
21+
$messageId = getenv('MAILTRAP_MESSAGE_ID');
22+
// optional (null|string)
23+
$attachmentType = 'inline';
24+
25+
$response = $mailtrap->sandbox()->attachments()->getMessageAttachments($accountId, $inboxId, $messageId, $attachmentType);
26+
27+
// print the response body (array)
28+
var_dump(ResponseHelper::toArray($response));
29+
} catch (Exception $e) {
30+
echo 'Caught exception: ', $e->getMessage(), "\n";
31+
}
32+
33+
/**
34+
* Get single attachment
35+
*
36+
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/attachments/{attachment_id}
37+
*/
38+
try {
39+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
40+
$inboxId = getenv('MAILTRAP_INBOX_ID');
41+
$messageId = getenv('MAILTRAP_MESSAGE_ID');
42+
$attachmentId = getenv('MAILTRAP_MESSAGE_ATTACHMENT_ID');
43+
44+
$response = $mailtrap->sandbox()->attachments()->getMessageAttachment($accountId, $inboxId, $messageId, $attachmentId);
45+
46+
// print the response body (array)
47+
var_dump(ResponseHelper::toArray($response));
48+
} catch (Exception $e) {
49+
echo 'Caught exception: ', $e->getMessage(), "\n";
50+
}

Diff for: src/Api/Sandbox/Attachment.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Mailtrap\Api\Sandbox;
4+
5+
use Mailtrap\Api\AbstractApi;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
/**
9+
* Class Attachment
10+
*/
11+
class Attachment extends AbstractApi implements SandboxInterface
12+
{
13+
/**
14+
* Get message attachments by inboxId and messageId.
15+
*
16+
* @param int $accountId
17+
* @param int $inboxId
18+
* @param int $messageId
19+
* @param string|null $attachmentType
20+
*
21+
* @return ResponseInterface
22+
*/
23+
public function getMessageAttachments(
24+
int $accountId,
25+
int $inboxId,
26+
int $messageId,
27+
string $attachmentType = null
28+
): ResponseInterface {
29+
$parameters = [];
30+
if (!empty($attachmentType)) {
31+
$parameters = [
32+
'attachment_type' => $attachmentType
33+
];
34+
}
35+
36+
return $this->handleResponse($this->httpGet(
37+
sprintf(
38+
'%s/api/accounts/%s/inboxes/%s/messages/%s/attachments',
39+
$this->getHost(),
40+
$accountId,
41+
$inboxId,
42+
$messageId
43+
),
44+
$parameters
45+
));
46+
}
47+
48+
/**
49+
* Get message single attachment by id.
50+
*
51+
* @param int $accountId
52+
* @param int $inboxId
53+
* @param int $messageId
54+
* @param int $attachmentId
55+
*
56+
* @return ResponseInterface
57+
*/
58+
public function getMessageAttachment(int $accountId, int $inboxId, int $messageId, int $attachmentId): ResponseInterface
59+
{
60+
return $this->handleResponse($this->httpGet(sprintf(
61+
'%s/api/accounts/%s/inboxes/%s/messages/%s/attachments/%s',
62+
$this->getHost(),
63+
$accountId,
64+
$inboxId,
65+
$messageId,
66+
$attachmentId
67+
)));
68+
}
69+
}

Diff for: src/MailtrapSandboxClient.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
use Mailtrap\Api;
88

99
/**
10-
* @method Api\Sandbox\Emails emails
11-
* @method Api\Sandbox\Project projects
12-
* @method Api\Sandbox\Inbox inboxes
10+
* @method Api\Sandbox\Emails emails
11+
* @method Api\Sandbox\Project projects
12+
* @method Api\Sandbox\Inbox inboxes
13+
* @method Api\Sandbox\Attachment attachments
1314
*
1415
* Class MailtrapSandboxClient
1516
*/
@@ -19,5 +20,6 @@ final class MailtrapSandboxClient extends AbstractMailtrapClient
1920
'emails' => Api\Sandbox\Emails::class,
2021
'projects' => Api\Sandbox\Project::class,
2122
'inboxes' => Api\Sandbox\Inbox::class,
23+
'attachments' => Api\Sandbox\Attachment::class
2224
];
2325
}

Diff for: tests/Api/Sandbox/AttachmentTest.php

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Mailtrap\Tests\Api\Sandbox;
4+
5+
use Mailtrap\Api\AbstractApi;
6+
use Mailtrap\Api\Sandbox\Attachment;
7+
use Mailtrap\Exception\HttpClientException;
8+
use Mailtrap\Helper\ResponseHelper;
9+
use Mailtrap\Tests\MailtrapTestCase;
10+
use Nyholm\Psr7\Response;
11+
12+
/**
13+
* @covers Attachment
14+
*
15+
* Class ProjectTest
16+
*/
17+
class AttachmentTest extends MailtrapTestCase
18+
{
19+
/**
20+
* @var Attachment
21+
*/
22+
private $attachment;
23+
24+
protected function setUp(): void
25+
{
26+
parent::setUp();
27+
28+
$this->attachment = $this->getMockBuilder(Attachment::class)
29+
->onlyMethods(['httpGet'])
30+
->setConstructorArgs([$this->getConfigMock()])
31+
->getMock()
32+
;
33+
}
34+
35+
protected function tearDown(): void
36+
{
37+
$this->attachment = null;
38+
39+
parent::tearDown();
40+
}
41+
42+
public function testGetMessageAttachments(): void
43+
{
44+
$attachmentType = 'inline';
45+
$this->attachment->expects($this->once())
46+
->method('httpGet')
47+
->with(
48+
sprintf(
49+
'%s/api/accounts/%s/inboxes/%s/messages/%s/attachments',
50+
AbstractApi::DEFAULT_HOST,
51+
self::FAKE_ACCOUNT_ID,
52+
self::FAKE_INBOX_ID,
53+
self::FAKE_MESSAGE_ID,
54+
),
55+
[
56+
'attachment_type' => $attachmentType
57+
]
58+
)
59+
->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedData())));
60+
61+
$response = $this->attachment->getMessageAttachments(
62+
self::FAKE_ACCOUNT_ID,
63+
self::FAKE_INBOX_ID,
64+
self::FAKE_MESSAGE_ID,
65+
$attachmentType
66+
);
67+
$responseData = ResponseHelper::toArray($response);
68+
69+
$this->assertInstanceOf(Response::class, $response);
70+
$this->assertCount(1, $responseData);
71+
$this->assertArrayHasKey('filename', array_shift($responseData));
72+
}
73+
74+
public function testGetMessageAttachment(): void
75+
{
76+
$this->attachment->expects($this->once())
77+
->method('httpGet')
78+
->with(sprintf(
79+
'%s/api/accounts/%s/inboxes/%s/messages/%s/attachments/%s',
80+
AbstractApi::DEFAULT_HOST,
81+
self::FAKE_ACCOUNT_ID,
82+
self::FAKE_INBOX_ID,
83+
self::FAKE_MESSAGE_ID,
84+
self::FAKE_MESSAGE_ATTACHMENT_ID
85+
))
86+
->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedData())));
87+
88+
$response = $this->attachment->getMessageAttachment(
89+
self::FAKE_ACCOUNT_ID,
90+
self::FAKE_INBOX_ID,
91+
self::FAKE_MESSAGE_ID,
92+
self::FAKE_MESSAGE_ATTACHMENT_ID
93+
);
94+
$responseData = ResponseHelper::toArray($response);
95+
96+
$this->assertInstanceOf(Response::class, $response);
97+
$this->assertCount(1, $responseData);
98+
$this->assertArrayHasKey('filename', array_shift($responseData));
99+
}
100+
101+
private function getExpectedData(): array
102+
{
103+
return [
104+
[
105+
"id" => self::FAKE_MESSAGE_ATTACHMENT_ID,
106+
"message_id" => self::FAKE_MESSAGE_ID,
107+
"filename" => "test.csv",
108+
"attachment_type" => "inline",
109+
"content_type" => "plain/text",
110+
"content_id" => null,
111+
"transfer_encoding" => null,
112+
"attachment_size" => 0,
113+
"created_at" => "2022-06-02T19:25:54.827Z",
114+
"updated_at" => "2022-06-02T19:25:54.827Z",
115+
"attachment_human_size" => "0 Bytes",
116+
"download_path" => "/api/accounts/3831/inboxes/4394/messages/457/attachments/67/download"
117+
]
118+
];
119+
}
120+
}

Diff for: tests/MailtrapTestCase.php

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ abstract class MailtrapTestCase extends TestCase
2020
public const FAKE_ACCOUNT_ACCESS_ID = 1000001;
2121
public const FAKE_PROJECT_ID = 2436;
2222
public const FAKE_INBOX_ID = 4015;
23+
public const FAKE_MESSAGE_ID = 457;
24+
public const FAKE_MESSAGE_ATTACHMENT_ID = 67;
2325

2426
protected function getHttpClientMock(): ClientInterface
2527
{

0 commit comments

Comments
 (0)