Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QueueTest.php
Go to the documentation of this file.
1 <?php
7 
11 class QueueTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $queue;
17 
21  protected $templateFilter;
22 
26  protected $date;
27 
31  protected $templateFactory;
32 
36  protected $problemFactory;
37 
42 
47 
51  protected $transportBuilder;
52 
56  protected $resource;
57 
61  protected $objectManager;
62 
63  protected function setUp()
64  {
65  $this->templateFilter = $this->getMockBuilder(\Magento\Newsletter\Model\Template\Filter::class)
66  ->disableOriginalConstructor()
67  ->setMethods(['create'])
68  ->getMock();
69  $this->date = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
70  ->disableOriginalConstructor()
71  ->getMock();
72  $this->templateFactory = $this->getMockBuilder(\Magento\Newsletter\Model\TemplateFactory::class)
73  ->disableOriginalConstructor()
74  ->setMethods(['create', 'load'])
75  ->getMock();
76  $this->problemFactory = $this->getMockBuilder(\Magento\Newsletter\Model\ProblemFactory::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79  $this->transportBuilder = $this->getMockBuilder(\Magento\Newsletter\Model\Queue\TransportBuilder::class)
80  ->disableOriginalConstructor()
81  ->setMethods(
82  ['setTemplateData', 'setTemplateOptions', 'setTemplateVars', 'setFrom', 'addTo', 'getTransport']
83  )
84  ->getMock();
85  $this->subscribersCollection =
86  $this->getMockBuilder(\Magento\Newsletter\Model\ResourceModel\Subscriber\Collection::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->resource = $this->getMockBuilder(\Magento\Newsletter\Model\ResourceModel\Queue::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $this->subscribersCollectionFactory = $this->getMockBuilder(
93  \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory::class
94  )
95  ->disableOriginalConstructor()
96  ->setMethods(['create'])
97  ->getMock();
98  $this->subscribersCollectionFactory->expects($this->any())->method('create')->willReturn(
99  $this->subscribersCollection
100  );
101 
102  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
103 
104  $this->queue = $this->objectManager->getObject(
105  \Magento\Newsletter\Model\Queue::class,
106  [
107  'templateFilter' => $this->templateFilter,
108  'date' => $this->date,
109  'templateFactory' => $this->templateFactory,
110  'problemFactory' => $this->problemFactory,
111  'subscriberCollectionFactory' => $this->subscribersCollectionFactory,
112  'transportBuilder' => $this->transportBuilder,
113  'resource' => $this->resource
114  ]
115  );
116  }
117 
118  public function testSendPerSubscriber1()
119  {
120  $this->queue->setQueueStatus(2);
121  $this->queue->setQueueStartAt(1);
122 
123  $this->assertEquals($this->queue, $this->queue->sendPerSubscriber());
124  }
125 
127  {
128  $this->queue->setQueueStatus(1);
129  $this->queue->setQueueStartAt(1);
130  $this->subscribersCollection->expects($this->once())->method('getQueueJoinedFlag')->willReturn(false);
131  $this->subscribersCollection->expects($this->once())->method('useQueue')->with($this->queue)->willReturnSelf();
132  $this->subscribersCollection->expects($this->once())->method('getSize')->willReturn(0);
133  $this->date->expects($this->once())->method('gmtDate')->willReturn('any_date');
134 
135  $this->assertEquals($this->queue, $this->queue->sendPerSubscriber());
136  }
137 
138  public function testSendPerSubscriber2()
139  {
140  $this->queue->setQueueStatus(1);
141  $this->queue->setQueueStartAt(1);
142  $collection = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
143  ->disableOriginalConstructor()
144  ->setMethods(['getItems'])
145  ->getMock();
146  $item = $this->getMockBuilder(\Magento\Newsletter\Model\Subscriber::class)
147  ->disableOriginalConstructor()
148  ->setMethods(['getStoreId', 'getSubscriberEmail', 'getSubscriberFullName', 'received'])
149  ->getMock();
150  $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
151  $this->subscribersCollection->expects($this->once())->method('getQueueJoinedFlag')->willReturn(false);
152  $this->subscribersCollection->expects($this->once())->method('useQueue')->with($this->queue)->willReturnSelf();
153  $this->subscribersCollection->expects($this->once())->method('getSize')->willReturn(5);
154  $this->subscribersCollection->expects($this->once())->method('useOnlyUnsent')->willReturnSelf();
155  $this->subscribersCollection->expects($this->once())->method('showCustomerInfo')->willReturnSelf();
156  $this->subscribersCollection->expects($this->once())->method('setPageSize')->willReturnSelf();
157  $this->subscribersCollection->expects($this->once())->method('setCurPage')->willReturnSelf();
158  $this->subscribersCollection->expects($this->once())->method('load')->willReturn($collection);
159  $this->transportBuilder->expects($this->once())->method('setTemplateData')->willReturnSelf();
160  $collection->expects($this->atLeastOnce())->method('getItems')->willReturn([$item]);
161  $item->expects($this->once())->method('getStoreId')->willReturn('store_id');
162  $item->expects($this->once())->method('getSubscriberEmail')->willReturn('email');
163  $item->expects($this->once())->method('getSubscriberFullName')->willReturn('full_name');
164  $this->transportBuilder->expects($this->once())->method('setTemplateOptions')->willReturnSelf();
165  $this->transportBuilder->expects($this->once())->method('setTemplateVars')->willReturnSelf();
166  $this->transportBuilder->expects($this->once())->method('setFrom')->willReturnSelf();
167  $this->transportBuilder->expects($this->once())->method('addTo')->willReturnSelf();
168  $this->transportBuilder->expects($this->once())->method('getTransport')->willReturn($transport);
169  $item->expects($this->once())->method('received')->with($this->queue)->willReturnSelf();
170 
171  $this->assertEquals($this->queue, $this->queue->sendPerSubscriber());
172  }
173 
174  public function testGetDataForSave()
175  {
176  $result = [
177  'template_id' => 'id',
178  'queue_status' => 'status',
179  'queue_start_at' => 'start_at',
180  'queue_finish_at' => 'finish_at'
181  ];
182  $this->queue->setTemplateId('id');
183  $this->queue->setQueueStatus('status');
184  $this->queue->setQueueStartAt('start_at');
185  $this->queue->setQueueFinishAt('finish_at');
186 
187  $this->assertEquals($result, $this->queue->getDataForSave());
188  }
189 
190  public function testGetTemplate()
191  {
192  $template = $this->getMockBuilder(\Magento\Newsletter\Model\Template::class)
193  ->disableOriginalConstructor()
194  ->getMock();
195  $this->queue->setTemplateId(2);
196  $this->templateFactory->expects($this->once())->method('create')->willReturn($template);
197  $template->expects($this->once())->method('load')->with(2)->willReturnSelf();
198 
199  $this->assertEquals($template, $this->queue->getTemplate());
200  }
201 
202  public function testGetStores()
203  {
204  $stores = ['store'];
205  $this->resource->expects($this->once())->method('getStores')->willReturn($stores);
206 
207  $this->assertEquals($stores, $this->queue->getStores());
208  }
209 }
$template
Definition: export.php:12