diff --git a/component/Web/Email.php b/component/Web/Email.php index 2841b2f..6e03115 100644 --- a/component/Web/Email.php +++ b/component/Web/Email.php @@ -5,6 +5,7 @@ namespace Phant\DataStructure\Web; use Phant\DataStructure\Web\EmailAddressAndName; +use Phant\DataStructure\Web\EmailAttachmentList; class Email { @@ -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 ) { } @@ -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, @@ -44,7 +47,8 @@ public static function make( $replyToEmailAddress ? EmailAddressAndName::make( $replyToEmailAddress, $replyToName - ) : null + ) : null, + $attachmentList ); } } diff --git a/component/Web/EmailAttachment.php b/component/Web/EmailAttachment.php new file mode 100644 index 0000000..01342f8 --- /dev/null +++ b/component/Web/EmailAttachment.php @@ -0,0 +1,15 @@ +addItem($attachment); + } +} diff --git a/test/Web/EmailAttachmentListTest.php b/test/Web/EmailAttachmentListTest.php new file mode 100644 index 0000000..d3d139f --- /dev/null +++ b/test/Web/EmailAttachmentListTest.php @@ -0,0 +1,36 @@ +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()); + } +} diff --git a/test/Web/EmailAttachmentTest.php b/test/Web/EmailAttachmentTest.php new file mode 100644 index 0000000..fe90fe7 --- /dev/null +++ b/test/Web/EmailAttachmentTest.php @@ -0,0 +1,30 @@ +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); + } +} diff --git a/test/Web/EmailTest.php b/test/Web/EmailTest.php index 7154e42..0f8624a 100644 --- a/test/Web/EmailTest.php +++ b/test/Web/EmailTest.php @@ -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( @@ -31,7 +46,8 @@ public function testInterface(): void new EmailAddressAndName( new EmailAddress('no-reply@acme.ext'), 'No reply' - ) + ), + $this->attachmentList ); $this->assertIsString($email->subject); @@ -44,16 +60,22 @@ public function testInterface(): void $this->assertEquals('
Message
', $email->messageHtml); $this->assertIsObject($email->from); + $this->assertInstanceOf(EmailAddressAndName::class, $email->from); $this->assertEquals('contact@acme.ext', (string)$email->from->emailAddress); $this->assertEquals('Acme', $email->from->name); $this->assertIsObject($email->to); + $this->assertInstanceOf(EmailAddressAndName::class, $email->to); $this->assertEquals('john.doe@domain.ext', (string)$email->to->emailAddress); $this->assertEquals('John DOE', $email->to->name); $this->assertIsObject($email->replyTo); + $this->assertInstanceOf(EmailAddressAndName::class, $email->replyTo); $this->assertEquals('no-reply@acme.ext', (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 @@ -67,7 +89,8 @@ public function testMake(): void 'john.doe@domain.ext', 'John DOE', 'no-reply@acme.ext', - 'No reply' + 'No reply', + $this->attachmentList ); $this->assertInstanceOf(Email::class, $email); @@ -82,15 +105,21 @@ public function testMake(): void $this->assertEquals('Message
', $email->messageHtml); $this->assertIsObject($email->from); + $this->assertInstanceOf(EmailAddressAndName::class, $email->from); $this->assertEquals('contact@acme.ext', (string)$email->from->emailAddress); $this->assertEquals('Acme', $email->from->name); $this->assertIsObject($email->to); + $this->assertInstanceOf(EmailAddressAndName::class, $email->to); $this->assertEquals('john.doe@domain.ext', (string)$email->to->emailAddress); $this->assertEquals('John DOE', $email->to->name); $this->assertIsObject($email->replyTo); + $this->assertInstanceOf(EmailAddressAndName::class, $email->replyTo); $this->assertEquals('no-reply@acme.ext', (string)$email->replyTo->emailAddress); $this->assertEquals('No reply', $email->replyTo->name); + + $this->assertIsObject($email->attachmentList); + $this->assertInstanceOf(EmailAttachmentList::class, $email->attachmentList); } }