Skip to content

Commit dda9946

Browse files
committed
Email attachment
1 parent 37ee70d commit dda9946

File tree

6 files changed

+137
-7
lines changed

6 files changed

+137
-7
lines changed

component/Web/Email.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Phant\DataStructure\Web;
66

77
use Phant\DataStructure\Web\EmailAddressAndName;
8+
use Phant\DataStructure\Web\EmailAttachmentList;
89

910
class Email
1011
{
@@ -14,7 +15,8 @@ final public function __construct(
1415
public string $messageHtml,
1516
public EmailAddressAndName $from,
1617
public EmailAddressAndName $to,
17-
public ?EmailAddressAndName $replyTo
18+
public ?EmailAddressAndName $replyTo = null,
19+
public ?EmailAttachmentList $attachmentList = null
1820
) {
1921
}
2022

@@ -27,7 +29,8 @@ public static function make(
2729
string $toEmailAddress,
2830
string $toName,
2931
?string $replyToEmailAddress = null,
30-
?string $replyToName = null
32+
?string $replyToName = null,
33+
?EmailAttachmentList $attachmentList = null
3134
): self {
3235
return new static(
3336
$subject,
@@ -44,7 +47,8 @@ public static function make(
4447
$replyToEmailAddress ? EmailAddressAndName::make(
4548
$replyToEmailAddress,
4649
$replyToName
47-
) : null
50+
) : null,
51+
$attachmentList
4852
);
4953
}
5054
}

