This repository has been archived on 2026-07-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
espocrm-base/tests/unit/Espo/Core/Mail/EmailSenderTest.php
T
Yuri Kuznetsov 6d28fab610 type fixes
2022-03-18 13:05:02 +02:00

156 lines
4.7 KiB
PHP

<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\Core\Mail;
use Espo\Core\InjectableFactory;
use Laminas\{
Mail\Transport\Smtp as SmtpTransport,
};
use Espo\Entities\{
Email,
};
use Espo\Core\{
Mail\EmailSender,
Mail\SmtpTransportFactory,
ORM\EntityManager,
Utils\Config,
Utils\Log,
};
use Espo\Services\InboundEmail as InboundEmailService;
class EmailSenderTest extends \PHPUnit\Framework\TestCase
{
public function setUp() : void
{
$config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
$entityManager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
$injectableFactory = $this->createMock(InjectableFactory::class);
$transportFactory = $this->getMockBuilder(SmtpTransportFactory::class)->disableOriginalConstructor()->getMock();
$this->transport = $this->getMockBuilder(SmtpTransport::class)->disableOriginalConstructor()->getMock();
$log = $this->createMock(Log::class);
$emailSender = new EmailSender(
$config,
$entityManager,
$injectableFactory,
$transportFactory,
$log
);
$transportFactory
->expects($this->any())
->method('create')
->willReturn($this->transport);
$this->emailSender = $emailSender;
$this->config = $config;
$config
->expects($this->any())
->method('get')
->will(
$this->returnValueMap([
['outboundEmailFromAddress', null, null],
['smtpServer', null, 'test-server'],
['smtpPort', null, '85'],
])
);
$entityManager
->expects($this->any())
->method('getRepository')
->will(
$this->returnValueMap([
['outboundEmailFromAddress', null, 'test@from.com'],
['smtpServer', null, 'test-server'],
])
);
$inboundEmailService = $this->createMock(InboundEmailService::class);
$injectableFactory
->expects($this->any())
->method('create')
->will(
$this->returnValueMap([
[InboundEmailService::class, $inboundEmailService],
])
);
}
protected function createEmail(array $data) : Email
{
$email = $this->getMockBuilder(Email::class)->disableOriginalConstructor()->getMock();
$email
->expects($this->any())
->method('get')
->will(
$this->returnCallback(
function ($name) use ($data) {
return $data[$name] ?? null;
}
)
);
$email
->expects($this->any())
->method('getBodyPlainForSending')
->willReturn('test');
$email
->expects($this->any())
->method('isNew')
->willReturn(true);
return $email;
}
public function testSend1()
{
$email = $this->createEmail([
'name' => 'test',
'from' => 'test@tester.com',
]);
$this->transport
->expects($this->once())
->method('send');
$this->emailSender->send($email);
}
}