diff --git a/test/CommonTrait.php b/test/CommonTrait.php index 4dca561..4a21f3b 100644 --- a/test/CommonTrait.php +++ b/test/CommonTrait.php @@ -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', diff --git a/test/EmailTest.php b/test/EmailTest.php new file mode 100644 index 0000000..629b8d6 --- /dev/null +++ b/test/EmailTest.php @@ -0,0 +1,69 @@ +date(new DateTimeImmutable('now')); + $subject->returnPath('test@gmail.com'); + $subject->setSender('test@gmail.com', 'testSender'); + $subject->addFrom(['testAddFrom@gmail.com', 'testAddFrom@gmail2.com'], 'testAddFrom'); + $subject->setFrom('testSetFrom@gmail.com', 'testSetFrom'); + $subject->addReplyTo('testaddReplyTo@gmail.com', 'testAddReplyTo'); + $subject->setReplyTo('testSetReplyTo@gmail.com', 'testSetReplyTo'); + $subject->addTo('testAddTo@gmail.com', 'testAddTo'); + $subject->setTo('testAddTo@gmail.com', 'testAddTo'); + $subject->addCc('testAddCc@gmail.com', 'testAddCc'); + $subject->setCc(new Address('testSetCc@gmail.com'), 'testSetBcc'); + $subject->addBcc('testAddBcc@gmail.com', 'testAddBcc'); + $subject->setBcc('testSetBcc@gmail.com', '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(); + } +} diff --git a/test/Factory/AbstractMailFactoryTest.php b/test/Factory/AbstractMailFactoryTest.php index 6f7659f..6f355f7 100644 --- a/test/Factory/AbstractMailFactoryTest.php +++ b/test/Factory/AbstractMailFactoryTest.php @@ -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); + } } diff --git a/test/Factory/MailOptionsAbstractFactoryTest.php b/test/Factory/MailOptionsAbstractFactoryTest.php index 43b2ac9..650a48f 100644 --- a/test/Factory/MailOptionsAbstractFactoryTest.php +++ b/test/Factory/MailOptionsAbstractFactoryTest.php @@ -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); + } } diff --git a/test/Factory/MailServiceAbstractFactoryTest.php b/test/Factory/MailServiceAbstractFactoryTest.php index 1b16e12..13565e8 100644 --- a/test/Factory/MailServiceAbstractFactoryTest.php +++ b/test/Factory/MailServiceAbstractFactoryTest.php @@ -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; @@ -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); + } } diff --git a/test/Options/SmtpOptionsTest.php b/test/Options/SmtpOptionsTest.php new file mode 100644 index 0000000..3b5bd54 --- /dev/null +++ b/test/Options/SmtpOptionsTest.php @@ -0,0 +1,33 @@ +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); + } +} diff --git a/test/Service/MailServiceTest.php b/test/Service/MailServiceTest.php index 95ca3c4..02bae60 100644 --- a/test/Service/MailServiceTest.php +++ b/test/Service/MailServiceTest.php @@ -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',