Skip to content

Commit

Permalink
added tests to incearse codecov coverage
Browse files Browse the repository at this point in the history
Signed-off-by: sergiu <[email protected]>
  • Loading branch information
SergiuBota1 committed Dec 2, 2024
1 parent 67214f5 commit 84edc30
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 4 deletions.
8 changes: 5 additions & 3 deletions test/CommonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,22 @@ private function generateConfig(): array
//options that will be used only if Symfony\Component\Mailer\Transport\Smtp\SmtpTransport
// adapter is used
'smtp_options' => [
'host' => 'qwd',
'host' => 'testHost',
'port' => 587,
'connection_class' => 'login',
'connection_config' => [

//the smtp authentication identity
'username' => 'qwd',
'username' => 'test',

//the smtp authentication credential
'password' => 'qwd',
'password' => 'testPassword',
'ssl' => 'tls',
],
],
],
'test' => 'string test',

// option to log the SENT emails
'log' => [
'sent' => $this->fileSystem->url() . '/log/mail/sent.log',
Expand Down
69 changes: 69 additions & 0 deletions test/EmailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace DotTest\Mail;

use DateTimeImmutable;
use Dot\Mail\Email;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Exception\LogicException;

class EmailTest extends TestCase
{
public function testGettersAndSetters(): void
{
$subject = new Email();

$subject->date(new DateTimeImmutable('now'));
$subject->returnPath('[email protected]');
$subject->setSender('[email protected]', 'testSender');
$subject->addFrom(['[email protected]', '[email protected]'], 'testAddFrom');
$subject->setFrom('[email protected]', 'testSetFrom');
$subject->addReplyTo('[email protected]', 'testAddReplyTo');
$subject->setReplyTo('[email protected]', 'testSetReplyTo');
$subject->addTo('[email protected]', 'testAddTo');
$subject->setTo('[email protected]', 'testAddTo');
$subject->addCc('[email protected]', 'testAddCc');
$subject->setCc(new Address('[email protected]'), 'testSetBcc');
$subject->addBcc('[email protected]', 'testAddBcc');
$subject->setBcc('[email protected]', 'testSetBcc');
$subject->priority(6);
$subject->text('test text body');
$subject->html('test html body');
$subject->setEncoding('UTF-8');

$this->assertInstanceOf(DateTimeImmutable::class, $subject->getDate());
$this->assertInstanceOf(Address::class, $subject->getReturnPath());
$this->assertInstanceOf(Address::class, $subject->getSender());
$this->assertIsArray($subject->getFrom());
$this->assertIsArray($subject->getReplyTo());
$this->assertIsArray($subject->getTo());
$this->assertIsArray($subject->getCc());
$this->assertIsArray($subject->getBcc());
$this->assertIsInt($subject->getPriority());
$this->assertIsString($subject->getTextBody());
$this->assertIsString($subject->getHtmlBody());
$this->assertIsString($subject->getEncoding());
$this->assertIsString($subject->getHtmlCharset());
$this->assertIsString($subject->getTextCharset());
}

public function testEnsureBodyValidity(): void
{
$subject = new Email();
$this->expectException(LogicException::class);
$subject->ensureValidity();
}

public function testEnsureValidity(): void
{
$subject = new Email();
$subject->html('test html body');
$subject->getHeaders()->addHeader('X-Unsent', '1');

$this->expectException(LogicException::class);
$subject->ensureValidity();
}
}
28 changes: 28 additions & 0 deletions test/Factory/AbstractMailFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,32 @@ public function testCanCreateRequestedService(): void
$result = $this->subject->canCreate($container, $requestedName);
$this->assertTrue($result);
}

/**
* @throws ContainerExceptionInterface
* @throws Exception
* @throws NotFoundExceptionInterface
*/
public function testCanCreateWrongRequestedName(): void
{
$container = $this->createMock(ContainerInterface::class);
$requestedName = 'dot-mail.testPart.default.test';

$result = $this->subject->canCreate($container, $requestedName);
$this->assertFalse($result);
}

/**
* @throws ContainerExceptionInterface
* @throws Exception
* @throws NotFoundExceptionInterface
*/
public function testCanCreateRequestedNameWrongParts(): void
{
$container = $this->createMock(ContainerInterface::class);
$requestedName = 'dot-mail-test.test.default';

$result = $this->subject->canCreate($container, $requestedName);
$this->assertFalse($result);
}
}
20 changes: 20 additions & 0 deletions test/Factory/MailOptionsAbstractFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,24 @@ public function testGeneratesMailOptions(): void

