Refactor creation of sending queue worker in test

[MAILPOET-3418]
This commit is contained in:
Rostislav Wolny
2021-03-26 14:21:38 +01:00
committed by Veljko V
parent 1c4bb7d22e
commit de210991a4

View File

@@ -112,14 +112,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->statsNotificationsWorker = Stub::makeEmpty(StatsNotificationsScheduler::class); $this->statsNotificationsWorker = Stub::makeEmpty(StatsNotificationsScheduler::class);
$this->loggerFactory = LoggerFactory::getInstance(); $this->loggerFactory = LoggerFactory::getInstance();
$this->cronHelper = $this->diContainer->get(CronHelper::class); $this->cronHelper = $this->diContainer->get(CronHelper::class);
$this->sendingQueueWorker = new SendingQueueWorker( $this->sendingQueueWorker = $this->getSendingQueueWorker(Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]));
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cronHelper,
$this->subscribersFinder
);
$this->newslettersRepository = ContainerWrapper::getInstance()->get(NewslettersRepository::class); $this->newslettersRepository = ContainerWrapper::getInstance()->get(NewslettersRepository::class);
} }
@@ -150,15 +143,8 @@ class SendingQueueTest extends \MailPoetTest {
} }
public function testItEnforcesExecutionLimitsBeforeQueueProcessing() { public function testItEnforcesExecutionLimitsBeforeQueueProcessing() {
$sendingQueueWorker = Stub::make( $sendingQueueWorker = Stub::make($this->getSendingQueueWorker(
new SendingQueueWorker( Stub::makeEmpty(NewslettersRepository::class)),
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cronHelper,
$this->subscribersFinder
),
[ [
'processQueue' => Expected::never(), 'processQueue' => Expected::never(),
'enforceSendingAndExecutionLimits' => Expected::exactly(1, function() { 'enforceSendingAndExecutionLimits' => Expected::exactly(1, function() {
@@ -183,14 +169,7 @@ class SendingQueueTest extends \MailPoetTest {
public function testItEnforcesExecutionLimitsAfterSendingWhenQueueStatusIsNotSetToComplete() { public function testItEnforcesExecutionLimitsAfterSendingWhenQueueStatusIsNotSetToComplete() {
$sendingQueueWorker = Stub::make( $sendingQueueWorker = Stub::make(
new SendingQueueWorker( $this->getSendingQueueWorker(Stub::makeEmpty(NewslettersRepository::class)),
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cronHelper,
$this->subscribersFinder
),
[ [
'enforceSendingAndExecutionLimits' => Expected::exactly(1), 'enforceSendingAndExecutionLimits' => Expected::exactly(1),
], $this); ], $this);
@@ -229,14 +208,7 @@ class SendingQueueTest extends \MailPoetTest {
$queue = $this->queue; $queue = $this->queue;
$queue->status = SendingQueue::STATUS_COMPLETED; $queue->status = SendingQueue::STATUS_COMPLETED;
$sendingQueueWorker = Stub::make( $sendingQueueWorker = Stub::make(
new SendingQueueWorker( $this->getSendingQueueWorker(Stub::makeEmpty(NewslettersRepository::class)),
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cronHelper,
$this->subscribersFinder
),
[ [
'enforceSendingAndExecutionLimits' => Expected::never(), 'enforceSendingAndExecutionLimits' => Expected::never(),
], $this); ], $this);
@@ -270,14 +242,7 @@ class SendingQueueTest extends \MailPoetTest {
public function testItEnforcesExecutionLimitsAfterQueueProcessing() { public function testItEnforcesExecutionLimitsAfterQueueProcessing() {
$sendingQueueWorker = Stub::make( $sendingQueueWorker = Stub::make(
new SendingQueueWorker( $this->getSendingQueueWorker(Stub::makeEmpty(NewslettersRepository::class)),
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cronHelper,
$this->subscribersFinder
),
[ [
'processQueue' => function() { 'processQueue' => function() {
// this function returns a queue object // this function returns a queue object
@@ -313,13 +278,8 @@ class SendingQueueTest extends \MailPoetTest {
public function testItPassesExtraParametersToMailerWhenTrackingIsDisabled() { public function testItPassesExtraParametersToMailerWhenTrackingIsDisabled() {
$this->settings->set('tracking.enabled', false); $this->settings->set('tracking.enabled', false);
$directUnsubscribeURL = $this->getDirectUnsubscribeURL(); $directUnsubscribeURL = $this->getDirectUnsubscribeURL();
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]), Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cronHelper,
$this->subscribersFinder,
Stub::make( Stub::make(
new MailerTask(), new MailerTask(),
[ [
@@ -344,13 +304,8 @@ class SendingQueueTest extends \MailPoetTest {
public function testItPassesExtraParametersToMailerWhenTrackingIsEnabled() { public function testItPassesExtraParametersToMailerWhenTrackingIsEnabled() {
$this->settings->set('tracking.enabled', true); $this->settings->set('tracking.enabled', true);
$trackedUnsubscribeURL = $this->getTrackedUnsubscribeURL(); $trackedUnsubscribeURL = $this->getTrackedUnsubscribeURL();
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]), Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cronHelper,
$this->subscribersFinder,
Stub::make( Stub::make(
new MailerTask(), new MailerTask(),
[ [
@@ -373,13 +328,8 @@ class SendingQueueTest extends \MailPoetTest {
} }
public function testItCanProcessSubscribersOneByOne() { public function testItCanProcessSubscribersOneByOne() {
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]), Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cronHelper,
$this->subscribersFinder,
Stub::make( Stub::make(
new MailerTask(), new MailerTask(),
[ [
@@ -424,13 +374,8 @@ class SendingQueueTest extends \MailPoetTest {
} }
public function testItCanProcessSubscribersInBulk() { public function testItCanProcessSubscribersInBulk() {
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]), Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cronHelper,
$this->subscribersFinder,
Stub::make( Stub::make(
new MailerTask(), new MailerTask(),
[ [
@@ -478,13 +423,8 @@ class SendingQueueTest extends \MailPoetTest {
} }
public function testItProcessesStandardNewsletters() { public function testItProcessesStandardNewsletters() {
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]), Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cronHelper,
$this->subscribersFinder,
Stub::make( Stub::make(
new MailerTask(), new MailerTask(),
[ [
@@ -539,13 +479,8 @@ class SendingQueueTest extends \MailPoetTest {
$this->newsletter->type = Newsletter::TYPE_WELCOME; $this->newsletter->type = Newsletter::TYPE_WELCOME;
$this->newsletterSegment->delete(); $this->newsletterSegment->delete();
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class), Stub::makeEmpty(NewslettersRepository::class),
$this->cronHelper,
$this->subscribersFinder,
Stub::makeEmpty(new MailerTask(), [], $this) Stub::makeEmpty(new MailerTask(), [], $this)
); );
$sendingQueueWorker->process(); $sendingQueueWorker->process();
@@ -559,13 +494,8 @@ class SendingQueueTest extends \MailPoetTest {
$this->newsletter->type = Newsletter::TYPE_WELCOME; $this->newsletter->type = Newsletter::TYPE_WELCOME;
$this->newsletterSegment->delete(); $this->newsletterSegment->delete();
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]), Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cronHelper,
$this->subscribersFinder,
Stub::make( Stub::make(
new MailerTask(), new MailerTask(),
[ [
@@ -579,6 +509,7 @@ class SendingQueueTest extends \MailPoetTest {
$this $this
) )
); );
$sendingQueueWorker->process(); $sendingQueueWorker->process();
// newsletter status is set to sent // newsletter status is set to sent
@@ -615,13 +546,8 @@ class SendingQueueTest extends \MailPoetTest {
$this->subscriber->save(); $this->subscriber->save();
$this->newsletterSegment->delete(); $this->newsletterSegment->delete();
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class), Stub::makeEmpty(NewslettersRepository::class),
$this->cronHelper,
$this->subscribersFinder,
Stub::make( Stub::make(
new MailerTask(), new MailerTask(),
[ [
@@ -799,14 +725,9 @@ class SendingQueueTest extends \MailPoetTest {
->method('__get') ->method('__get')
->with('id') ->with('id')
->will($this->returnValue(100)); ->will($this->returnValue(100));
$sendingQueueWorker = Stub::make(new SendingQueueWorker( $sendingQueueWorker = Stub::make(
$this->sendingErrorHandler, $this->getSendingQueueWorker(Stub::makeEmpty(NewslettersRepository::class))
$this->statsNotificationsWorker, );
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cronHelper,
$this->subscribersFinder
));
$sendingQueueWorker->__construct( $sendingQueueWorker->__construct(
$this->sendingErrorHandler, $this->sendingErrorHandler,
$this->statsNotificationsWorker, $this->statsNotificationsWorker,
@@ -845,13 +766,8 @@ class SendingQueueTest extends \MailPoetTest {
} }
public function testItDoesNotUpdateNewsletterHashDuringSending() { public function testItDoesNotUpdateNewsletterHashDuringSending() {
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]), Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cronHelper,
$this->subscribersFinder,
Stub::make( Stub::make(
new MailerTask(), new MailerTask(),
[ [
@@ -876,14 +792,7 @@ class SendingQueueTest extends \MailPoetTest {
}; };
$wp = new WPFunctions; $wp = new WPFunctions;
$wp->addFilter('mailpoet_cron_worker_sending_queue_batch_size', $filter); $wp->addFilter('mailpoet_cron_worker_sending_queue_batch_size', $filter);
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(Stub::makeEmpty(NewslettersRepository::class));
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cronHelper,
$this->subscribersFinder
);
expect($sendingQueueWorker->batchSize)->equals($customBatchSizeValue); expect($sendingQueueWorker->batchSize)->equals($customBatchSizeValue);
$wp->removeFilter('mailpoet_cron_worker_sending_queue_batch_size', $filter); $wp->removeFilter('mailpoet_cron_worker_sending_queue_batch_size', $filter);
} }
@@ -895,13 +804,8 @@ class SendingQueueTest extends \MailPoetTest {
'scheduled_at' => Carbon::createFromTimestamp(WPFunctions::get()->currentTime('timestamp'))->addMonths(1), 'scheduled_at' => Carbon::createFromTimestamp(WPFunctions::get()->currentTime('timestamp'))->addMonths(1),
]); ]);
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]), Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cronHelper,
$this->subscribersFinder,
$this->make(new MailerTask(), [ $this->make(new MailerTask(), [
'send' => $this->mailerTaskDummyResponse, 'send' => $this->mailerTaskDummyResponse,
]) ])
@@ -921,13 +825,8 @@ class SendingQueueTest extends \MailPoetTest {
'scheduled_at' => $inOneHour, 'scheduled_at' => $inOneHour,
]); ]);
$sendingQueueWorker = new SendingQueueWorker( $sendingQueueWorker = $this->getSendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]), Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cronHelper,
$this->subscribersFinder,
$this->make(new MailerTask(), [ $this->make(new MailerTask(), [
'send' => $this->mailerTaskDummyResponse, 'send' => $this->mailerTaskDummyResponse,
]) ])
@@ -953,4 +852,16 @@ class SendingQueueTest extends \MailPoetTest {
ORM::raw_execute('TRUNCATE ' . NewsletterSegment::$_table); ORM::raw_execute('TRUNCATE ' . NewsletterSegment::$_table);
ORM::raw_execute('TRUNCATE ' . StatisticsNewsletters::$_table); ORM::raw_execute('TRUNCATE ' . StatisticsNewsletters::$_table);
} }
private function getSendingQueueWorker($newsletterRepositoryMock = null, $mailerMock = null) {
return new SendingQueueWorker(
$this->sendingErrorHandler,
$this->statsNotificationsWorker,
$this->loggerFactory,
$newsletterRepositoryMock ?: $this->newslettersRepository,
$this->cronHelper,
$this->subscribersFinder,
$mailerMock
);
}
} }