| Current Path : /home/rtorresani/www/vendor/magento/module-directory/Test/Unit/Model/ |
| Current File : //home/rtorresani/www/vendor/magento/module-directory/Test/Unit/Model/ObserverTest.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Directory\Test\Unit\Model;
use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Directory\Model\Currency\Import\Factory;
use Magento\Directory\Model\CurrencyFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Mail\TransportInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Directory\Model\Observer;
use Magento\Directory\Model\Currency\Import\ImportInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* Unit test for \Magento\Directory\Model\Observer
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ObserverTest extends TestCase
{
/**
* @var string
*/
private const STUB_SENDER = 'Sender';
/**
* @var string
*/
private const STUB_ERROR_TEMPLATE = 'currency_import_error_email_template';
/**
* @var Factory|MockObject
*/
private $importFactoryMock;
/**
* @var ScopeConfigInterface|MockObject
*/
private $scopeConfigMock;
/**
* @var StoreManagerInterface|MockObject
*/
private $storeManagerMock;
/**
* @var TransportBuilder|MockObject
*/
private $transportBuilderMock;
/**
* @var CurrencyFactory|MockObject
*/
private $currencyFactoryMock;
/**
* @var StateInterface|MockObject
*/
private $inlineTranslationMock;
/**
* @var Observer
*/
private $model;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->importFactoryMock = $this->createMock(Factory::class);
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
$this->transportBuilderMock = $this->createMock(TransportBuilder::class);
$this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
$this->currencyFactoryMock = $this->createMock(CurrencyFactory::class);
$this->inlineTranslationMock = $this->getMockForAbstractClass(StateInterface::class);
$objectManager = new ObjectManager($this);
$this->model = $objectManager->getObject(
Observer::class,
[
'importFactory' => $this->importFactoryMock,
'scopeConfig' => $this->scopeConfigMock,
'transportBuilder' => $this->transportBuilderMock,
'storeManager' => $this->storeManagerMock,
'currencyFactory' => $this->currencyFactoryMock,
'inlineTranslation' => $this->inlineTranslationMock
]
);
}
/**
* @return void
*/
public function testScheduledUpdateCurrencyRates(): void
{
$importWarnings = ['WARNING: error1', 'WARNING: error2'];
$this->scopeConfigMock->expects($this->any())
->method('getValue')
->withConsecutive(
[
Observer::IMPORT_ENABLE,
ScopeInterface::SCOPE_STORE
],
[
Observer::CRON_STRING_PATH,
ScopeInterface::SCOPE_STORE
],
[
Observer::IMPORT_SERVICE,
ScopeInterface::SCOPE_STORE
],
[
Observer::XML_PATH_ERROR_RECIPIENT,
ScopeInterface::SCOPE_STORE
],
[
Observer::XML_PATH_ERROR_TEMPLATE,
ScopeInterface::SCOPE_STORE
],
[
Observer::XML_PATH_ERROR_IDENTITY,
ScopeInterface::SCOPE_STORE
]
)->willReturnOnConsecutiveCalls(
1,
'* * * * *',
'fixerio',
'test1@email.com,test2@email.com',
self::STUB_ERROR_TEMPLATE,
self::STUB_SENDER
);
$import = $this->getMockForAbstractClass(ImportInterface::class);
$import->expects($this->once())->method('fetchRates')
->willReturn([]);
$import->expects($this->once())->method('getMessages')
->willReturn(['error1', 'error2']);
$this->importFactoryMock->expects($this->once())->method('create')
->with('fixerio')
->willReturn($import);
$this->transportBuilderMock->expects($this->once())
->method('setTemplateIdentifier')
->with(self::STUB_ERROR_TEMPLATE)
->willReturnSelf();
$this->transportBuilderMock->expects($this->once())
->method('setTemplateOptions')
->with(['area' => FrontNameResolver::AREA_CODE, 'store' => Store::DEFAULT_STORE_ID])
->willReturnSelf();
$this->transportBuilderMock->expects($this->once())
->method('setTemplateVars')
->with(['warnings' => join("\n", $importWarnings)])
->willReturnSelf();
$this->transportBuilderMock->expects($this->once())
->method('setFrom')
->with(self::STUB_SENDER)
->willReturnSelf();
$this->transportBuilderMock->expects($this->once())
->method('addTo')
->with(['test1@email.com', 'test2@email.com'])
->willReturnSelf();
$transport = $this->getMockForAbstractClass(TransportInterface::class);
$this->transportBuilderMock->expects($this->once())
->method('getTransport')
->willReturn($transport);
$transport->expects($this->once())
->method('sendMessage')
->willReturnSelf();
$this->inlineTranslationMock->expects($this->once())->method('suspend')->willReturnSelf();
$this->inlineTranslationMock->expects($this->once())->method('resume')->willReturnSelf();
$this->model->scheduledUpdateCurrencyRates([]);
}
}