|
| 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 | +} |
0 commit comments