QueueTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Test\Unit\Model;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class QueueTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Newsletter\Model\Queue
  14. */
  15. protected $queue;
  16. /**
  17. * @var \Magento\Newsletter\Model\Template\Filter|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $templateFilter;
  20. /**
  21. * @var \Magento\Framework\Stdlib\DateTime\DateTime|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $date;
  24. /**
  25. * @var \Magento\Newsletter\Model\TemplateFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $templateFactory;
  28. /**
  29. * @var \Magento\Newsletter\Model\ProblemFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $problemFactory;
  32. /**
  33. * @var \Magento\Newsletter\Model\ResourceModel\Subscriber\Collection|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $subscribersCollection;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $subscribersCollectionFactory;
  40. /**
  41. * @var \Magento\Newsletter\Model\Queue\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $transportBuilder;
  44. /**
  45. * @var \Magento\Newsletter\Model\ResourceModel\Queue|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $resource;
  48. /**
  49. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  50. */
  51. protected $objectManager;
  52. protected function setUp()
  53. {
  54. $this->templateFilter = $this->getMockBuilder(\Magento\Newsletter\Model\Template\Filter::class)
  55. ->disableOriginalConstructor()
  56. ->setMethods(['create'])
  57. ->getMock();
  58. $this->date = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->templateFactory = $this->getMockBuilder(\Magento\Newsletter\Model\TemplateFactory::class)
  62. ->disableOriginalConstructor()
  63. ->setMethods(['create', 'load'])
  64. ->getMock();
  65. $this->problemFactory = $this->getMockBuilder(\Magento\Newsletter\Model\ProblemFactory::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->transportBuilder = $this->getMockBuilder(\Magento\Newsletter\Model\Queue\TransportBuilder::class)
  69. ->disableOriginalConstructor()
  70. ->setMethods(
  71. ['setTemplateData', 'setTemplateOptions', 'setTemplateVars', 'setFrom', 'addTo', 'getTransport']
  72. )
  73. ->getMock();
  74. $this->subscribersCollection =
  75. $this->getMockBuilder(\Magento\Newsletter\Model\ResourceModel\Subscriber\Collection::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->resource = $this->getMockBuilder(\Magento\Newsletter\Model\ResourceModel\Queue::class)
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $this->subscribersCollectionFactory = $this->getMockBuilder(
  82. \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory::class
  83. )
  84. ->disableOriginalConstructor()
  85. ->setMethods(['create'])
  86. ->getMock();
  87. $this->subscribersCollectionFactory->expects($this->any())->method('create')->willReturn(
  88. $this->subscribersCollection
  89. );
  90. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  91. $this->queue = $this->objectManager->getObject(
  92. \Magento\Newsletter\Model\Queue::class,
  93. [
  94. 'templateFilter' => $this->templateFilter,
  95. 'date' => $this->date,
  96. 'templateFactory' => $this->templateFactory,
  97. 'problemFactory' => $this->problemFactory,
  98. 'subscriberCollectionFactory' => $this->subscribersCollectionFactory,
  99. 'transportBuilder' => $this->transportBuilder,
  100. 'resource' => $this->resource
  101. ]
  102. );
  103. }
  104. public function testSendPerSubscriber1()
  105. {
  106. $this->queue->setQueueStatus(2);
  107. $this->queue->setQueueStartAt(1);
  108. $this->assertEquals($this->queue, $this->queue->sendPerSubscriber());
  109. }
  110. public function testSendPerSubscriberZeroSize()
  111. {
  112. $this->queue->setQueueStatus(1);
  113. $this->queue->setQueueStartAt(1);
  114. $this->subscribersCollection->expects($this->once())->method('getQueueJoinedFlag')->willReturn(false);
  115. $this->subscribersCollection->expects($this->once())->method('useQueue')->with($this->queue)->willReturnSelf();
  116. $this->subscribersCollection->expects($this->once())->method('getSize')->willReturn(0);
  117. $this->date->expects($this->once())->method('gmtDate')->willReturn('any_date');
  118. $this->assertEquals($this->queue, $this->queue->sendPerSubscriber());
  119. }
  120. public function testSendPerSubscriber2()
  121. {
  122. $this->queue->setQueueStatus(1);
  123. $this->queue->setQueueStartAt(1);
  124. $collection = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
  125. ->disableOriginalConstructor()
  126. ->setMethods(['getItems'])
  127. ->getMock();
  128. $item = $this->getMockBuilder(\Magento\Newsletter\Model\Subscriber::class)
  129. ->disableOriginalConstructor()
  130. ->setMethods(['getStoreId', 'getSubscriberEmail', 'getSubscriberFullName', 'received'])
  131. ->getMock();
  132. $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
  133. $this->subscribersCollection->expects($this->once())->method('getQueueJoinedFlag')->willReturn(false);
  134. $this->subscribersCollection->expects($this->once())->method('useQueue')->with($this->queue)->willReturnSelf();
  135. $this->subscribersCollection->expects($this->once())->method('getSize')->willReturn(5);
  136. $this->subscribersCollection->expects($this->once())->method('useOnlyUnsent')->willReturnSelf();
  137. $this->subscribersCollection->expects($this->once())->method('showCustomerInfo')->willReturnSelf();
  138. $this->subscribersCollection->expects($this->once())->method('setPageSize')->willReturnSelf();
  139. $this->subscribersCollection->expects($this->once())->method('setCurPage')->willReturnSelf();
  140. $this->subscribersCollection->expects($this->once())->method('load')->willReturn($collection);
  141. $this->transportBuilder->expects($this->once())->method('setTemplateData')->willReturnSelf();
  142. $collection->expects($this->atLeastOnce())->method('getItems')->willReturn([$item]);
  143. $item->expects($this->once())->method('getStoreId')->willReturn('store_id');
  144. $item->expects($this->once())->method('getSubscriberEmail')->willReturn('email');
  145. $item->expects($this->once())->method('getSubscriberFullName')->willReturn('full_name');
  146. $this->transportBuilder->expects($this->once())->method('setTemplateOptions')->willReturnSelf();
  147. $this->transportBuilder->expects($this->once())->method('setTemplateVars')->willReturnSelf();
  148. $this->transportBuilder->expects($this->once())->method('setFrom')->willReturnSelf();
  149. $this->transportBuilder->expects($this->once())->method('addTo')->willReturnSelf();
  150. $this->transportBuilder->expects($this->once())->method('getTransport')->willReturn($transport);
  151. $item->expects($this->once())->method('received')->with($this->queue)->willReturnSelf();
  152. $this->assertEquals($this->queue, $this->queue->sendPerSubscriber());
  153. }
  154. public function testGetDataForSave()
  155. {
  156. $result = [
  157. 'template_id' => 'id',
  158. 'queue_status' => 'status',
  159. 'queue_start_at' => 'start_at',
  160. 'queue_finish_at' => 'finish_at'
  161. ];
  162. $this->queue->setTemplateId('id');
  163. $this->queue->setQueueStatus('status');
  164. $this->queue->setQueueStartAt('start_at');
  165. $this->queue->setQueueFinishAt('finish_at');
  166. $this->assertEquals($result, $this->queue->getDataForSave());
  167. }
  168. public function testGetTemplate()
  169. {
  170. $template = $this->getMockBuilder(\Magento\Newsletter\Model\Template::class)
  171. ->disableOriginalConstructor()
  172. ->getMock();
  173. $this->queue->setTemplateId(2);
  174. $this->templateFactory->expects($this->once())->method('create')->willReturn($template);
  175. $template->expects($this->once())->method('load')->with(2)->willReturnSelf();
  176. $this->assertEquals($template, $this->queue->getTemplate());
  177. }
  178. public function testGetStores()
  179. {
  180. $stores = ['store'];
  181. $this->resource->expects($this->once())->method('getStores')->willReturn($stores);
  182. $this->assertEquals($stores, $this->queue->getStores());
  183. }
  184. }