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