Use mailerFactory in SendingQueue Mailer task
[MAILPOET-4115]
This commit is contained in:
committed by
Veljko V
parent
6b758d90e6
commit
d4a8315933
@@ -81,14 +81,14 @@ class SendingQueue {
|
|||||||
WPFunctions $wp,
|
WPFunctions $wp,
|
||||||
Links $links,
|
Links $links,
|
||||||
ScheduledTasksRepository $scheduledTasksRepository,
|
ScheduledTasksRepository $scheduledTasksRepository,
|
||||||
$mailerTask = false,
|
MailerTask $mailerTask,
|
||||||
$newsletterTask = false
|
$newsletterTask = false
|
||||||
) {
|
) {
|
||||||
$this->errorHandler = $errorHandler;
|
$this->errorHandler = $errorHandler;
|
||||||
$this->throttlingHandler = $throttlingHandler;
|
$this->throttlingHandler = $throttlingHandler;
|
||||||
$this->statsNotificationsScheduler = $statsNotificationsScheduler;
|
$this->statsNotificationsScheduler = $statsNotificationsScheduler;
|
||||||
$this->subscribersFinder = $subscriberFinder;
|
$this->subscribersFinder = $subscriberFinder;
|
||||||
$this->mailerTask = ($mailerTask) ? $mailerTask : new MailerTask();
|
$this->mailerTask = $mailerTask;
|
||||||
$this->newsletterTask = ($newsletterTask) ? $newsletterTask : new NewsletterTask();
|
$this->newsletterTask = ($newsletterTask) ? $newsletterTask : new NewsletterTask();
|
||||||
$this->segmentsRepository = $segmentsRepository;
|
$this->segmentsRepository = $segmentsRepository;
|
||||||
$this->mailerMetaInfo = new MetaInfo;
|
$this->mailerMetaInfo = new MetaInfo;
|
||||||
|
@@ -2,39 +2,45 @@
|
|||||||
|
|
||||||
namespace MailPoet\Cron\Workers\SendingQueue\Tasks;
|
namespace MailPoet\Cron\Workers\SendingQueue\Tasks;
|
||||||
|
|
||||||
use MailPoet\Mailer\Mailer as MailerFactory;
|
use MailPoet\Mailer\Mailer as MailerInstance;
|
||||||
|
use MailPoet\Mailer\MailerFactory;
|
||||||
use MailPoet\Mailer\MailerLog;
|
use MailPoet\Mailer\MailerLog;
|
||||||
|
use MailPoet\Mailer\Methods\MailPoet;
|
||||||
|
|
||||||
class Mailer {
|
class Mailer {
|
||||||
public $mailer;
|
/** @var MailerFactory */
|
||||||
|
private $mailerFactory;
|
||||||
|
|
||||||
|
/** @var MailerInstance */
|
||||||
|
private $mailer;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$mailer = false
|
MailerFactory $mailerFactory
|
||||||
) {
|
) {
|
||||||
$this->mailer = ($mailer) ? $mailer : $this->configureMailer();
|
$this->mailerFactory = $mailerFactory;
|
||||||
|
$this->mailer = $this->configureMailer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function configureMailer($newsletter = null) {
|
public function configureMailer($newsletter = null) {
|
||||||
$sender['address'] = (!empty($newsletter->senderAddress)) ?
|
$sender['address'] = (!empty($newsletter->senderAddress)) ?
|
||||||
$newsletter->senderAddress :
|
$newsletter->senderAddress :
|
||||||
false;
|
null;
|
||||||
$sender['name'] = (!empty($newsletter->senderName)) ?
|
$sender['name'] = (!empty($newsletter->senderName)) ?
|
||||||
$newsletter->senderName :
|
$newsletter->senderName :
|
||||||
false;
|
null;
|
||||||
$replyTo['address'] = (!empty($newsletter->replyToAddress)) ?
|
$replyTo['address'] = (!empty($newsletter->replyToAddress)) ?
|
||||||
$newsletter->replyToAddress :
|
$newsletter->replyToAddress :
|
||||||
false;
|
null;
|
||||||
$replyTo['name'] = (!empty($newsletter->replyToName)) ?
|
$replyTo['name'] = (!empty($newsletter->replyToName)) ?
|
||||||
$newsletter->replyToName :
|
$newsletter->replyToName :
|
||||||
false;
|
null;
|
||||||
if (!$sender['address']) {
|
if (!$sender['address']) {
|
||||||
$sender = false;
|
$sender = null;
|
||||||
}
|
}
|
||||||
if (!$replyTo['address']) {
|
if (!$replyTo['address']) {
|
||||||
$replyTo = false;
|
$replyTo = null;
|
||||||
}
|
}
|
||||||
$this->mailer = new MailerFactory();
|
$this->mailer = $this->mailerFactory->buildMailer(null, $sender, $replyTo);
|
||||||
$this->mailer->init($method = false, $sender, $replyTo);
|
|
||||||
return $this->mailer;
|
return $this->mailer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +53,7 @@ class Mailer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getProcessingMethod() {
|
public function getProcessingMethod() {
|
||||||
return ($this->mailer->mailerConfig['method'] === MailerFactory::METHOD_MAILPOET) ?
|
return ($this->mailer->mailerInstance instanceof MailPoet) ?
|
||||||
'bulk' :
|
'bulk' :
|
||||||
'individual';
|
'individual';
|
||||||
}
|
}
|
||||||
|
@@ -190,6 +190,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Cron\Workers\StatsNotifications\Scheduler::class);
|
$container->autowire(\MailPoet\Cron\Workers\StatsNotifications\Scheduler::class);
|
||||||
$container->autowire(\MailPoet\Cron\Workers\StatsNotifications\StatsNotificationsRepository::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\Workers\StatsNotifications\StatsNotificationsRepository::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Cron\Workers\StatsNotifications\NewsletterLinkRepository::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\Workers\StatsNotifications\NewsletterLinkRepository::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Cron\Workers\SendingQueue\Tasks\Mailer::class)->setPublic(true);
|
||||||
// Cron workers
|
// Cron workers
|
||||||
$container->autowire(\MailPoet\Cron\Workers\Scheduler::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\Workers\Scheduler::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Cron\Workers\SendingQueue\SendingQueue::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\Workers\SendingQueue\SendingQueue::class)->setPublic(true);
|
||||||
|
@@ -118,7 +118,7 @@ class MailerFactory {
|
|||||||
$sender = $this->settings->get('sender', []);
|
$sender = $this->settings->get('sender', []);
|
||||||
if (empty($sender['address'])) throw new InvalidStateException(__('Sender name and email are not configured.', 'mailpoet'));
|
if (empty($sender['address'])) throw new InvalidStateException(__('Sender name and email are not configured.', 'mailpoet'));
|
||||||
}
|
}
|
||||||
$fromName = $this->encodeAddressNamePart($sender['name']);
|
$fromName = $this->encodeAddressNamePart($sender['name'] ?? '');
|
||||||
return [
|
return [
|
||||||
'from_name' => $fromName,
|
'from_name' => $fromName,
|
||||||
'from_email' => $sender['address'],
|
'from_email' => $sender['address'],
|
||||||
@@ -139,7 +139,7 @@ class MailerFactory {
|
|||||||
if (empty($replyTo['address'])) {
|
if (empty($replyTo['address'])) {
|
||||||
$replyTo['address'] = $sender['from_email'];
|
$replyTo['address'] = $sender['from_email'];
|
||||||
}
|
}
|
||||||
$replyToName = $this->encodeAddressNamePart($replyTo['name']);
|
$replyToName = $this->encodeAddressNamePart($replyTo['name'] ?? '');
|
||||||
return [
|
return [
|
||||||
'reply_to_name' => $replyToName,
|
'reply_to_name' => $replyToName,
|
||||||
'reply_to_email' => $replyTo['address'],
|
'reply_to_email' => $replyTo['address'],
|
||||||
|
@@ -27,6 +27,7 @@ use MailPoet\Entities\StatisticsNewsletterEntity;
|
|||||||
use MailPoet\Entities\SubscriberEntity;
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
use MailPoet\Entities\SubscriberSegmentEntity;
|
use MailPoet\Entities\SubscriberSegmentEntity;
|
||||||
use MailPoet\Logging\LoggerFactory;
|
use MailPoet\Logging\LoggerFactory;
|
||||||
|
use MailPoet\Mailer\MailerFactory;
|
||||||
use MailPoet\Mailer\MailerLog;
|
use MailPoet\Mailer\MailerLog;
|
||||||
use MailPoet\Models\Newsletter;
|
use MailPoet\Models\Newsletter;
|
||||||
use MailPoet\Models\NewsletterSegment;
|
use MailPoet\Models\NewsletterSegment;
|
||||||
@@ -207,7 +208,8 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$this->segmentsRepository,
|
$this->segmentsRepository,
|
||||||
$this->wp,
|
$this->wp,
|
||||||
$this->tasksLinks,
|
$this->tasksLinks,
|
||||||
$this->scheduledTasksRepository
|
$this->scheduledTasksRepository,
|
||||||
|
$this->diContainer->get(MailerTask::class)
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
$sendingQueueWorker->process();
|
$sendingQueueWorker->process();
|
||||||
@@ -236,7 +238,7 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$this->tasksLinks,
|
$this->tasksLinks,
|
||||||
$this->scheduledTasksRepository,
|
$this->scheduledTasksRepository,
|
||||||
Stub::make(
|
Stub::make(
|
||||||
new MailerTask(),
|
new MailerTask($this->diContainer->get(MailerFactory::class)),
|
||||||
[
|
[
|
||||||
'sendBulk' => $this->mailerTaskDummyResponse,
|
'sendBulk' => $this->mailerTaskDummyResponse,
|
||||||
]
|
]
|
||||||
@@ -280,7 +282,7 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$this->tasksLinks,
|
$this->tasksLinks,
|
||||||
$this->scheduledTasksRepository,
|
$this->scheduledTasksRepository,
|
||||||
Stub::make(
|
Stub::make(
|
||||||
new MailerTask(),
|
new MailerTask($this->diContainer->get(MailerFactory::class)),
|
||||||
[
|
[
|
||||||
'sendBulk' => $this->mailerTaskDummyResponse,
|
'sendBulk' => $this->mailerTaskDummyResponse,
|
||||||
]
|
]
|
||||||
@@ -321,7 +323,8 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$this->segmentsRepository,
|
$this->segmentsRepository,
|
||||||
$this->wp,
|
$this->wp,
|
||||||
$this->tasksLinks,
|
$this->tasksLinks,
|
||||||
$this->scheduledTasksRepository
|
$this->scheduledTasksRepository,
|
||||||
|
$this->diContainer->get(MailerTask::class)
|
||||||
);
|
);
|
||||||
$sendingQueueWorker->process();
|
$sendingQueueWorker->process();
|
||||||
}
|
}
|
||||||
@@ -345,8 +348,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$directUnsubscribeURL = $this->getDirectUnsubscribeURL();
|
$directUnsubscribeURL = $this->getDirectUnsubscribeURL();
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
||||||
Stub::make(
|
Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'send' => Expected::exactly(1, function($newsletter, $subscriber, $extraParams) use ($directUnsubscribeURL) {
|
'send' => Expected::exactly(1, function($newsletter, $subscriber, $extraParams) use ($directUnsubscribeURL) {
|
||||||
expect(isset($extraParams['unsubscribe_url']))->true();
|
expect(isset($extraParams['unsubscribe_url']))->true();
|
||||||
@@ -371,8 +375,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$trackedUnsubscribeURL = $this->getTrackedUnsubscribeURL();
|
$trackedUnsubscribeURL = $this->getTrackedUnsubscribeURL();
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
||||||
Stub::make(
|
Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'send' => Expected::exactly(1, function($newsletter, $subscriber, $extraParams) use ($trackedUnsubscribeURL) {
|
'send' => Expected::exactly(1, function($newsletter, $subscriber, $extraParams) use ($trackedUnsubscribeURL) {
|
||||||
expect(isset($extraParams['unsubscribe_url']))->true();
|
expect(isset($extraParams['unsubscribe_url']))->true();
|
||||||
@@ -395,8 +400,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
public function testItCanProcessSubscribersOneByOne() {
|
public function testItCanProcessSubscribersOneByOne() {
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
||||||
Stub::make(
|
Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'send' => Expected::exactly(1, function($newsletter, $subscriber, $extraParams) {
|
'send' => Expected::exactly(1, function($newsletter, $subscriber, $extraParams) {
|
||||||
// newsletter body should not be empty
|
// newsletter body should not be empty
|
||||||
@@ -513,8 +519,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
public function testItCanProcessSubscribersInBulk() {
|
public function testItCanProcessSubscribersInBulk() {
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
||||||
Stub::make(
|
Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'sendBulk' => Expected::exactly(1, function($newsletter, $subscriber) {
|
'sendBulk' => Expected::exactly(1, function($newsletter, $subscriber) {
|
||||||
// newsletter body should not be empty
|
// newsletter body should not be empty
|
||||||
@@ -562,8 +569,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
public function testItProcessesStandardNewsletters() {
|
public function testItProcessesStandardNewsletters() {
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
||||||
Stub::make(
|
Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'send' => Expected::exactly(1, function($newsletter, $subscriber) {
|
'send' => Expected::exactly(1, function($newsletter, $subscriber) {
|
||||||
// newsletter body should not be empty
|
// newsletter body should not be empty
|
||||||
@@ -618,7 +626,7 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
|
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class),
|
Stub::makeEmpty(NewslettersRepository::class),
|
||||||
Stub::makeEmpty(new MailerTask(), [], $this)
|
Stub::makeEmpty(MailerTask::class, [], $this)
|
||||||
);
|
);
|
||||||
$sendingQueueWorker->process();
|
$sendingQueueWorker->process();
|
||||||
|
|
||||||
@@ -633,8 +641,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
|
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
||||||
Stub::make(
|
Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'send' => Expected::exactly(1, function($newsletter, $subscriber) {
|
'send' => Expected::exactly(1, function($newsletter, $subscriber) {
|
||||||
// newsletter body should not be empty
|
// newsletter body should not be empty
|
||||||
@@ -685,8 +694,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
|
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class),
|
Stub::makeEmpty(NewslettersRepository::class),
|
||||||
Stub::make(
|
Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'send' => Expected::exactly(0),
|
'send' => Expected::exactly(0),
|
||||||
],
|
],
|
||||||
@@ -718,8 +728,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
]);
|
]);
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
null,
|
null,
|
||||||
Stub::make(
|
Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'send' => Expected::exactly(1, function() {
|
'send' => Expected::exactly(1, function() {
|
||||||
return $this->mailerTaskDummyResponse;
|
return $this->mailerTaskDummyResponse;
|
||||||
@@ -754,8 +765,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$queue->countTotal = 2;
|
$queue->countTotal = 2;
|
||||||
$queue->save();
|
$queue->save();
|
||||||
$sendingQueueWorker = $this->sendingQueueWorker;
|
$sendingQueueWorker = $this->sendingQueueWorker;
|
||||||
$sendingQueueWorker->mailerTask = Stub::make(
|
$sendingQueueWorker->mailerTask = Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'send' => Expected::exactly(1, function() {
|
'send' => Expected::exactly(1, function() {
|
||||||
return $this->mailerTaskDummyResponse;
|
return $this->mailerTaskDummyResponse;
|
||||||
@@ -793,8 +805,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$queue->countTotal = count($subscribers);
|
$queue->countTotal = count($subscribers);
|
||||||
$queue->save();
|
$queue->save();
|
||||||
$sendingQueueWorker = $this->sendingQueueWorker;
|
$sendingQueueWorker = $this->sendingQueueWorker;
|
||||||
$sendingQueueWorker->mailerTask = Stub::make(
|
$sendingQueueWorker->mailerTask = Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'send' => Expected::exactly(1, function() {
|
'send' => Expected::exactly(1, function() {
|
||||||
return $this->mailerTaskDummyResponse;
|
return $this->mailerTaskDummyResponse;
|
||||||
@@ -826,9 +839,11 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$queue->countTotal = 2;
|
$queue->countTotal = 2;
|
||||||
$queue->save();
|
$queue->save();
|
||||||
$sendingQueueWorker = $this->sendingQueueWorker;
|
$sendingQueueWorker = $this->sendingQueueWorker;
|
||||||
$sendingQueueWorker->mailerTask = Stub::make(
|
$sendingQueueWorker->mailerTask = Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
['send' => $this->mailerTaskDummyResponse]
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
|
['send' => $this->mailerTaskDummyResponse],
|
||||||
|
$this
|
||||||
);
|
);
|
||||||
$sendingQueueWorker->process();
|
$sendingQueueWorker->process();
|
||||||
|
|
||||||
@@ -847,9 +862,11 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testItDoesNotSendToTrashedSubscribers() {
|
public function testItDoesNotSendToTrashedSubscribers() {
|
||||||
$sendingQueueWorker = $this->sendingQueueWorker;
|
$sendingQueueWorker = $this->sendingQueueWorker;
|
||||||
$sendingQueueWorker->mailerTask = Stub::make(
|
$sendingQueueWorker->mailerTask = Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
['send' => $this->mailerTaskDummyResponse]
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
|
['send' => $this->mailerTaskDummyResponse],
|
||||||
|
$this
|
||||||
);
|
);
|
||||||
|
|
||||||
// newsletter is sent to existing subscriber
|
// newsletter is sent to existing subscriber
|
||||||
@@ -875,9 +892,11 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testItDoesNotSendToGloballyUnsubscribedSubscribers() {
|
public function testItDoesNotSendToGloballyUnsubscribedSubscribers() {
|
||||||
$sendingQueueWorker = $this->sendingQueueWorker;
|
$sendingQueueWorker = $this->sendingQueueWorker;
|
||||||
$sendingQueueWorker->mailerTask = Stub::make(
|
$sendingQueueWorker->mailerTask = Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
['send' => $this->mailerTaskDummyResponse]
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
|
['send' => $this->mailerTaskDummyResponse],
|
||||||
|
$this
|
||||||
);
|
);
|
||||||
|
|
||||||
// newsletter is not sent to globally unsubscribed subscriber
|
// newsletter is not sent to globally unsubscribed subscriber
|
||||||
@@ -893,9 +912,11 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testItDoesNotSendToSubscribersUnsubscribedFromSegments() {
|
public function testItDoesNotSendToSubscribersUnsubscribedFromSegments() {
|
||||||
$sendingQueueWorker = $this->sendingQueueWorker;
|
$sendingQueueWorker = $this->sendingQueueWorker;
|
||||||
$sendingQueueWorker->mailerTask = Stub::make(
|
$sendingQueueWorker->mailerTask = Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
['send' => $this->mailerTaskDummyResponse]
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
|
['send' => $this->mailerTaskDummyResponse],
|
||||||
|
$this
|
||||||
);
|
);
|
||||||
|
|
||||||
// newsletter is not sent to subscriber unsubscribed from segment
|
// newsletter is not sent to subscriber unsubscribed from segment
|
||||||
@@ -911,9 +932,11 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testItDoesNotSendToInactiveSubscribers() {
|
public function testItDoesNotSendToInactiveSubscribers() {
|
||||||
$sendingQueueWorker = $this->sendingQueueWorker;
|
$sendingQueueWorker = $this->sendingQueueWorker;
|
||||||
$sendingQueueWorker->mailerTask = Stub::make(
|
$sendingQueueWorker->mailerTask = Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
['send' => $this->mailerTaskDummyResponse]
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
|
['send' => $this->mailerTaskDummyResponse],
|
||||||
|
$this
|
||||||
);
|
);
|
||||||
|
|
||||||
// newsletter is not sent to inactive subscriber
|
// newsletter is not sent to inactive subscriber
|
||||||
@@ -951,11 +974,13 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$this->wp,
|
$this->wp,
|
||||||
$this->tasksLinks,
|
$this->tasksLinks,
|
||||||
$this->scheduledTasksRepository,
|
$this->scheduledTasksRepository,
|
||||||
Stub::make(
|
Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'sendBulk' => $this->mailerTaskDummyResponse,
|
'sendBulk' => $this->mailerTaskDummyResponse,
|
||||||
]
|
],
|
||||||
|
$this
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
@@ -984,8 +1009,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
public function testItDoesNotUpdateNewsletterHashDuringSending() {
|
public function testItDoesNotUpdateNewsletterHashDuringSending() {
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
||||||
Stub::make(
|
Stub::construct(
|
||||||
new MailerTask(),
|
MailerTask::class,
|
||||||
|
[$this->diContainer->get(MailerFactory::class)],
|
||||||
[
|
[
|
||||||
'send' => Expected::once($this->mailerTaskDummyResponse),
|
'send' => Expected::once($this->mailerTaskDummyResponse),
|
||||||
],
|
],
|
||||||
@@ -1022,9 +1048,9 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
|
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
||||||
$this->make(new MailerTask(), [
|
Stub::construct(MailerTask::class, [$this->diContainer->get(MailerFactory::class)], [
|
||||||
'send' => $this->mailerTaskDummyResponse,
|
'send' => $this->mailerTaskDummyResponse,
|
||||||
])
|
], $this)
|
||||||
);
|
);
|
||||||
$sendingQueueWorker->process();
|
$sendingQueueWorker->process();
|
||||||
|
|
||||||
@@ -1043,7 +1069,7 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
|
|
||||||
$sendingQueueWorker = $this->getSendingQueueWorker(
|
$sendingQueueWorker = $this->getSendingQueueWorker(
|
||||||
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
|
||||||
$this->make(new MailerTask(), [
|
$this->construct(MailerTask::class, [$this->diContainer->get(MailerFactory::class)], [
|
||||||
'send' => $this->mailerTaskDummyResponse,
|
'send' => $this->mailerTaskDummyResponse,
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
@@ -1168,7 +1194,7 @@ class SendingQueueTest extends \MailPoetTest {
|
|||||||
$this->wp,
|
$this->wp,
|
||||||
$this->tasksLinks,
|
$this->tasksLinks,
|
||||||
$this->scheduledTasksRepository,
|
$this->scheduledTasksRepository,
|
||||||
$mailerMock
|
$mailerMock ?? $this->diContainer->get(MailerTask::class)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,15 +7,13 @@ use Codeception\Stub\Expected;
|
|||||||
use MailPoet\Config\Populator;
|
use MailPoet\Config\Populator;
|
||||||
use MailPoet\Cron\Workers\SendingQueue\Tasks\Mailer as MailerTask;
|
use MailPoet\Cron\Workers\SendingQueue\Tasks\Mailer as MailerTask;
|
||||||
use MailPoet\Mailer\Mailer;
|
use MailPoet\Mailer\Mailer;
|
||||||
|
use MailPoet\Mailer\MailerFactory;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\Settings\SettingsRepository;
|
use MailPoet\Settings\SettingsRepository;
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
use MailPoetVendor\Idiorm\ORM;
|
||||||
|
|
||||||
class MailerTest extends \MailPoetTest {
|
class MailerTest extends \MailPoetTest {
|
||||||
/** @var MailerTask */
|
|
||||||
public $mailerTask;
|
|
||||||
public $sender;
|
|
||||||
/** @var SettingsController */
|
/** @var SettingsController */
|
||||||
private $settings;
|
private $settings;
|
||||||
|
|
||||||
@@ -26,47 +24,52 @@ class MailerTest extends \MailPoetTest {
|
|||||||
$this->settings = $this->diContainer->get(SettingsController::class);
|
$this->settings = $this->diContainer->get(SettingsController::class);
|
||||||
$populator = $this->diContainer->get(Populator::class);
|
$populator = $this->diContainer->get(Populator::class);
|
||||||
$populator->up();
|
$populator->up();
|
||||||
$this->mailerTask = new MailerTask();
|
|
||||||
$this->sender = $this->settings->get('sender');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConfiguresMailerWhenItConstructs() {
|
public function testConfiguresMailerWhenItConstructs() {
|
||||||
expect($this->mailerTask->mailer instanceof \MailPoet\Mailer\Mailer)->true();
|
$mailerFactoryMock = $this->createMock(MailerFactory::class);
|
||||||
|
$mailerFactoryMock->expects($this->once())
|
||||||
|
->method('buildMailer')
|
||||||
|
->willReturn($this->createMock(Mailer::class));
|
||||||
|
new MailerTask($mailerFactoryMock);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItCanConfigureMailerWithSenderAndReplyToAddresses() {
|
public function testItCanConfigureMailerWithSenderAndReplyToAddressesFromEmail() {
|
||||||
$newsletter = new \stdClass();
|
$newsletter = new \stdClass();
|
||||||
|
|
||||||
// when no sender/reply-to information is set, use the sender information
|
|
||||||
// from Settings
|
|
||||||
$mailer = $this->mailerTask->configureMailer($newsletter);
|
|
||||||
expect($mailer->sender['from_name'])->equals($this->sender['name']);
|
|
||||||
expect($mailer->sender['from_email'])->equals($this->sender['address']);
|
|
||||||
expect($mailer->replyTo['reply_to_name'])->equals($this->sender['name']);
|
|
||||||
expect($mailer->replyTo['reply_to_email'])->equals($this->sender['address']);
|
|
||||||
$newsletter->senderName = 'Sender';
|
$newsletter->senderName = 'Sender';
|
||||||
$newsletter->senderAddress = 'from@example.com';
|
$newsletter->senderAddress = 'from@example.com';
|
||||||
$newsletter->replyToName = 'Reply-to';
|
$newsletter->replyToName = 'Reply-to';
|
||||||
$newsletter->replyToAddress = 'reply-to@example.com';
|
$newsletter->replyToAddress = 'reply-to@example.com';
|
||||||
|
|
||||||
// when newsletter's sender/reply-to information is available, use that
|
$mailerFactoryMock = $this->createMock(MailerFactory::class);
|
||||||
// to configure mailer
|
// First call in constructor
|
||||||
$mailer = $this->mailerTask->configureMailer($newsletter);
|
$mailerFactoryMock->expects($this->at(0))
|
||||||
expect($mailer->sender['from_name'])->equals($newsletter->senderName);
|
->method('buildMailer')
|
||||||
expect($mailer->sender['from_email'])->equals($newsletter->senderAddress);
|
->willReturn($this->createMock(Mailer::class));
|
||||||
expect($mailer->replyTo['reply_to_name'])->equals($newsletter->replyToName);
|
// Second call in custom mailer configuration should be called with sender and reply to from newsletter
|
||||||
expect($mailer->replyTo['reply_to_email'])->equals($newsletter->replyToAddress);
|
$mailerFactoryMock->expects($this->at(1))
|
||||||
|
->method('buildMailer')
|
||||||
|
->with(
|
||||||
|
null,
|
||||||
|
['name' => 'Sender', 'address' => 'from@example.com'],
|
||||||
|
['name' => 'Reply-to', 'address' => 'reply-to@example.com']
|
||||||
|
)
|
||||||
|
->willReturn($this->createMock(Mailer::class));
|
||||||
|
$mailerTask = new MailerTask($mailerFactoryMock);
|
||||||
|
$mailerTask->configureMailer($newsletter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItGetsMailerLog() {
|
public function testItGetsMailerLog() {
|
||||||
$mailerLog = $this->mailerTask->getMailerLog();
|
$mailerTask = $this->diContainer->get(MailerTask::class);
|
||||||
|
$mailerLog = $mailerTask->getMailerLog();
|
||||||
expect(is_array($mailerLog))->true();
|
expect(is_array($mailerLog))->true();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItUpdatesMailerLogSentCount() {
|
public function testItUpdatesMailerLogSentCount() {
|
||||||
$mailerLog = $this->mailerTask->getMailerLog();
|
$mailerTask = $this->diContainer->get(MailerTask::class);
|
||||||
|
$mailerLog = $mailerTask->getMailerLog();
|
||||||
expect(array_sum($mailerLog['sent']))->equals(0);
|
expect(array_sum($mailerLog['sent']))->equals(0);
|
||||||
$mailerLog = $this->mailerTask->updateSentCount();
|
$mailerLog = $mailerTask->updateSentCount();
|
||||||
expect(array_sum($mailerLog['sent']))->equals(1);
|
expect(array_sum($mailerLog['sent']))->equals(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +82,7 @@ class MailerTest extends \MailPoetTest {
|
|||||||
'mailpoet_api_key' => 'some_key',
|
'mailpoet_api_key' => 'some_key',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
$mailerTask = new MailerTask();
|
$mailerTask = new MailerTask($this->diContainer->get(MailerFactory::class));
|
||||||
expect($mailerTask->getProcessingMethod())->equals('bulk');
|
expect($mailerTask->getProcessingMethod())->equals('bulk');
|
||||||
|
|
||||||
// when using other methods, newsletters should be processed individually
|
// when using other methods, newsletters should be processed individually
|
||||||
@@ -89,7 +92,7 @@ class MailerTest extends \MailPoetTest {
|
|||||||
'method' => 'PHPMail',
|
'method' => 'PHPMail',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
$mailerTask = new MailerTask();
|
$mailerTask = new MailerTask($this->diContainer->get(MailerFactory::class));
|
||||||
expect($mailerTask->getProcessingMethod())->equals('individual');
|
expect($mailerTask->getProcessingMethod())->equals('individual');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,36 +102,28 @@ class MailerTest extends \MailPoetTest {
|
|||||||
$subscriber->firstName = 'John';
|
$subscriber->firstName = 'John';
|
||||||
$subscriber->lastName = 'Doe';
|
$subscriber->lastName = 'Doe';
|
||||||
$subscriber->save();
|
$subscriber->save();
|
||||||
$preparedSubscriber = $this->mailerTask->prepareSubscriberForSending($subscriber);
|
$mailerTask = $this->diContainer->get(MailerTask::class);
|
||||||
|
$preparedSubscriber = $mailerTask->prepareSubscriberForSending($subscriber);
|
||||||
expect($preparedSubscriber)->equals('John Doe <test@example.com>');
|
expect($preparedSubscriber)->equals('John Doe <test@example.com>');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItCanSend() {
|
public function testItCanSend() {
|
||||||
$phpMailClass = 'MailPoet\Mailer\Methods\PHPMail';
|
$phpMailClass = 'MailPoet\Mailer\Methods\PHPMail';
|
||||||
$this->settings->set(
|
$mailerMock = Stub::makeEmpty(Mailer::class, [
|
||||||
Mailer::MAILER_CONFIG_SETTING_NAME,
|
'mailerInstance' => Stub::make(
|
||||||
[
|
$phpMailClass,
|
||||||
'method' => 'PHPMail',
|
['send' => Expected::exactly(1, function() {
|
||||||
]
|
return ['response' => true];
|
||||||
);
|
})],
|
||||||
|
$this
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
$mailerFactoryMock = $this->createMock(MailerFactory::class);
|
||||||
|
$mailerFactoryMock->expects($this->once())
|
||||||
|
->method('buildMailer')
|
||||||
|
->willReturn($mailerMock);
|
||||||
// mock mailer instance and ensure that send method is invoked
|
// mock mailer instance and ensure that send method is invoked
|
||||||
$mailerTask = new MailerTask(
|
$mailerTask = new MailerTask($mailerFactoryMock);
|
||||||
(object)[
|
|
||||||
'mailerInstance' => Stub::make(
|
|
||||||
$phpMailClass,
|
|
||||||
['send' => Expected::exactly(1, function() {
|
|
||||||
return ['response' => true];
|
|
||||||
})],
|
|
||||||
$this
|
|
||||||
),
|
|
||||||
'mailerConfig' => [
|
|
||||||
'method' => null,
|
|
||||||
],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
// mailer instance should be properly configured
|
|
||||||
expect($mailerTask->mailer->mailerInstance instanceof $phpMailClass)
|
|
||||||
->true();
|
|
||||||
// send method should return true
|
// send method should return true
|
||||||
expect($mailerTask->send('Newsletter', 'Subscriber'))->equals(['response' => true]);
|
expect($mailerTask->send('Newsletter', 'Subscriber'))->equals(['response' => true]);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user