Skip to content

Commit a04cbf2

Browse files
committed
ENGCOM-3683: Unit test fix.
1 parent 85a2a56 commit a04cbf2

File tree

2 files changed

+176
-66
lines changed

2 files changed

+176
-66
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types = 1);
7+
8+
namespace Magento\Sitemap\Test\Unit\Model;
9+
10+
use Magento\Backend\App\Area\FrontNameResolver;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\Mail\Template\TransportBuilder;
13+
use Magento\Framework\Mail\TransportInterface;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use Magento\Framework\Translate\Inline\StateInterface;
17+
use Magento\Sitemap\Model\EmailNotification;
18+
use Magento\Sitemap\Model\Observer;
19+
use Magento\Store\Model\ScopeInterface;
20+
use Magento\Store\Model\Store;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* Test for Magento\Sitemap\Model\EmailNotification
25+
*/
26+
class EmailNotificationTest extends TestCase
27+
{
28+
/**
29+
* @var ObjectManager
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @var EmailNotification
35+
*/
36+
private $model;
37+
38+
/**
39+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
private $scopeConfigMock;
42+
43+
/**
44+
* @var TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
private $transportBuilderMock;
47+
48+
/**
49+
* @var StateInterface|\PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
private $inlineTranslationMock;
52+
53+
/**
54+
* @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
55+
*/
56+
private $objectManagerMock;
57+
58+
protected function setUp()
59+
{
60+
$this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
61+
->getMock();
62+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
63+
->getMock();
64+
$this->transportBuilderMock = $this->getMockBuilder(TransportBuilder::class)
65+
->disableOriginalConstructor()
66+
->getMock();
67+
$this->inlineTranslationMock = $this->getMockBuilder(StateInterface::class)
68+
->getMock();
69+
70+
$this->objectManager = new ObjectManager($this);
71+
$this->model = $this->objectManager->getObject(
72+
EmailNotification::class,
73+
[
74+
'inlineTranslation' => $this->inlineTranslationMock,
75+
'scopeConfig' => $this->scopeConfigMock,
76+
'transportBuilder' => $this->transportBuilderMock,
77+
]
78+
);
79+
}
80+
81+
public function testSendErrors()
82+
{
83+
$exception = 'Sitemap Exception';
84+
$transport = $this->createMock(TransportInterface::class);
85+
86+
$this->scopeConfigMock->expects($this->at(0))
87+
->method('getValue')
88+
->with(
89+
Observer::XML_PATH_ERROR_TEMPLATE,
90+
ScopeInterface::SCOPE_STORE
91+
)
92+
->willReturn('[email protected]');
93+
94+
$this->inlineTranslationMock->expects($this->once())
95+
->method('suspend');
96+
97+
$this->transportBuilderMock->expects($this->once())
98+
->method('setTemplateIdentifier')
99+
->will($this->returnSelf());
100+
101+
$this->transportBuilderMock->expects($this->once())
102+
->method('setTemplateOptions')
103+
->with([
104+
'area' => FrontNameResolver::AREA_CODE,
105+
'store' => Store::DEFAULT_STORE_ID,
106+
])
107+
->will($this->returnSelf());
108+
109+
$this->transportBuilderMock->expects($this->once())
110+
->method('setTemplateVars')
111+
->with(['warnings' => $exception])
112+
->will($this->returnSelf());
113+
114+
$this->transportBuilderMock->expects($this->once())
115+
->method('setFrom')
116+
->will($this->returnSelf());
117+
118+
$this->transportBuilderMock->expects($this->once())
119+
->method('addTo')
120+
->will($this->returnSelf());
121+
122+
$this->transportBuilderMock->expects($this->once())
123+
->method('getTransport')
124+
->willReturn($transport);
125+
126+
$transport->expects($this->once())
127+
->method('sendMessage');
128+
129+
$this->inlineTranslationMock->expects($this->once())
130+
->method('resume');
131+
132+
$this->model->sendErrors(['Sitemap Exception']);
133+
}
134+
}

app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
*/
66
namespace Magento\Sitemap\Test\Unit\Model;
77

8+
use Magento\Framework\App\Area;
89
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Sitemap\Model\EmailNotification;
11+
use Magento\Store\Model\App\Emulation;
912

1013
/**
1114
* Class ObserverTest
15+
*
1216
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1317
*/
1418
class ObserverTest extends \PHPUnit\Framework\TestCase
@@ -33,21 +37,6 @@ class ObserverTest extends \PHPUnit\Framework\TestCase
3337
*/
3438
private $collectionFactoryMock;
3539

36-
/**
37-
* @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
38-
*/
39-
private $transportBuilderMock;
40-
41-
/**
42-
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
43-
*/
44-
private $storeManagerMock;
45-
46-
/**
47-
* @var \Magento\Framework\Translate\Inline\StateInterface|\PHPUnit_Framework_MockObject_MockObject
48-
*/
49-
private $inlineTranslationMock;
50-
5140
/**
5241
* @var \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection|\PHPUnit_Framework_MockObject_MockObject
5342
*/
@@ -63,6 +52,16 @@ class ObserverTest extends \PHPUnit\Framework\TestCase
6352
*/
6453
private $objectManagerMock;
6554

