public function testSendPerSubscriber2() { $this->queue->setQueueStatus(1); $this->queue->setQueueStartAt(1); $collection = $this->getMockBuilder('\\Magento\\Framework\\Data\\Collection')->disableOriginalConstructor()->setMethods(['getItems'])->getMock(); $item = $this->getMockBuilder('\\Magento\\Newsletter\\Model\\Subscriber')->disableOriginalConstructor()->setMethods(['getStoreId', 'getSubscriberEmail', 'getSubscriberFullName', 'received'])->getMock(); $transport = $this->getMock('\\Magento\\Framework\\Mail\\TransportInterface'); $this->subscribersCollection->expects($this->once())->method('getQueueJoinedFlag')->willReturn(false); $this->subscribersCollection->expects($this->once())->method('useQueue')->with($this->queue)->willReturnSelf(); $this->subscribersCollection->expects($this->once())->method('getSize')->willReturn(5); $this->subscribersCollection->expects($this->once())->method('useOnlyUnsent')->willReturnSelf(); $this->subscribersCollection->expects($this->once())->method('showCustomerInfo')->willReturnSelf(); $this->subscribersCollection->expects($this->once())->method('setPageSize')->willReturnSelf(); $this->subscribersCollection->expects($this->once())->method('setCurPage')->willReturnSelf(); $this->subscribersCollection->expects($this->once())->method('load')->willReturn($collection); $this->transportBuilder->expects($this->once())->method('setTemplateData')->willReturnSelf(); $collection->expects($this->atLeastOnce())->method('getItems')->willReturn([$item]); $item->expects($this->once())->method('getStoreId')->willReturn('store_id'); $item->expects($this->once())->method('getSubscriberEmail')->willReturn('email'); $item->expects($this->once())->method('getSubscriberFullName')->willReturn('full_name'); $this->transportBuilder->expects($this->once())->method('setTemplateOptions')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('setTemplateVars')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('setFrom')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('addTo')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('getTransport')->willReturn($transport); $item->expects($this->once())->method('received')->with($this->queue)->willReturnSelf(); $this->assertEquals($this->queue, $this->queue->sendPerSubscriber()); }
/** * @param int $templateType * @param string $messageType * @param string $bodyText * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetTransport($templateType = \Magento\Framework\App\TemplateTypesInterface::TYPE_HTML, $messageType = \Magento\Framework\Mail\Message::TYPE_HTML, $bodyText = '<h1>Html message</h1>') { $data = array('template_subject' => 'Email Subject', 'template_text' => $bodyText, 'template_styles' => 'Styles', 'template_type' => $templateType); $vars = array('reason' => 'Reason', 'customer' => 'Customer'); $options = array('area' => 'frontend', 'store' => 1); $template = $this->getMock('\\Magento\\Email\\Model\\Template', array(), array(), '', false); $template->expects($this->once())->method('setVars')->with($this->equalTo($vars))->will($this->returnSelf()); $template->expects($this->once())->method('setOptions')->with($this->equalTo($options))->will($this->returnSelf()); $template->expects($this->once())->method('getSubject')->will($this->returnValue('Email Subject')); $template->expects($this->once())->method('getProcessedTemplate')->will($this->returnValue($bodyText)); $template->expects($this->once())->method('setData')->with($this->equalTo($data))->will($this->returnSelf()); $this->templateFactoryMock->expects($this->once())->method('get')->with($this->equalTo('identifier'))->will($this->returnValue($template)); $this->messageMock->expects($this->once())->method('setSubject')->with($this->equalTo('Email Subject'))->will($this->returnSelf()); $this->messageMock->expects($this->once())->method('setMessageType')->with($this->equalTo($messageType))->will($this->returnSelf()); $this->messageMock->expects($this->once())->method('setBody')->with($this->equalTo($bodyText))->will($this->returnSelf()); $transport = $this->getMock('\\Magento\\Framework\\Mail\\TransportInterface'); $this->_mailTransportFactoryMock->expects($this->at(0))->method('create')->with($this->equalTo(array('message' => $this->messageMock)))->will($this->returnValue($transport)); $this->objectManagerMock->expects($this->at(0))->method('create')->with($this->equalTo('Magento\\Framework\\Mail\\Message'))->will($this->returnValue($transport)); $this->builder->setTemplateIdentifier('identifier')->setTemplateVars($vars)->setTemplateOptions($options)->setTemplateData($data); $result = $this->builder->getTransport(); $this->assertInstanceOf('Magento\\Framework\\Mail\\TransportInterface', $result); }
/** * Send messages to subscribers for this queue * * @param int $count * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function sendPerSubscriber($count = 20) { if ($this->getQueueStatus() != self::STATUS_SENDING && ($this->getQueueStatus() != self::STATUS_NEVER && $this->getQueueStartAt())) { return $this; } if (!$this->_subscribersCollection->getQueueJoinedFlag()) { $this->_subscribersCollection->useQueue($this); } if ($this->_subscribersCollection->getSize() == 0) { $this->_finishQueue(); return $this; } $collection = $this->_subscribersCollection->useOnlyUnsent()->showCustomerInfo()->setPageSize($count)->setCurPage(1)->load(); $this->_transportBuilder->setTemplateData(['template_subject' => $this->getNewsletterSubject(), 'template_text' => $this->getNewsletterText(), 'template_styles' => $this->getNewsletterStyles(), 'template_filter' => $this->_templateFilter, 'template_type' => self::TYPE_HTML]); /** @var \Magento\Newsletter\Model\Subscriber $item */ foreach ($collection->getItems() as $item) { $transport = $this->_transportBuilder->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $item->getStoreId()])->setTemplateVars(['subscriber' => $item])->setFrom(['name' => $this->getNewsletterSenderEmail(), 'email' => $this->getNewsletterSenderName()])->addTo($item->getSubscriberEmail(), $item->getSubscriberFullName())->getTransport(); try { $transport->sendMessage(); } catch (\Magento\Framework\Mail\Exception $e) { /** @var \Magento\Newsletter\Model\Problem $problem */ $problem = $this->_problemFactory->create(); $problem->addSubscriberData($item); $problem->addQueueData($this); $problem->addErrorData($e); $problem->save(); } $item->received($this); } if (count($collection->getItems()) < $count - 1 || count($collection->getItems()) == 0) { $this->_finishQueue(); } return $this; }