Skip to content

Commit

Permalink
Merge pull request #4 from visto9259/master
Browse files Browse the repository at this point in the history
To fix issue #3
  • Loading branch information
visto9259 committed Apr 5, 2023
2 parents ae86cf5 + 5255338 commit 7fa2bde
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Service/MessageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down
34 changes: 34 additions & 0 deletions test/LmcMailTest/Mock/MockAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace LmcMailTest\Mock;

class MockAddress implements \Laminas\Mail\Address\AddressInterface
{

const EMAIL_ADDRESS = '[email protected]';
const NAME = 'Test Example';

/**
* @inheritDoc
*/
public function getEmail()
{
return self::EMAIL_ADDRESS;
}

/**
* @inheritDoc
*/
public function getName()
{
return self::NAME;
}

/**
* @inheritDoc
*/
public function toString()
{
return sprintf('%s <%s>', self::NAME, self::EMAIL_ADDRESS);
}
}
33 changes: 32 additions & 1 deletion test/LmcMailTest/Service/MessageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -81,7 +83,36 @@ public function testCreateHtmlMessageManyTos()
$this->assertCount(2, $to);
$addresses = $to->get('[email protected]');
$this->assertNotFalse($addresses,'Address not found');
$this->assertEquals('[email protected]', $addresses->getEmail(), "Was expecting email address to be [email protected]");
$this->assertEquals('[email protected]', $addresses->getEmail(), "Was expecting email address to be [email protected]");
}

public function testCreateHtmlMessageAddressListTo()
{
$addressList = new AddressList();
$addressList->add('[email protected]');
$addressList->add('[email protected]');
$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('[email protected]');
$this->assertNotFalse($addresses,'Address not found');
$this->assertEquals('[email protected]', $addresses->getEmail(), "Was expecting email address to be [email protected]");
}

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('[email protected]');
$this->assertNotFalse($addresses,'Address not found');
$this->assertEquals('[email protected]', $addresses->getEmail(), "Was expecting email address to be [email protected]");
}

public function testSendTextMessage()
Expand Down

0 comments on commit 7fa2bde

Please sign in to comment.