55+
/**
56+
* @var Emulation|\PHPUnit_Framework_MockObject_MockObject
57+
*/
58+
private $appEmulationMock;
59+
60+
/**
61+
* @var EmailNotification|\PHPUnit_Framework_MockObject_MockObject
62+
*/
63+
private $emailNotificationMock;
64+
6665
protected function setUp()
6766
{
6867
$this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
@@ -74,36 +73,36 @@ protected function setUp()
7473
)->disableOriginalConstructor()
7574
->setMethods(['create'])
7675
->getMock();
77-
$this->transportBuilderMock = $this->getMockBuilder(\Magento\Framework\Mail\Template\TransportBuilder::class)
78-
->disableOriginalConstructor()
79-
->getMock();
80-
$this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
81-
->getMock();
82-
$this->inlineTranslationMock = $this->getMockBuilder(\Magento\Framework\Translate\Inline\StateInterface::class)
83-
->getMock();
8476
$this->sitemapCollectionMock = $this->createPartialMock(
8577
\Magento\Sitemap\Model\ResourceModel\Sitemap\Collection::class,
8678
['getIterator']
8779
);
88-
$this->sitemapMock = $this->createPartialMock(\Magento\Sitemap\Model\Sitemap::class, ['generateXml']);
89-
80+
$this->sitemapMock = $this->createPartialMock(
81+
\Magento\Sitemap\Model\Sitemap::class,
82+
[
83+
'generateXml',
84+
'getStoreId',
85+
]
86+
);
87+
$this->appEmulationMock = $this->createMock(Emulation::class);
88+
$this->emailNotificationMock = $this->createMock(EmailNotification::class);
9089
$this->objectManager = new ObjectManager($this);
90+
9191
$this->observer = $this->objectManager->getObject(
9292
\Magento\Sitemap\Model\Observer::class,
9393
[
9494
'scopeConfig' => $this->scopeConfigMock,
9595
'collectionFactory' => $this->collectionFactoryMock,
96-
'storeManager' => $this->storeManagerMock,
97-
'transportBuilder' => $this->transportBuilderMock,
98-
'inlineTranslation' => $this->inlineTranslationMock
96+
'appEmulation' => $this->appEmulationMock,
97+
'emailNotification' => $this->emailNotificationMock
9998
]
10099
);
101100
}
102101

103102
public function testScheduledGenerateSitemapsSendsExceptionEmail()
104103
{
105104
$exception = 'Sitemap Exception';
106-
$transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
105+
$storeId = 1;
107106

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

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

118-
$this->sitemapMock->expects($this->once())
117+
$this->sitemapMock->expects($this->at(0))
118+
->method('getStoreId')
119+
->willReturn($storeId);
120+
121+
$this->sitemapMock->expects($this->at(1))
119122
->method('generateXml')
120123
->willThrowException(new \Exception($exception));
121124

122-
$this->scopeConfigMock->expects($this->at(1))
125+
$this->scopeConfigMock->expects($this->at(0))
123126
->method('getValue')
124127
->with(
125128
\Magento\Sitemap\Model\Observer::XML_PATH_ERROR_RECIPIENT,
126129
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
127130
)
128131
->willReturn('[email protected]');
129132

130-
$this->inlineTranslationMock->expects($this->once())
131-
->method('suspend');
132-
133-
$this->transportBuilderMock->expects($this->once())
134-
->method('setTemplateIdentifier')
135-
->will($this->returnSelf());
136-
137-
$this->transportBuilderMock->expects($this->once())
138-
->method('setTemplateOptions')
139-
->with([
140-
'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
141-
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
142-
])
143-
->will($this->returnSelf());
144-
145-
$this->transportBuilderMock->expects($this->once())
146-
->method('setTemplateVars')
147-
->with(['warnings' => $exception])
148-
->will($this->returnSelf());
149-
150-
$this->transportBuilderMock->expects($this->once())
151-
->method('setFrom')
152-
->will($this->returnSelf());
153-
154-
$this->transportBuilderMock->expects($this->once())
155-
->method('addTo')
156-
->will($this->returnSelf());
157-
158-
$this->transportBuilderMock->expects($this->once())
159-
->method('getTransport')
160-
->willReturn($transport);
161-
162-
$transport->expects($this->once())
163-
->method('sendMessage');
133+
$this->appEmulationMock->expects($this->at(0))
134+
->method('startEnvironmentEmulation')
135+
->with(
136+
$storeId,
137+
Area::AREA_FRONTEND,
138+
true
139+
);
164140

165-
$this->inlineTranslationMock->expects($this->once())
166-
->method('resume');
141+
$this->appEmulationMock->expects($this->at(1))
142+
->method('stopEnvironmentEmulation');
167143

168144
$this->observer->scheduledGenerateSitemaps();
169145
}

0 commit comments

Comments
 (0)