ObserverTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ProductAlert\Test\Unit\Model;
  7. use Magento\Customer\Api\Data\CustomerInterface;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\ProductAlert\Model\ProductSalability;
  10. /**
  11. * Class ObserverTest
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. * @SuppressWarnings(PHPMD.TooManyFields)
  14. */
  15. class ObserverTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  19. */
  20. private $objectManager;
  21. /**
  22. * @var \Magento\ProductAlert\Model\Observer
  23. */
  24. private $observer;
  25. /**
  26. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $scopeConfigMock;
  29. /**
  30. * @var \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $collectionFactoryMock;
  33. /**
  34. * @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $transportBuilderMock;
  37. /**
  38. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $storeManagerMock;
  41. /**
  42. * @var \Magento\Framework\Translate\Inline\StateInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $inlineTranslationMock;
  45. /**
  46. * @var \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $sitemapCollectionMock;
  49. /**
  50. * @var \Magento\Sitemap\Model\Sitemap|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $sitemapMock;
  53. /**
  54. * @var \Magento\ProductAlert\Model\EmailFactory|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $emailFactoryMock;
  57. /**
  58. * @var \Magento\ProductAlert\Model\Email|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $emailMock;
  61. /**
  62. * @var \Magento\ProductAlert\Model\ResourceModel\Price\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  63. */
  64. private $priceColFactoryMock;
  65. /**
  66. * @var \Magento\ProductAlert\Model\ResourceModel\Stock\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  67. */
  68. private $stockColFactoryMock;
  69. /**
  70. * @var \Magento\Store\Model\Website|\PHPUnit_Framework_MockObject_MockObject
  71. */
  72. private $websiteMock;
  73. /**
  74. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  75. */
  76. private $storeMock;
  77. /**
  78. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  79. */
  80. private $customerRepositoryMock;
  81. /**
  82. * @var \Magento\Catalog\Api\ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  83. */
  84. private $productRepositoryMock;
  85. /**
  86. * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
  87. */
  88. private $productMock;
  89. /**
  90. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  91. */
  92. private $objectManagerMock;
  93. /**
  94. * @var ProductSalability|\PHPUnit_Framework_MockObject_MockObject
  95. */
  96. private $productSalabilityMock;
  97. /**
  98. * @return void
  99. */
  100. protected function setUp()
  101. {
  102. $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  103. ->getMock();
  104. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  105. ->getMock();
  106. $this->collectionFactoryMock = $this->getMockBuilder(
  107. \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory::class
  108. )->disableOriginalConstructor()
  109. ->setMethods(['create'])
  110. ->getMock();
  111. $this->transportBuilderMock = $this->getMockBuilder(\Magento\Framework\Mail\Template\TransportBuilder::class)
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  115. ->getMock();
  116. $this->inlineTranslationMock = $this->getMockBuilder(\Magento\Framework\Translate\Inline\StateInterface::class)
  117. ->getMock();
  118. $this->sitemapCollectionMock = $this->createPartialMock(
  119. \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection::class,
  120. ['getIterator']
  121. );
  122. $this->sitemapMock = $this->createPartialMock(\Magento\Sitemap\Model\Sitemap::class, ['generateXml']);
  123. $this->emailFactoryMock = $this->getMockBuilder(
  124. \Magento\ProductAlert\Model\EmailFactory::class
  125. )->disableOriginalConstructor()
  126. ->setMethods(['create'])
  127. ->getMock();
  128. $this->emailMock = $this->getMockBuilder(\Magento\ProductAlert\Model\Email::class)
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $this->priceColFactoryMock = $this->getMockBuilder(
  132. \Magento\ProductAlert\Model\ResourceModel\Price\CollectionFactory::class
  133. )->disableOriginalConstructor()
  134. ->setMethods(['create', 'addWebsiteFilter', 'setCustomerOrder'])
  135. ->getMock();
  136. $this->stockColFactoryMock = $this->getMockBuilder(
  137. \Magento\ProductAlert\Model\ResourceModel\Stock\CollectionFactory::class
  138. )->disableOriginalConstructor()
  139. ->setMethods(['create', 'addWebsiteFilter', 'setCustomerOrder', 'addStatusFilter'])
  140. ->getMock();
  141. $this->websiteMock = $this->createPartialMock(
  142. \Magento\Store\Model\Website::class,
  143. ['getDefaultGroup', 'getDefaultStore']
  144. );
  145. $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  146. ->disableOriginalConstructor()
  147. ->setMethods(['getDefaultStore', 'getId'])
  148. ->getMock();
  149. $this->customerRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
  150. ->getMock();
  151. $this->productRepositoryMock = $this->getMockBuilder(\Magento\Catalog\Api\ProductRepositoryInterface::class)
  152. ->getMock();
  153. $this->productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  154. ->disableOriginalConstructor()
  155. ->setMethods(
  156. [
  157. 'setCustomerGroupId',
  158. 'getFinalPrice',
  159. ]
  160. )->getMock();
  161. $this->productSalabilityMock = $this->createPartialMock(ProductSalability::class, ['isSalable']);
  162. $this->objectManager = new ObjectManager($this);
  163. $this->observer = $this->objectManager->getObject(
  164. \Magento\ProductAlert\Model\Observer::class,
  165. [
  166. 'scopeConfig' => $this->scopeConfigMock,
  167. 'collectionFactory' => $this->collectionFactoryMock,
  168. 'storeManager' => $this->storeManagerMock,
  169. 'transportBuilder' => $this->transportBuilderMock,
  170. 'inlineTranslation' => $this->inlineTranslationMock,
  171. 'emailFactory' => $this->emailFactoryMock,
  172. 'priceColFactory' => $this->priceColFactoryMock,
  173. 'stockColFactory' => $this->stockColFactoryMock,
  174. 'customerRepository' => $this->customerRepositoryMock,
  175. 'productRepository' => $this->productRepositoryMock,
  176. 'productSalability' => $this->productSalabilityMock
  177. ]
  178. );
  179. }
  180. /**
  181. * @expectedException \Exception
  182. */
  183. public function testGetWebsitesThrowsException()
  184. {
  185. $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(false);
  186. $this->emailFactoryMock->expects($this->once())->method('create')->willReturn($this->emailMock);
  187. $this->storeManagerMock->expects($this->once())->method('getWebsites')->willThrowException(new \Exception());
  188. $this->observer->process();
  189. }
  190. /**
  191. * @expectedException \Exception
  192. */
  193. public function testProcessPriceThrowsException()
  194. {
  195. $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(false);
  196. $this->emailFactoryMock->expects($this->once())->method('create')->willReturn($this->emailMock);
  197. $this->storeManagerMock->expects($this->once())->method('getWebsites')->willReturn([$this->websiteMock]);
  198. $this->websiteMock->expects($this->any())->method('getDefaultGroup')->willReturn($this->storeMock);
  199. $this->storeMock->expects($this->any())->method('getDefaultStore')->willReturnSelf();
  200. $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(true);
  201. $this->priceColFactoryMock->expects($this->once())->method('create')->willThrowException(new \Exception());
  202. $this->observer->process();
  203. }
  204. /**
  205. * @expectedException \Exception
  206. */
  207. public function testProcessPriceCustomerRepositoryThrowsException()
  208. {
  209. $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(false);
  210. $this->emailFactoryMock->expects($this->once())->method('create')->willReturn($this->emailMock);
  211. $this->storeManagerMock->expects($this->once())->method('getWebsites')->willReturn([$this->websiteMock]);
  212. $this->websiteMock->expects($this->any())->method('getDefaultGroup')->willReturn($this->storeMock);
  213. $this->storeMock->expects($this->any())->method('getDefaultStore')->willReturnSelf();
  214. $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(true);
  215. $this->priceColFactoryMock->expects($this->once())->method('create')->willReturnSelf();
  216. $this->priceColFactoryMock->expects($this->once())->method('addWebsiteFilter')->willReturnSelf();
  217. $items = [
  218. new \Magento\Framework\DataObject([
  219. 'customer_id' => '42'
  220. ])
  221. ];
  222. $this->priceColFactoryMock->expects($this->once())
  223. ->method('setCustomerOrder')
  224. ->willReturn(new \ArrayIterator($items));
  225. $this->customerRepositoryMock->expects($this->once())->method('getById')->willThrowException(new \Exception());
  226. $this->observer->process();
  227. }
  228. /**
  229. * @expectedException \Exception
  230. */
  231. public function testProcessPriceEmailThrowsException()
  232. {
  233. $id = 1;
  234. $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(false);
  235. $this->emailFactoryMock->expects($this->once())->method('create')->willReturn($this->emailMock);
  236. $this->storeManagerMock->expects($this->once())->method('getWebsites')->willReturn([$this->websiteMock]);
  237. $this->websiteMock->expects($this->any())->method('getDefaultGroup')->willReturn($this->storeMock);
  238. $this->storeMock->expects($this->any())->method('getDefaultStore')->willReturnSelf();
  239. $this->websiteMock->expects($this->once())->method('getDefaultStore')->willReturn($this->storeMock);
  240. $this->storeMock->expects($this->any())->method('getId')->willReturn(2);
  241. $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(true);
  242. $this->priceColFactoryMock->expects($this->once())->method('create')->willReturnSelf();
  243. $this->priceColFactoryMock->expects($this->once())->method('addWebsiteFilter')->willReturnSelf();
  244. $items = [
  245. new \Magento\Framework\DataObject([
  246. 'customer_id' => $id
  247. ])
  248. ];
  249. $this->priceColFactoryMock->expects($this->once())
  250. ->method('setCustomerOrder')
  251. ->willReturn(new \ArrayIterator($items));
  252. $customerMock = $this->getMockForAbstractClass(CustomerInterface::class);
  253. $this->customerRepositoryMock->expects($this->once())->method('getById')->willReturn($customerMock);
  254. $this->productMock->expects($this->once())->method('setCustomerGroupId')->willReturnSelf();
  255. $this->productMock->expects($this->once())->method('getFinalPrice')->willReturn('655.99');
  256. $this->productRepositoryMock->expects($this->once())->method('getById')->willReturn($this->productMock);
  257. $this->emailMock->expects($this->once())->method('send')->willThrowException(new \Exception());
  258. $this->observer->process();
  259. }
  260. /**
  261. * @expectedException \Exception
  262. */
  263. public function testProcessStockThrowsException()
  264. {
  265. $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(false);
  266. $this->emailFactoryMock->expects($this->once())->method('create')->willReturn($this->emailMock);
  267. $this->storeManagerMock->expects($this->once())->method('getWebsites')->willReturn([$this->websiteMock]);
  268. $this->websiteMock->expects($this->any())->method('getDefaultGroup')->willReturn($this->storeMock);
  269. $this->storeMock->expects($this->any())->method('getDefaultStore')->willReturnSelf();
  270. $this->scopeConfigMock->expects($this->at(0))->method('getValue')->willReturn(false);
  271. $this->scopeConfigMock->expects($this->at(1))->method('getValue')->willReturn(true);
  272. $this->stockColFactoryMock->expects($this->once())->method('create')->willThrowException(new \Exception());
  273. $this->observer->process();
  274. }
  275. /**
  276. * @expectedException \Exception
  277. */
  278. public function testProcessStockCustomerRepositoryThrowsException()
  279. {
  280. $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(false);
  281. $this->emailFactoryMock->expects($this->once())->method('create')->willReturn($this->emailMock);
  282. $this->storeManagerMock->expects($this->once())->method('getWebsites')->willReturn([$this->websiteMock]);
  283. $this->websiteMock->expects($this->any())->method('getDefaultGroup')->willReturn($this->storeMock);
  284. $this->storeMock->expects($this->any())->method('getDefaultStore')->willReturnSelf();
  285. $this->scopeConfigMock->expects($this->at(0))->method('getValue')->willReturn(false);
  286. $this->scopeConfigMock->expects($this->at(1))->method('getValue')->willReturn(true);
  287. $this->stockColFactoryMock->expects($this->once())->method('create')->willReturnSelf();
  288. $this->stockColFactoryMock->expects($this->once())->method('addWebsiteFilter')->willReturnSelf();
  289. $this->stockColFactoryMock->expects($this->once())->method('addStatusFilter')->willReturnSelf();
  290. $items = [
  291. new \Magento\Framework\DataObject([
  292. 'customer_id' => '42'
  293. ])
  294. ];
  295. $this->stockColFactoryMock->expects($this->once())
  296. ->method('setCustomerOrder')
  297. ->willReturn(new \ArrayIterator($items));
  298. $this->customerRepositoryMock->expects($this->once())->method('getById')->willThrowException(new \Exception());
  299. $this->observer->process();
  300. }
  301. /**
  302. * @expectedException \Exception
  303. */
  304. public function testProcessStockEmailThrowsException()
  305. {
  306. $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(false);
  307. $this->emailFactoryMock->expects($this->once())->method('create')->willReturn($this->emailMock);
  308. $this->storeManagerMock->expects($this->once())->method('getWebsites')->willReturn([$this->websiteMock]);
  309. $this->websiteMock->expects($this->any())->method('getDefaultGroup')->willReturn($this->storeMock);
  310. $this->storeMock->expects($this->any())->method('getDefaultStore')->willReturnSelf();
  311. $this->websiteMock->expects($this->once())->method('getDefaultStore')->willReturn($this->storeMock);
  312. $this->storeMock->expects($this->any())->method('getId')->willReturn(2);
  313. $this->scopeConfigMock->expects($this->at(0))->method('getValue')->willReturn(false);
  314. $this->scopeConfigMock->expects($this->at(1))->method('getValue')->willReturn(true);
  315. $this->stockColFactoryMock->expects($this->once())->method('create')->willReturnSelf();
  316. $this->stockColFactoryMock->expects($this->once())->method('addWebsiteFilter')->willReturnSelf();
  317. $this->stockColFactoryMock->expects($this->once())->method('addStatusFilter')->willReturnSelf();
  318. $items = [
  319. new \Magento\Framework\DataObject([
  320. 'customer_id' => '42'
  321. ])
  322. ];
  323. $this->stockColFactoryMock->expects($this->once())
  324. ->method('setCustomerOrder')
  325. ->willReturn(new \ArrayIterator($items));
  326. $customerMock = $this->getMockForAbstractClass(CustomerInterface::class);
  327. $this->customerRepositoryMock->expects($this->once())->method('getById')->willReturn($customerMock);
  328. $this->productMock->expects($this->once())->method('setCustomerGroupId')->willReturnSelf();
  329. $this->productSalabilityMock->expects($this->once())->method('isSalable')->willReturn(false);
  330. $this->productRepositoryMock->expects($this->once())->method('getById')->willReturn($this->productMock);
  331. $this->emailMock->expects($this->once())->method('send')->willThrowException(new \Exception());
  332. $this->observer->process();
  333. }
  334. }