Skip to content

Commit f671efc

Browse files
committed
ISSUE-345: tests
1 parent d199301 commit f671efc

File tree

3 files changed

+263
-2
lines changed

3 files changed

+263
-2
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@
1616
.vagrant
1717
.phpdoc/
1818
.phpunit.result.cache
19-
/.env
20-
/.env.local
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Tests\Unit\Domain\Messaging\Command;
6+
7+
use PhpList\Core\Domain\Messaging\Command\SendTestEmailCommand;
8+
use PhpList\Core\Domain\Messaging\Service\EmailService;
9+
use PHPUnit\Framework\MockObject\MockObject;
10+
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\Console\Application;
12+
use Symfony\Component\Console\Tester\CommandTester;
13+
use Symfony\Component\Mime\Email;
14+
15+
class SendTestEmailCommandTest extends TestCase
16+
{
17+
private EmailService&MockObject $emailService;
18+
private CommandTester $commandTester;
19+
20+
protected function setUp(): void
21+
{
22+
$this->emailService = $this->createMock(EmailService::class);
23+
$command = new SendTestEmailCommand($this->emailService);
24+
25+
$application = new Application();
26+
$application->add($command);
27+
28+
$this->commandTester = new CommandTester($command);
29+
}
30+
31+
public function testExecuteWithValidEmail(): void
32+
{
33+
$this->emailService->expects($this->once())
34+
->method('sendEmail')
35+
->with($this->callback(function (Email $email) {
36+
$this->assertEquals('Test Email from phpList', $email->getSubject());
37+
$this->assertStringContainsString('This is a test email', $email->getTextBody());
38+
$this->assertStringContainsString('<h1>Test</h1>', $email->getHtmlBody());
39+
40+
$toAddresses = $email->getTo();
41+
$this->assertCount(1, $toAddresses);
42+
$this->assertEquals('[email protected]', $toAddresses[0]->getAddress());
43+
44+
$fromAddresses = $email->getFrom();
45+
$this->assertCount(1, $fromAddresses);
46+
$this->assertEquals('[email protected]', $fromAddresses[0]->getAddress());
47+
$this->assertEquals('Admin Team', $fromAddresses[0]->getName());
48+
49+
return true;
50+
}));
51+
52+
$this->commandTester->execute([
53+
'recipient' => '[email protected]',
54+
]);
55+
56+
$output = $this->commandTester->getDisplay();
57+
$this->assertStringContainsString('Test email sent successfully', $output);
58+
59+
$this->assertEquals(0, $this->commandTester->getStatusCode());
60+
}
61+
62+
public function testExecuteWithoutRecipient(): void
63+
{
64+
$this->emailService->expects($this->never())
65+
->method('sendEmail');
66+
67+
$this->commandTester->execute([]);
68+
69+
$output = $this->commandTester->getDisplay();
70+
$this->assertStringContainsString('Recipient email address not provided', $output);
71+
72+
$this->assertEquals(1, $this->commandTester->getStatusCode());
73+
}
74+
75+
public function testExecuteWithInvalidEmail(): void
76+
{
77+
$this->emailService->expects($this->never())
78+
->method('sendEmail');
79+
80+
$this->commandTester->execute([
81+
'recipient' => 'invalid-email',
82+
]);
83+
84+
$output = $this->commandTester->getDisplay();
85+
$this->assertStringContainsString('Invalid email address', $output);
86+
87+
$this->assertEquals(1, $this->commandTester->getStatusCode());
88+
}
89+
90+
public function testExecuteWithEmailServiceException(): void
91+
{
92+
$this->emailService->expects($this->once())
93+
->method('sendEmail')
94+
->willThrowException(new \Exception('Test exception'));
95+
96+
$this->commandTester->execute([
97+
'recipient' => '[email protected]',
98+
]);
99+
100+
$output = $this->commandTester->getDisplay();
101+
$this->assertStringContainsString('Failed to send test email', $output);
102+
$this->assertStringContainsString('Test exception', $output);
103+
104+
$this->assertEquals(1, $this->commandTester->getStatusCode());
105+
}
106+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Tests\Unit\Domain\Messaging\Service;
6+
7+
use PhpList\Core\Domain\Messaging\Service\EmailService;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\Mailer\MailerInterface;
11+
use Symfony\Component\Mime\Email;
12+
13+
class EmailServiceTest extends TestCase
14+
{
15+
private EmailService $emailService;
16+
private MailerInterface&MockObject $mailer;
17+
private string $defaultFromEmail = '[email protected]';
18+
19+
protected function setUp(): void
20+
{
21+
$this->mailer = $this->createMock(MailerInterface::class);
22+
$this->emailService = new EmailService($this->mailer, $this->defaultFromEmail);
23+
}
24+
25+
public function testSendEmailWithDefaultFrom(): void
26+
{
27+
$email = (new Email())
28+
29+
->subject('Test Subject')
30+
->text('Test Content');
31+
32+
$this->mailer->expects($this->once())
33+
->method('send')
34+
->with($this->callback(function (Email $sentEmail) {
35+
$fromAddresses = $sentEmail->getFrom();
36+
$this->assertCount(1, $fromAddresses);
37+
$this->assertEquals($this->defaultFromEmail, $fromAddresses[0]->getAddress());
38+
return true;
39+
}));
40+
41+
$this->emailService->sendEmail($email);
42+
}
43+
44+
public function testSendEmailWithCustomFrom(): void
45+
{
46+
$customFrom = '[email protected]';
47+
$email = (new Email())
48+
->from($customFrom)
49+
50+
->subject('Test Subject')
51+
->text('Test Content');
52+
53+
$this->mailer->expects($this->once())
54+
->method('send')
55+
->with($this->callback(function (Email $sentEmail) use ($customFrom) {
56+
$fromAddresses = $sentEmail->getFrom();
57+
$this->assertCount(1, $fromAddresses);
58+
$this->assertEquals($customFrom, $fromAddresses[0]->getAddress());
59+
return true;
60+
}));
61+
62+
$this->emailService->sendEmail($email);
63+
}
64+
65+
public function testSendEmailWithCcBccAndReplyTo(): void
66+
{
67+
$email = (new Email())
68+
69+
->subject('Test Subject')
70+
->text('Test Content');
71+
72+
$cc = ['[email protected]'];
73+
$bcc = ['[email protected]'];
74+
$replyTo = ['[email protected]'];
75+
76+
$this->mailer->expects($this->once())
77+
->method('send')
78+
->with($this->callback(function (Email $sentEmail) use ($cc, $bcc, $replyTo) {
79+
$ccAddresses = $sentEmail->getCc();
80+
$bccAddresses = $sentEmail->getBcc();
81+
$replyToAddresses = $sentEmail->getReplyTo();
82+
83+
$this->assertCount(1, $ccAddresses);
84+
$this->assertEquals($cc[0], $ccAddresses[0]->getAddress());
85+
86+
$this->assertCount(1, $bccAddresses);
87+
$this->assertEquals($bcc[0], $bccAddresses[0]->getAddress());
88+
89+
$this->assertCount(1, $replyToAddresses);
90+
$this->assertEquals($replyTo[0], $replyToAddresses[0]->getAddress());
91+
92+
return true;
93+
}));
94+
95+
$this->emailService->sendEmail($email, $cc, $bcc, $replyTo);
96+
}
97+
98+
public function testSendEmailWithAttachments(): void
99+
{
100+
$email = (new Email())
101+
102+
->subject('Test Subject')
103+
->text('Test Content');
104+
105+
$attachments = ['/path/to/attachment.pdf'];
106+
107+
$this->mailer->expects($this->once())
108+
->method('send');
109+
110+
$this->emailService->sendEmail($email, [], [], [], $attachments);
111+
}
112+
113+
public function testSendBulkEmail(): void
114+
{
115+
116+
$subject = 'Bulk Test Subject';
117+
$text = 'Bulk Test Content';
118+
$html = '<p>Bulk Test HTML Content</p>';
119+
$from = '[email protected]';
120+
$fromName = 'Sender Name';
121+
122+
$this->mailer->expects($this->exactly(count($recipients)))
123+
->method('send')
124+
->with($this->callback(function (Email $sentEmail) use ($subject, $text, $html, $from, $fromName) {
125+
$this->assertEquals($subject, $sentEmail->getSubject());
126+
$this->assertEquals($text, $sentEmail->getTextBody());
127+
$this->assertEquals($html, $sentEmail->getHtmlBody());
128+
129+
$fromAddresses = $sentEmail->getFrom();
130+
$this->assertCount(1, $fromAddresses);
131+
$this->assertEquals($from, $fromAddresses[0]->getAddress());
132+
$this->assertEquals($fromName, $fromAddresses[0]->getName());
133+
134+
return true;
135+
}));
136+
137+
$this->emailService->sendBulkEmail($recipients, $subject, $text, $html, $from, $fromName);
138+
}
139+
140+
public function testSendBulkEmailWithDefaultFrom(): void
141+
{
142+
$recipients = ['[email protected]', '[email protected]'];
143+
$subject = 'Bulk Test Subject';
144+
$text = 'Bulk Test Content';
145+
146+
$this->mailer->expects($this->exactly(count($recipients)))
147+
->method('send')
148+
->with($this->callback(function (Email $sentEmail) {
149+
$fromAddresses = $sentEmail->getFrom();
150+
$this->assertCount(1, $fromAddresses);
151+
$this->assertEquals($this->defaultFromEmail, $fromAddresses[0]->getAddress());
152+
return true;
153+
}));
154+
155+
$this->emailService->sendBulkEmail($recipients, $subject, $text);
156+
}
157+
}

0 commit comments

Comments
 (0)