diff --git a/src/Service/MessageService.php b/src/Service/MessageService.php index 50b1c35..6ce04da 100644 --- a/src/Service/MessageService.php +++ b/src/Service/MessageService.php @@ -148,7 +148,7 @@ public function createHtmlMessage(string|Address|AddressInterface|array|AddressL /** * Create a text message - * @param string|Address|AddressInterface|array|AddressList|Traversable $from + * @param string|Address|AddressInterface|array|AddressList|Traversable|null $from * @param string|Address|AddressInterface|array|AddressList|Traversable $to * @param string $subject * @param string|ModelInterface $nameOrModel @@ -180,16 +180,16 @@ public function send(Message $message): void /** * Create default message - * @param string|Address|AddressInterface|array|AddressList|Traversable $from + * @param string|Address|AddressInterface|array|AddressList|Traversable|null $from * @param string $encoding - * @param string|array $to + * @param string|Address|AddressInterface|array|AddressList|Traversable $to * @param string $subject * @param MimeMessage|string $body * @return Message */ protected function getDefaultMessage(string|Address|AddressInterface|array|AddressList|Traversable|null $from, string $encoding, - string|array $to, + string|Address|AddressInterface|array|AddressList|Traversable $to, string $subject, MimeMessage|string $body): Message { diff --git a/test/LmcMailTest/Mock/MockAddress.php b/test/LmcMailTest/Mock/MockAddress.php new file mode 100644 index 0000000..969f3c8 --- /dev/null +++ b/test/LmcMailTest/Mock/MockAddress.php @@ -0,0 +1,34 @@ +', self::NAME, self::EMAIL_ADDRESS); + } +} diff --git a/test/LmcMailTest/Service/MessageServiceTest.php b/test/LmcMailTest/Service/MessageServiceTest.php index 0ea47e8..d95833c 100644 --- a/test/LmcMailTest/Service/MessageServiceTest.php +++ b/test/LmcMailTest/Service/MessageServiceTest.php @@ -2,12 +2,14 @@ namespace Service; +use Laminas\Mail\AddressList; use Laminas\Mail\Message; use Laminas\Mime\Message as MimeMessage; use Laminas\ServiceManager\ServiceManager; use Laminas\View\Model\ViewModel; use LmcMail\Service\MessageService; use LmcMailTest\Util\ServiceManagerFactory; +use LmcMailTest\Mock\MockAddress; class MessageServiceTest extends \PHPUnit\Framework\TestCase { @@ -81,7 +83,36 @@ public function testCreateHtmlMessageManyTos() $this->assertCount(2, $to); $addresses = $to->get('test@example.com'); $this->assertNotFalse($addresses,'Address not found'); - $this->assertEquals('test@example.com', $addresses->getEmail(), "Was expecting email address to be user@example.com"); + $this->assertEquals('test@example.com', $addresses->getEmail(), "Was expecting email address to be test@example.com"); + } + + public function testCreateHtmlMessageAddressListTo() + { + $addressList = new AddressList(); + $addressList->add('test1@example.com'); + $addressList->add('test2@example.com'); + $message = $this->messageService->createHtmlMessage([],$addressList, 'test','mail/test_html'); + $this->assertInstanceOf(Message::class, $message); + $body = $message->getBody(); + $this->assertInstanceOf(MimeMessage::class, $body); + $to = $message->getTo(); + $this->assertCount(2, $to); + $addresses = $to->get('test1@example.com'); + $this->assertNotFalse($addresses,'Address not found'); + $this->assertEquals('test1@example.com', $addresses->getEmail(), "Was expecting email address to be test1@example.com"); + } + + public function testCreateHtmlMessageAddressInterfaceTo() + { + $message = $this->messageService->createHtmlMessage([],new MockAddress(), 'test','mail/test_html'); + $this->assertInstanceOf(Message::class, $message); + $body = $message->getBody(); + $this->assertInstanceOf(MimeMessage::class, $body); + $to = $message->getTo(); + $this->assertCount(1, $to); + $addresses = $to->get('test@example.com'); + $this->assertNotFalse($addresses,'Address not found'); + $this->assertEquals('test@example.com', $addresses->getEmail(), "Was expecting email address to be test1@example.com"); } public function testSendTextMessage()