$this->assertInstanceOf(MailOptions::class, $subject);
}

/**
* @throws ContainerExceptionInterface
* @throws Exception
* @throws NotFoundExceptionInterface
*/
public function testIsSpecificConfigArray(): void
{
$defaultName = 'dot-mail.options.test';
$container = $this->createMock(ContainerInterface::class);

$container->expects($this->once())
->method('get')
->with('config')
->willReturn($this->config);

$subject = (new MailOptionsAbstractFactory())($container, $defaultName);

$this->assertInstanceOf(MailOptions::class, $subject);
}
}
58 changes: 58 additions & 0 deletions test/Factory/MailServiceAbstractFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DotTest\Mail\Factory;

use Dot\Mail\Event\AbstractMailEventListener;
use Dot\Mail\Exception\InvalidArgumentException;
use Dot\Mail\Exception\RuntimeException;
use Dot\Mail\Factory\MailServiceAbstractFactory as Subject;
use Dot\Mail\Options\AttachmentsOptions;
Expand Down Expand Up @@ -202,4 +203,61 @@ public function testGenerateServiceWithoutProvidedAdapter(): void
$this->assertInstanceOf(SmtpTransport::class, $mailService->getTransport());
$this->assertCount(2, $mailService->getAttachments());
}

/**
* @throws ContainerExceptionInterface
* @throws Exception
* @throws NotFoundExceptionInterface
*/
public function testGenerateServiceInvalidAdapter(): void
{
$requestedName = 'dot-mail.service.default';

$this->mailOptions->expects($this->any())
->method('getTransport')
->willReturn('test');

$this->container->expects($this->atLeastOnce())
->method('get')
->willReturnMap([
['dot-mail.options.default', $this->mailOptions],
[LogServiceInterface::class, $this->createMock(LogService::class)],
['Invalid Listener Test', 'Invalid Listener provided'],
]);

$this->expectException(InvalidArgumentException::class);
(new Subject())($this->container, $requestedName);
}

/**
* @throws ContainerExceptionInterface
* @throws Exception
* @throws NotFoundExceptionInterface
*/
public function testGenerateServiceThrowException(): void
{
$requestedName = 'dot-mail.service.default';

$this->mailOptions->expects($this->any())
->method('getTransport')
->willReturn(InvalidArgumentException::class);

$this->container->expects($this->atLeastOnce())
->method('get')
->willReturnMap([
['dot-mail.options.default', $this->mailOptions],
[LogServiceInterface::class, $this->createMock(LogService::class)],
[SmtpTransport::class, new SmtpTransport()],
['Invalid Listener Test', 'Invalid Listener provided'],
]);
$this->container->expects($this->any())
->method('has')
->willReturnMap([
[InvalidArgumentException::class, true],
['Invalid Listener Test', true],
]);

$this->expectException(InvalidArgumentException::class);
(new Subject())($this->container, $requestedName);
}
}
33 changes: 33 additions & 0 deletions test/Options/SmtpOptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace DotTest\Mail\Options;

use Dot\Mail\Options\SmtpOptions;
use DotTest\Mail\CommonTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;

class SmtpOptionsTest extends TestCase
{
use CommonTrait;

public function testSmtpGettersAndSetters(): void
{
$subject = new SmtpOptions();

$subject->setName('smtpTest');
$subject->setConnectionTimeLimit(123);

$this->assertIsString($subject->getName());
$this->assertIsArray($subject->getConnectionConfig());
$this->assertIsString($subject->getHost());
$this->assertIsString($subject->getConnectionClass());
$this->assertIsInt($subject->getPort());
$this->assertIsInt($subject->getConnectionTimeLimit());

$this->expectException(InvalidArgumentException::class);
$subject->setPort(0);
}
}
2 changes: 1 addition & 1 deletion test/Service/MailServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function testAttachFilesToMimeMessageBody(): void

$mimeMessage = new TextPart($stringMessage);
$this->mailService->setSubject('Test Subject');
$this->message->setBody($mimeMessage);
$this->mailService->setBody($mimeMessage);
$this->mailService->addAttachments([
$this->fileSystem->url() . '/data/mail/attachments/testPdfAttachment.pdf',
$this->fileSystem->url() . '/data/mail/attachments/testXlsAttachment.xls',
Expand Down

0 comments on commit 84edc30

Please sign in to comment.