component/Web/EmailAttachment.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phant\DataStructure\Web;
6+
7+
class EmailAttachment
8+
{
9+
final public function __construct(
10+
public string $name,
11+
public string $content,
12+
public string $type
13+
) {
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phant\DataStructure\Web;
6+
7+
use Phant\DataStructure\Web\EmailAttachment;
8+
9+
class EmailAttachmentList extends \Phant\DataStructure\Abstract\Collection
10+
{
11+
final public function add(
12+
EmailAttachment $attachment
13+
): self {
14+
return $this->addItem($attachment);
15+
}
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Web;
6+
7+
use Phant\DataStructure\Web\{
8+
EmailAttachment,
9+
EmailAttachmentList,
10+
};
11+
use Test\Web\EmailAttachmentTest;
12+
13+
final class EmailAttachmentListTest extends \PHPUnit\Framework\TestCase
14+
{
15+
protected EmailAttachmentList $fixture;
16+
17+
public function setUp(): void
18+
{
19+
$this->fixture = new EmailAttachmentList();
20+
}
21+
22+
public function testAdd(): void
23+
{
24+
$this->assertEquals(0, $this->fixture->getNbItems());
25+
26+
$this->fixture->add(
27+
new EmailAttachment(
28+
'file.pdf',
29+
EmailAttachmentTest::Content,
30+
'application/pdf'
31+
)
32+
);
33+
34+
$this->assertEquals(1, $this->fixture->getNbItems());
35+
}
36+
}

test/Web/EmailAttachmentTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Web;
6+
7+
use Phant\DataStructure\Web\EmailAttachment;
8+
9+
final class EmailAttachmentTest extends \PHPUnit\Framework\TestCase
10+
{
11+
public const Content = 'JVBERi0xLjcNCiXi48/TDQozIDAgb2JqDQo8PA0KL0xpbmVhcml6ZWQgMQ0KL0wgMTYzMA0KL0ggWyA2MjAgMTIyIF0NCi9PIDUNCi9FIDEyMTcNCi9OIDENCi9UIDE0NDQNCj4+DQplbmRvYmoNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICANCnhyZWYNCjMgNQ0KMDAwMDAwMDAxNyAwMDAwMCBuDQowMDAwMDAwNTY1IDAwMDAwIG4NCjAwMDAwMDA3NDIgMDAwMDAgbg0KMDAwMDAwMDg5NCAwMDAwMCBuDQowMDAwMDAwNjIwIDAwMDAw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vl90Cd/mdSZpvKAe6AYS3RwxzDtvU70cVpOPs8KyF+sZ98P+KyecrVuMQrI4tnJuz7l1YauYey7vGePA7D3f9e1GqqYUQcJxSrBrc4OGuYNqEf1uvZxNWfTTtrmt+VdwMb9A1KWezgNCmVuZHN0cmVhbQ0KZW5kb2JqDQoxIDAgb2JqDQo8PA0KL1R5cGUgL1BhZ2VzDQovS2lkcyBbIDUgMCBSIF0NCi9Db3VudCAxDQo+Pg0KZW5kb2JqDQoyIDAgb2JqDQo8PA0KL01vZERhdGUgKEQ6MjAyMzEyMDQwODU2MDIrMDEnMDAnKQ0KL1Byb2R1Y2VyDQooMy1IZWlnaHRzXChUTVwpIFBERiBPcHRpbWl6YXRpb24gU2hlbGwgNS45LjEuNSBcKGh0dHA6Ly93d3cucGRmLXRvb2xzLmNvbVwpKQ0KPj4NCmVuZG9iag0KeHJlZg0KMCAzDQowMDAwMDAwMDAwIDY1NTM1IGYNCjAwMDAwMDEyMTcgMDAwMDAgbg0KMDAwMDAwMTI4MyAwMDAwMCBuDQp0cmFpbGVyDQo8PA0KL1NpemUgMw0KL0lEIFs8ZDIyNzk5M2QzODc3YTBkMDkxODNhZGE3YWNlYWRkMTA+PGQyMjc5OTNkMzg3N2EwZDA5MTgzYWRhN2FjZWFkZDEwPl0NCj4+DQpzdGFydHhyZWYNCjE3OA0KJSVFT0YNCg==';
12+
13+
public function testInterface(): void
14+
{
15+
$email = new EmailAttachment(
16+
'file.pdf',
17+
self::Content,
18+
'application/pdf'
19+
);
20+
21+
$this->assertIsString($email->name);
22+
$this->assertEquals('file.pdf', $email->name);
23+
24+
$this->assertIsString($email->content);
25+
$this->assertEquals(self::Content, $email->content);
26+
27+
$this->assertIsString($email->type);
28+
$this->assertEquals('application/pdf', $email->type);
29+
}
30+
}

test/Web/EmailTest.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,27 @@
88
Email,
99
EmailAddress,
1010
EmailAddressAndName,
11+
EmailAttachment,
12+
EmailAttachmentList,
1113
};
12-
13-
use Phant\Error\NotCompliant;
14+
use Test\Web\EmailAttachmentTest;
1415

1516
final class EmailTest extends \PHPUnit\Framework\TestCase
1617
{
18+
protected EmailAttachmentList $attachmentList;
19+
20+
public function setUp(): void
21+
{
22+
$this->attachmentList = (new EmailAttachmentList())
23+
->add(
24+
new EmailAttachment(
25+
'file.pdf',
26+
EmailAttachmentTest::Content,
27+
'application/pdf'
28+
)
29+
);
30+
}
31+
1732
public function testInterface(): void
1833
{
1934
$email = new Email(
@@ -31,7 +46,8 @@ public function testInterface(): void
3146
new EmailAddressAndName(
3247
new EmailAddress('no-reply@acme.ext'),
3348
'No reply'
34-
)
49+
),
50+
$this->attachmentList
3551
);
3652

3753
$this->assertIsString($email->subject);
@@ -44,16 +60,22 @@ public function testInterface(): void
4460
$this->assertEquals('<p>Message</p>', $email->messageHtml);
4561

4662
$this->assertIsObject($email->from);
63+
$this->assertInstanceOf(EmailAddressAndName::class, $email->from);
4764
$this->assertEquals('contact@acme.ext', (string)$email->from->emailAddress);
4865
$this->assertEquals('Acme', $email->from->name);
4966

5067
$this->assertIsObject($email->to);
68+
$this->assertInstanceOf(EmailAddressAndName::class, $email->to);
5169
$this->assertEquals('john.doe@domain.ext', (string)$email->to->emailAddress);
5270
$this->assertEquals('John DOE', $email->to->name);
5371

5472
$this->assertIsObject($email->replyTo);
73+
$this->assertInstanceOf(EmailAddressAndName::class, $email->replyTo);
5574
$this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->emailAddress);
5675
$this->assertEquals('No reply', $email->replyTo->name);
76+
77+
$this->assertIsObject($email->attachmentList);
78+
$this->assertInstanceOf(EmailAttachmentList::class, $email->attachmentList);
5779
}
5880

5981
public function testMake(): void
@@ -67,7 +89,8 @@ public function testMake(): void
6789
'john.doe@domain.ext',
6890
'John DOE',
6991
'no-reply@acme.ext',
70-
'No reply'
92+
'No reply',
93+
$this->attachmentList
7194
);
7295

7396
$this->assertInstanceOf(Email::class, $email);
@@ -82,15 +105,21 @@ public function testMake(): void
82105
$this->assertEquals('<p>Message</p>', $email->messageHtml);
83106

84107
$this->assertIsObject($email->from);
108+
$this->assertInstanceOf(EmailAddressAndName::class, $email->from);
85109
$this->assertEquals('contact@acme.ext', (string)$email->from->emailAddress);
86110
$this->assertEquals('Acme', $email->from->name);
87111

88112
$this->assertIsObject($email->to);
113+
$this->assertInstanceOf(EmailAddressAndName::class, $email->to);
89114
$this->assertEquals('john.doe@domain.ext', (string)$email->to->emailAddress);
90115
$this->assertEquals('John DOE', $email->to->name);
91116

92117
$this->assertIsObject($email->replyTo);
118+
$this->assertInstanceOf(EmailAddressAndName::class, $email->replyTo);
93119
$this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->emailAddress);
94120
$this->assertEquals('No reply', $email->replyTo->name);
121+
122+
$this->assertIsObject($email->attachmentList);
123+
$this->assertInstanceOf(EmailAttachmentList::class, $email->attachmentList);
95124
}
96125
}

0 commit comments

Comments
 (0)