Skip to content

Commit

Permalink
ENGCOM-3683: Unit test fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
p-bystritsky committed Dec 21, 2018
1 parent 85a2a56 commit a04cbf2
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 66 deletions.
134 changes: 134 additions & 0 deletions app/code/Magento/Sitemap/Test/Unit/Model/EmailNotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento\Sitemap\Test\Unit\Model;

use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Mail\TransportInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Sitemap\Model\EmailNotification;
use Magento\Sitemap\Model\Observer;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;
use PHPUnit\Framework\TestCase;

/**
* Test for Magento\Sitemap\Model\EmailNotification
*/
class EmailNotificationTest extends TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var EmailNotification
*/
private $model;

/**
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $scopeConfigMock;

/**
* @var TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
private $transportBuilderMock;

/**
* @var StateInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $inlineTranslationMock;

/**
* @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $objectManagerMock;

protected function setUp()
{
$this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
->getMock();
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->getMock();
$this->transportBuilderMock = $this->getMockBuilder(TransportBuilder::class)
->disableOriginalConstructor()
->getMock();
$this->inlineTranslationMock = $this->getMockBuilder(StateInterface::class)
->getMock();

$this->objectManager = new ObjectManager($this);
$this->model = $this->objectManager->getObject(
EmailNotification::class,
[
'inlineTranslation' => $this->inlineTranslationMock,
'scopeConfig' => $this->scopeConfigMock,
'transportBuilder' => $this->transportBuilderMock,
]
);
}

public function testSendErrors()
{
$exception = 'Sitemap Exception';
$transport = $this->createMock(TransportInterface::class);

$this->scopeConfigMock->expects($this->at(0))
->method('getValue')
->with(
Observer::XML_PATH_ERROR_TEMPLATE,
ScopeInterface::SCOPE_STORE
)
->willReturn('error-recipient@example.com');

$this->inlineTranslationMock->expects($this->once())
->method('suspend');

$this->transportBuilderMock->expects($this->once())
->method('setTemplateIdentifier')
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('setTemplateOptions')
->with([
'area' => FrontNameResolver::AREA_CODE,
'store' => Store::DEFAULT_STORE_ID,
])
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('setTemplateVars')
->with(['warnings' => $exception])
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('setFrom')
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('addTo')
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('getTransport')
->willReturn($transport);

$transport->expects($this->once())
->method('sendMessage');

$this->inlineTranslationMock->expects($this->once())
->method('resume');

$this->model->sendErrors(['Sitemap Exception']);
}
}
108 changes: 42 additions & 66 deletions app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
*/
namespace Magento\Sitemap\Test\Unit\Model;

use Magento\Framework\App\Area;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Sitemap\Model\EmailNotification;
use Magento\Store\Model\App\Emulation;

/**
* Class ObserverTest
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ObserverTest extends \PHPUnit\Framework\TestCase
Expand All @@ -33,21 +37,6 @@ class ObserverTest extends \PHPUnit\Framework\TestCase
*/
private $collectionFactoryMock;

/**
* @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
private $transportBuilderMock;

/**
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;

/**
* @var \Magento\Framework\Translate\Inline\StateInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $inlineTranslationMock;

/**
* @var \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection|\PHPUnit_Framework_MockObject_MockObject
*/
Expand All @@ -63,6 +52,16 @@ class ObserverTest extends \PHPUnit\Framework\TestCase
*/
private $objectManagerMock;

/**
* @var Emulation|\PHPUnit_Framework_MockObject_MockObject
*/
private $appEmulationMock;

/**
* @var EmailNotification|\PHPUnit_Framework_MockObject_MockObject
*/
private $emailNotificationMock;

protected function setUp()
{
$this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
Expand All @@ -74,36 +73,36 @@ protected function setUp()
)->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->transportBuilderMock = $this->getMockBuilder(\Magento\Framework\Mail\Template\TransportBuilder::class)
->disableOriginalConstructor()
->getMock();
$this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
->getMock();
$this->inlineTranslationMock = $this->getMockBuilder(\Magento\Framework\Translate\Inline\StateInterface::class)
->getMock();
$this->sitemapCollectionMock = $this->createPartialMock(
\Magento\Sitemap\Model\ResourceModel\Sitemap\Collection::class,
['getIterator']
);
$this->sitemapMock = $this->createPartialMock(\Magento\Sitemap\Model\Sitemap::class, ['generateXml']);

$this->sitemapMock = $this->createPartialMock(
\Magento\Sitemap\Model\Sitemap::class,
[
'generateXml',
'getStoreId',
]
);
$this->appEmulationMock = $this->createMock(Emulation::class);
$this->emailNotificationMock = $this->createMock(EmailNotification::class);
$this->objectManager = new ObjectManager($this);

$this->observer = $this->objectManager->getObject(
\Magento\Sitemap\Model\Observer::class,
[
'scopeConfig' => $this->scopeConfigMock,
'collectionFactory' => $this->collectionFactoryMock,
'storeManager' => $this->storeManagerMock,
'transportBuilder' => $this->transportBuilderMock,
'inlineTranslation' => $this->inlineTranslationMock
'appEmulation' => $this->appEmulationMock,
'emailNotification' => $this->emailNotificationMock
]
);
}

public function testScheduledGenerateSitemapsSendsExceptionEmail()
{
$exception = 'Sitemap Exception';
$transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
$storeId = 1;

$this->scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true);

Expand All @@ -115,55 +114,32 @@ public function testScheduledGenerateSitemapsSendsExceptionEmail()
->method('getIterator')
->willReturn(new \ArrayIterator([$this->sitemapMock]));

$this->sitemapMock->expects($this->once())
$this->sitemapMock->expects($this->at(0))
->method('getStoreId')
->willReturn($storeId);

$this->sitemapMock->expects($this->at(1))
->method('generateXml')
->willThrowException(new \Exception($exception));

$this->scopeConfigMock->expects($this->at(1))
$this->scopeConfigMock->expects($this->at(0))
->method('getValue')
->with(
\Magento\Sitemap\Model\Observer::XML_PATH_ERROR_RECIPIENT,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
->willReturn('error-recipient@example.com');

$this->inlineTranslationMock->expects($this->once())
->method('suspend');

$this->transportBuilderMock->expects($this->once())
->method('setTemplateIdentifier')
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('setTemplateOptions')
->with([
'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
])
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('setTemplateVars')
->with(['warnings' => $exception])
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('setFrom')
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('addTo')
->will($this->returnSelf());

$this->transportBuilderMock->expects($this->once())
->method('getTransport')
->willReturn($transport);

$transport->expects($this->once())
->method('sendMessage');
$this->appEmulationMock->expects($this->at(0))
->method('startEnvironmentEmulation')
->with(
$storeId,
Area::AREA_FRONTEND,
true
);

$this->inlineTranslationMock->expects($this->once())
->method('resume');
$this->appEmulationMock->expects($this->at(1))
->method('stopEnvironmentEmulation');

$this->observer->scheduledGenerateSitemaps();
}
Expand Down

0 comments on commit a04cbf2

Please sign in to comment.