create email and emailer with tests
This commit is contained in:
parent
86787b7d3f
commit
962614ea02
4 changed files with 168 additions and 0 deletions
73
src/Email/Email.php
Normal file
73
src/Email/Email.php
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace FreightQuote\Email;
|
||||
|
||||
class Email
|
||||
{
|
||||
private array $recipients = [];
|
||||
private array $cc = [];
|
||||
private array $bcc = [];
|
||||
private string $subject = '';
|
||||
private string $body = '';
|
||||
private array $fileAttachments = [];
|
||||
|
||||
public function addRecipient(string $email): void
|
||||
{
|
||||
$this->recipients[] = $email;
|
||||
}
|
||||
|
||||
public function getRecipients(): array
|
||||
{
|
||||
return $this->recipients;
|
||||
}
|
||||
|
||||
public function addCC(string $email): void
|
||||
{
|
||||
$this->cc[] = $email;
|
||||
}
|
||||
|
||||
public function getCC(): array
|
||||
{
|
||||
return $this->cc;
|
||||
}
|
||||
|
||||
public function addBCC(string $email): void
|
||||
{
|
||||
$this->bcc[] = $email;
|
||||
}
|
||||
|
||||
public function getBCC(): array
|
||||
{
|
||||
return $this->bcc;
|
||||
}
|
||||
|
||||
public function setSubject(string $subject): void
|
||||
{
|
||||
$this->subject = $subject;
|
||||
}
|
||||
|
||||
public function getSubject(): string
|
||||
{
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
public function setBody(string $body): void
|
||||
{
|
||||
$this->body = $body;
|
||||
}
|
||||
|
||||
public function getBody(): string
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function addAttachment(string $path): void
|
||||
{
|
||||
$this->fileAttachments[] = $path;
|
||||
}
|
||||
|
||||
public function getAttachments(): array
|
||||
{
|
||||
return $this->fileAttachments;
|
||||
}
|
||||
}
|
||||
10
src/Email/Emailer.php
Normal file
10
src/Email/Emailer.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace FreightQuote\Email;
|
||||
|
||||
|
||||
|
||||
interface Emailer
|
||||
{
|
||||
public function send(Email $email): bool;
|
||||
}
|
||||
27
tests/Fakes/Email/FakeEmailer.php
Normal file
27
tests/Fakes/Email/FakeEmailer.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes\Email;
|
||||
|
||||
use FreightQuote\Email\Email;
|
||||
use FreightQuote\Email\Emailer;
|
||||
|
||||
class FakeEmailer implements Emailer
|
||||
{
|
||||
private int $sentEmailCount = 0;
|
||||
|
||||
public function getSentEmailCount(): int
|
||||
{
|
||||
return $this->sentEmailCount;
|
||||
}
|
||||
|
||||
public function send(Email $email): bool
|
||||
{
|
||||
if (count($email->getRecipients()) > 0) {
|
||||
$this->sentEmailCount++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
58
tests/Unit/Email/EmailTest.php
Normal file
58
tests/Unit/Email/EmailTest.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Email;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use FreightQuote\Email\Email;
|
||||
|
||||
class EmailTest extends TestCase
|
||||
{
|
||||
private Email $email;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->email = new Email();
|
||||
}
|
||||
|
||||
public function test_email_recipients(): void
|
||||
{
|
||||
$emailAddress = 'test@email.com';
|
||||
$this->email->addRecipient($emailAddress);
|
||||
$this->assertEquals([$emailAddress], $this->email->getRecipients());
|
||||
}
|
||||
|
||||
public function test_email_cc(): void
|
||||
{
|
||||
$emailAddress = 'test@email.com';
|
||||
$this->email->addCC($emailAddress);
|
||||
$this->assertEquals([$emailAddress], $this->email->getCC());
|
||||
}
|
||||
|
||||
public function test_email_bcc(): void
|
||||
{
|
||||
$emailAddress = 'test@email.com';
|
||||
$this->email->addBCC($emailAddress);
|
||||
$this->assertEquals([$emailAddress], $this->email->getBCC());
|
||||
}
|
||||
|
||||
public function test_email_subject(): void
|
||||
{
|
||||
$subject = 'test subject';
|
||||
$this->email->setSubject($subject);
|
||||
$this->assertEquals($subject, $this->email->getSubject());
|
||||
}
|
||||
|
||||
public function test_email_body(): void
|
||||
{
|
||||
$body = 'test body';
|
||||
$this->email->setBody($body);
|
||||
$this->assertEquals($body, $this->email->getBody());
|
||||
}
|
||||
|
||||
public function test_email_attachments(): void
|
||||
{
|
||||
$path = '/path/to/file';
|
||||
$this->email->addAttachment($path);
|
||||
$this->assertEquals([$path], $this->email->getAttachments());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue