Skip to content

Commit

Permalink
Email attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
lennyrouanet committed Dec 4, 2023
1 parent 37ee70d commit dda9946
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 7 deletions.
10 changes: 7 additions & 3 deletions component/Web/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Phant\DataStructure\Web;

use Phant\DataStructure\Web\EmailAddressAndName;
use Phant\DataStructure\Web\EmailAttachmentList;

class Email
{
Expand All @@ -14,7 +15,8 @@ final public function __construct(
public string $messageHtml,
public EmailAddressAndName $from,
public EmailAddressAndName $to,
public ?EmailAddressAndName $replyTo
public ?EmailAddressAndName $replyTo = null,
public ?EmailAttachmentList $attachmentList = null
) {
}

Expand All @@ -27,7 +29,8 @@ public static function make(
string $toEmailAddress,
string $toName,
?string $replyToEmailAddress = null,
?string $replyToName = null
?string $replyToName = null,
?EmailAttachmentList $attachmentList = null
): self {
return new static(
$subject,
Expand All @@ -44,7 +47,8 @@ public static function make(
$replyToEmailAddress ? EmailAddressAndName::make(
$replyToEmailAddress,
$replyToName
) : null
) : null,
$attachmentList
);
}
}
15 changes: 15 additions & 0 deletions component/Web/EmailAttachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Phant\DataStructure\Web;

class EmailAttachment
{
final public function __construct(
public string $name,
public string $content,
public string $type
) {
}
}
16 changes: 16 additions & 0 deletions component/Web/EmailAttachmentList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Phant\DataStructure\Web;

use Phant\DataStructure\Web\EmailAttachment;

class EmailAttachmentList extends \Phant\DataStructure\Abstract\Collection
{
final public function add(
EmailAttachment $attachment
): self {
return $this->addItem($attachment);
}
}
36 changes: 36 additions & 0 deletions test/Web/EmailAttachmentListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Test\Web;

use Phant\DataStructure\Web\{
EmailAttachment,
EmailAttachmentList,
};
use Test\Web\EmailAttachmentTest;

final class EmailAttachmentListTest extends \PHPUnit\Framework\TestCase
{
protected EmailAttachmentList $fixture;

public function setUp(): void
{
$this->fixture = new EmailAttachmentList();
}

public function testAdd(): void
{
$this->assertEquals(0, $this->fixture->getNbItems());

$this->fixture->add(
new EmailAttachment(
'file.pdf',
EmailAttachmentTest::Content,
'application/pdf'
)
);

$this->assertEquals(1, $this->fixture->getNbItems());
}
}
30 changes: 30 additions & 0 deletions test/Web/EmailAttachmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Test\Web;

use Phant\DataStructure\Web\EmailAttachment;

final class EmailAttachmentTest extends \PHPUnit\Framework\TestCase
{
public const Content = 'JVBERi0xLjcNCiXi48/TDQozIDAgb2JqDQo8PA0KL0xpbmVhcml6ZWQgMQ0KL0wgMTYzMA0KL0ggWyA2MjAgMTIyIF0NCi9PIDUNCi9FIDEyMTcNCi9OIDENCi9UIDE0NDQNCj4+DQplbmRvYmoNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICANCnhyZWYNCjMgNQ0KMDAwMDAwMDAxNyAwMDAwMCBuDQowMDAwMDAwNTY1IDAwMDAwIG4NCjAwMDAwMDA3NDIgMDAwMDAgbg0KMDAwMDAwMDg5NCAwMDAwMCBuDQowMDAwMDAwNjIwIDAwMDAwIG4NCnRyYWlsZXINCjw8DQovU2l6ZSA4DQovUHJldiAxNDM0DQovSW5mbyAyIDAgUg0KL1Jvb3QgNCAwIFINCi9JRCBbPGQyMjc5OTNkMzg3N2EwZDA5MTgzYWRhN2FjZWFkZDEwPjxkMjI3OTkzZDM4NzdhMGQwOTE4M2FkYTdhY2VhZGQxMD5dDQo+Pg0Kc3RhcnR4cmVmDQowDQolJUVPRg0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg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8PA0KL01vZERhdGUgKEQ6MjAyMzEyMDQwODU2MDIrMDEnMDAnKQ0KL1Byb2R1Y2VyDQooMy1IZWlnaHRzXChUTVwpIFBERiBPcHRpbWl6YXRpb24gU2hlbGwgNS45LjEuNSBcKGh0dHA6Ly93d3cucGRmLXRvb2xzLmNvbVwpKQ0KPj4NCmVuZG9iag0KeHJlZg0KMCAzDQowMDAwMDAwMDAwIDY1NTM1IGYNCjAwMDAwMDEyMTcgMDAwMDAgbg0KMDAwMDAwMTI4MyAwMDAwMCBuDQp0cmFpbGVyDQo8PA0KL1NpemUgMw0KL0lEIFs8ZDIyNzk5M2QzODc3YTBkMDkxODNhZGE3YWNlYWRkMTA+PGQyMjc5OTNkMzg3N2EwZDA5MTgzYWRhN2FjZWFkZDEwPl0NCj4+DQpzdGFydHhyZWYNCjE3OA0KJSVFT0YNCg==';

public function testInterface(): void
{
$email = new EmailAttachment(
'file.pdf',
self::Content,
'application/pdf'
);

$this->assertIsString($email->name);
$this->assertEquals('file.pdf', $email->name);

$this->assertIsString($email->content);
$this->assertEquals(self::Content, $email->content);

$this->assertIsString($email->type);
$this->assertEquals('application/pdf', $email->type);
}
}
37 changes: 33 additions & 4 deletions test/Web/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,27 @@
Email,
EmailAddress,
EmailAddressAndName,
EmailAttachment,
EmailAttachmentList,
};

