- Updates Sending Queue Worker and Mailer task to allow dependency
injection via constructor - Updates unit tests to use dependency injection instead of modifying object's internals
This commit is contained in:
@ -17,9 +17,9 @@ class SendingQueue {
|
||||
public $newsletter_task;
|
||||
public $timer;
|
||||
|
||||
function __construct($timer = false) {
|
||||
$this->mailer_task = new MailerTask();
|
||||
$this->newsletter_task = new NewsletterTask();
|
||||
function __construct($timer = false, $mailer_task = false, $newsletter_task = false) {
|
||||
$this->mailer_task = ($mailer_task) ? $mailer_task : new MailerTask();
|
||||
$this->newsletter_task = ($newsletter_task) ? $newsletter_task : new NewsletterTask();
|
||||
$this->timer = ($timer) ? $timer : microtime(true);
|
||||
// abort if execution or sending limit are reached
|
||||
CronHelper::enforceExecutionLimit($this->timer);
|
||||
|
@ -9,8 +9,8 @@ if(!defined('ABSPATH')) exit;
|
||||
class Mailer {
|
||||
public $mailer;
|
||||
|
||||
function __construct() {
|
||||
$this->mailer = $this->configureMailer();
|
||||
function __construct($mailer = false) {
|
||||
$this->mailer = ($mailer) ? $mailer : $this->configureMailer();
|
||||
}
|
||||
|
||||
function configureMailer($newsletter = null) {
|
||||
|
@ -93,10 +93,12 @@ class SendingQueueTest extends MailPoetTest {
|
||||
}
|
||||
|
||||
function testItCanProcessSubscribersOneByOne() {
|
||||
$sending_queue_worker = $this->sending_queue_worker;
|
||||
$sending_queue_worker->mailer_task = Stub::make(
|
||||
$sending_queue_worker = new SendingQueueWorker(
|
||||
$timer = false,
|
||||
Stub::make(
|
||||
new MailerTask(),
|
||||
array('send' => Stub::exactly(1, function($newsletter, $subscriber) { return true; }))
|
||||
)
|
||||
);
|
||||
$sending_queue_worker->process();
|
||||
|
||||
@ -131,13 +133,15 @@ class SendingQueueTest extends MailPoetTest {
|
||||
}
|
||||
|
||||
function testItCanProcessSubscribersInBulk() {
|
||||
$sending_queue_worker = $this->sending_queue_worker;
|
||||
$sending_queue_worker->mailer_task = Stub::make(
|
||||
$sending_queue_worker = new SendingQueueWorker(
|
||||
$timer = false,
|
||||
Stub::make(
|
||||
new MailerTask(),
|
||||
array(
|
||||
'send' => Stub::exactly(1, function($newsletter, $subscriber) { return true; }),
|
||||
'getProcessingMethod' => Stub::exactly(1, function() { return 'bulk'; })
|
||||
)
|
||||
)
|
||||
);
|
||||
$sending_queue_worker->process();
|
||||
|
||||
@ -250,10 +254,12 @@ class SendingQueueTest extends MailPoetTest {
|
||||
}
|
||||
|
||||
function testItUpdatesFailedListWhenSendingFailed() {
|
||||
$sending_queue_worker = $this->sending_queue_worker;
|
||||
$sending_queue_worker->mailer_task = Stub::make(
|
||||
$sending_queue_worker = new SendingQueueWorker(
|
||||
$timer = false,
|
||||
Stub::make(
|
||||
new MailerTask(),
|
||||
array('send' => Stub::exactly(1, function($newsletter, $subscriber) { return false; }))
|
||||
)
|
||||
);
|
||||
$sending_queue_worker->process();
|
||||
|
||||
|
@ -3,7 +3,6 @@ use Codeception\Util\Stub;
|
||||
use MailPoet\Config\Populator;
|
||||
use MailPoet\Cron\Workers\SendingQueue\Tasks\Mailer as MailerTask;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
use MailPoet\Mailer\Methods\PHPMail;
|
||||
use MailPoet\Models\Setting;
|
||||
use MailPoet\Models\Subscriber;
|
||||
|
||||
@ -104,11 +103,14 @@ class MailerTaskTest extends MailPoetTest {
|
||||
'method' => 'PHPMail'
|
||||
)
|
||||
);
|
||||
$mailer_task = new MailerTask();
|
||||
// mock mailer instance and ensure that send method is invoked
|
||||
$mailer_task->mailer->mailer_instance = Stub::make(
|
||||
$mailer_task = new MailerTask(
|
||||
(object)array(
|
||||
'mailer_instance' => Stub::make(
|
||||
$php_mail_class,
|
||||
array('send' => Stub::exactly(1, function($newsletter, $subscriber) { return true; }))
|
||||
)
|
||||
)
|
||||
);
|
||||
// mailer instance should be properly configured
|
||||
expect($mailer_task->mailer->mailer_instance instanceof $php_mail_class)
|
||||
|
Reference in New Issue
Block a user