ObserverTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sitemap\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * Class ObserverTest
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class ObserverTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  16. */
  17. private $objectManager;
  18. /**
  19. * @var \Magento\Sitemap\Model\Observer
  20. */
  21. private $observer;
  22. /**
  23. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $scopeConfigMock;
  26. /**
  27. * @var \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $collectionFactoryMock;
  30. /**
  31. * @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $transportBuilderMock;
  34. /**
  35. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $storeManagerMock;
  38. /**
  39. * @var \Magento\Framework\Translate\Inline\StateInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $inlineTranslationMock;
  42. /**
  43. * @var \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $sitemapCollectionMock;
  46. /**
  47. * @var \Magento\Sitemap\Model\Sitemap|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $sitemapMock;
  50. /**
  51. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $objectManagerMock;
  54. protected function setUp()
  55. {
  56. $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  57. ->getMock();
  58. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  59. ->getMock();
  60. $this->collectionFactoryMock = $this->getMockBuilder(
  61. \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory::class
  62. )->disableOriginalConstructor()
  63. ->setMethods(['create'])
  64. ->getMock();
  65. $this->transportBuilderMock = $this->getMockBuilder(\Magento\Framework\Mail\Template\TransportBuilder::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  69. ->getMock();
  70. $this->inlineTranslationMock = $this->getMockBuilder(\Magento\Framework\Translate\Inline\StateInterface::class)
  71. ->getMock();
  72. $this->sitemapCollectionMock = $this->createPartialMock(
  73. \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection::class,
  74. ['getIterator']
  75. );
  76. $this->sitemapMock = $this->createPartialMock(\Magento\Sitemap\Model\Sitemap::class, ['generateXml']);
  77. $this->objectManager = new ObjectManager($this);
  78. $this->observer = $this->objectManager->getObject(
  79. \Magento\Sitemap\Model\Observer::class,
  80. [
  81. 'scopeConfig' => $this->scopeConfigMock,
  82. 'collectionFactory' => $this->collectionFactoryMock,
  83. 'storeManager' => $this->storeManagerMock,
  84. 'transportBuilder' => $this->transportBuilderMock,
  85. 'inlineTranslation' => $this->inlineTranslationMock
  86. ]
  87. );
  88. }
  89. public function testScheduledGenerateSitemapsSendsExceptionEmail()
  90. {
  91. $exception = 'Sitemap Exception';
  92. $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
  93. $this->scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true);
  94. $this->collectionFactoryMock->expects($this->once())
  95. ->method('create')
  96. ->willReturn($this->sitemapCollectionMock);
  97. $this->sitemapCollectionMock->expects($this->any())
  98. ->method('getIterator')
  99. ->willReturn(new \ArrayIterator([$this->sitemapMock]));
  100. $this->sitemapMock->expects($this->once())
  101. ->method('generateXml')
  102. ->willThrowException(new \Exception($exception));
  103. $this->scopeConfigMock->expects($this->at(1))
  104. ->method('getValue')
  105. ->with(
  106. \Magento\Sitemap\Model\Observer::XML_PATH_ERROR_RECIPIENT,
  107. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  108. )
  109. ->willReturn('error-recipient@example.com');
  110. $this->inlineTranslationMock->expects($this->once())
  111. ->method('suspend');
  112. $this->transportBuilderMock->expects($this->once())
  113. ->method('setTemplateIdentifier')
  114. ->will($this->returnSelf());
  115. $this->transportBuilderMock->expects($this->once())
  116. ->method('setTemplateOptions')
  117. ->with([
  118. 'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
  119. 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
  120. ])
  121. ->will($this->returnSelf());
  122. $this->transportBuilderMock->expects($this->once())
  123. ->method('setTemplateVars')
  124. ->with(['warnings' => $exception])
  125. ->will($this->returnSelf());
  126. $this->transportBuilderMock->expects($this->once())
  127. ->method('setFrom')
  128. ->will($this->returnSelf());
  129. $this->transportBuilderMock->expects($this->once())
  130. ->method('addTo')
  131. ->will($this->returnSelf());
  132. $this->transportBuilderMock->expects($this->once())
  133. ->method('getTransport')
  134. ->willReturn($transport);
  135. $transport->expects($this->once())
  136. ->method('sendMessage');
  137. $this->inlineTranslationMock->expects($this->once())
  138. ->method('resume');
  139. $this->observer->scheduledGenerateSitemaps();
  140. }
  141. }