Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ObserverTest.php
Go to the documentation of this file.
1 <?php
7 
9 
14 class ObserverTest extends \PHPUnit\Framework\TestCase
15 {
19  private $objectManager;
20 
24  private $observer;
25 
29  private $scopeConfigMock;
30 
34  private $collectionFactoryMock;
35 
39  private $transportBuilderMock;
40 
44  private $storeManagerMock;
45 
49  private $inlineTranslationMock;
50 
54  private $sitemapCollectionMock;
55 
59  private $sitemapMock;
60 
64  private $objectManagerMock;
65 
66  protected function setUp()
67  {
68  $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
69  ->getMock();
70  $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
71  ->getMock();
72  $this->collectionFactoryMock = $this->getMockBuilder(
73  \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory::class
74  )->disableOriginalConstructor()
75  ->setMethods(['create'])
76  ->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();
84  $this->sitemapCollectionMock = $this->createPartialMock(
85  \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection::class,
86  ['getIterator']
87  );
88  $this->sitemapMock = $this->createPartialMock(\Magento\Sitemap\Model\Sitemap::class, ['generateXml']);
89 
90  $this->objectManager = new ObjectManager($this);
91  $this->observer = $this->objectManager->getObject(
92  \Magento\Sitemap\Model\Observer::class,
93  [
94  'scopeConfig' => $this->scopeConfigMock,
95  'collectionFactory' => $this->collectionFactoryMock,
96  'storeManager' => $this->storeManagerMock,
97  'transportBuilder' => $this->transportBuilderMock,
98  'inlineTranslation' => $this->inlineTranslationMock
99  ]
100  );
101  }
102 
104  {
105  $exception = 'Sitemap Exception';
106  $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
107 
108  $this->scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true);
109 
110  $this->collectionFactoryMock->expects($this->once())
111  ->method('create')
112  ->willReturn($this->sitemapCollectionMock);
113 
114  $this->sitemapCollectionMock->expects($this->any())
115  ->method('getIterator')
116  ->willReturn(new \ArrayIterator([$this->sitemapMock]));
117 
118  $this->sitemapMock->expects($this->once())
119  ->method('generateXml')
120  ->willThrowException(new \Exception($exception));
121 
122  $this->scopeConfigMock->expects($this->at(1))
123  ->method('getValue')
124  ->with(
125  \Magento\Sitemap\Model\Observer::XML_PATH_ERROR_RECIPIENT,
127  )
128  ->willReturn('[email protected]');
129 
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');
164 
165  $this->inlineTranslationMock->expects($this->once())
166  ->method('resume');
167 
168  $this->observer->scheduledGenerateSitemaps();
169  }
170 }