use Phant\Error\NotCompliant;
use Test\Web\EmailAttachmentTest;

final class EmailTest extends \PHPUnit\Framework\TestCase
{
protected EmailAttachmentList $attachmentList;

public function setUp(): void
{
$this->attachmentList = (new EmailAttachmentList())
->add(
new EmailAttachment(
'file.pdf',
EmailAttachmentTest::Content,
'application/pdf'
)
);
}

public function testInterface(): void
{
$email = new Email(
Expand All @@ -31,7 +46,8 @@ public function testInterface(): void
new EmailAddressAndName(
new EmailAddress('[email protected]'),
'No reply'
)
),
$this->attachmentList
);

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

$this->assertIsObject($email->from);
$this->assertInstanceOf(EmailAddressAndName::class, $email->from);
$this->assertEquals('[email protected]', (string)$email->from->emailAddress);
$this->assertEquals('Acme', $email->from->name);

$this->assertIsObject($email->to);
$this->assertInstanceOf(EmailAddressAndName::class, $email->to);
$this->assertEquals('[email protected]', (string)$email->to->emailAddress);
$this->assertEquals('John DOE', $email->to->name);

$this->assertIsObject($email->replyTo);
$this->assertInstanceOf(EmailAddressAndName::class, $email->replyTo);
$this->assertEquals('[email protected]', (string)$email->replyTo->emailAddress);
$this->assertEquals('No reply', $email->replyTo->name);

$this->assertIsObject($email->attachmentList);
$this->assertInstanceOf(EmailAttachmentList::class, $email->attachmentList);
}

public function testMake(): void
Expand All @@ -67,7 +89,8 @@ public function testMake(): void
'[email protected]',
'John DOE',
'[email protected]',
'No reply'
'No reply',
$this->attachmentList
);

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

$this->assertIsObject($email->from);
$this->assertInstanceOf(EmailAddressAndName::class, $email->from);
$this->assertEquals('[email protected]', (string)$email->from->emailAddress);
$this->assertEquals('Acme', $email->from->name);

$this->assertIsObject($email->to);
$this->assertInstanceOf(EmailAddressAndName::class, $email->to);
$this->assertEquals('[email protected]', (string)$email->to->emailAddress);
$this->assertEquals('John DOE', $email->to->name);

$this->assertIsObject($email->replyTo);
$this->assertInstanceOf(EmailAddressAndName::class, $email->replyTo);
$this->assertEquals('[email protected]', (string)$email->replyTo->emailAddress);
$this->assertEquals('No reply', $email->replyTo->name);

$this->assertIsObject($email->attachmentList);
$this->assertInstanceOf(EmailAttachmentList::class, $email->attachmentList);
}
}

0 comments on commit dda9946

Please sign in to comment.