Add new unit tests [MAILPOET-903]
This commit is contained in:
189
tests/unit/Cron/Workers/SendingQueue/MigrationTest.php
Normal file
189
tests/unit/Cron/Workers/SendingQueue/MigrationTest.php
Normal file
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\Cron\Workers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use MailPoet\Cron\Workers\SendingQueue\Migration;
|
||||
use MailPoet\Mailer\MailerLog;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
use MailPoet\Models\ScheduledTaskSubscriber;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Models\Setting;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Tasks\Sending as SendingTask;
|
||||
|
||||
class MigrationTest extends \MailPoetTest {
|
||||
function _before() {
|
||||
// Alter table to test migration
|
||||
if(!Migration::checkUnmigratedColumnsExist()) {
|
||||
$this->downgradeTable();
|
||||
$this->altered = true;
|
||||
}
|
||||
|
||||
$this->subscriber_to_process = Subscriber::createOrUpdate(array(
|
||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
||||
'email' => 'to_process@example.com'
|
||||
));
|
||||
$this->subscriber_processed = Subscriber::createOrUpdate(array(
|
||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
||||
'email' => 'processed@example.com'
|
||||
));
|
||||
|
||||
// subscribers should be migrated
|
||||
$this->queue_running = $this->createSendingQueue();
|
||||
$this->queue_paused = $this->createSendingQueue(SendingQueue::STATUS_PAUSED);
|
||||
|
||||
// subscribers should not be migrated
|
||||
$this->queue_completed = $this->createSendingQueue(SendingQueue::STATUS_COMPLETED);
|
||||
$this->queue_scheduled = $this->createSendingQueue(SendingQueue::STATUS_SCHEDULED);
|
||||
|
||||
$this->worker = new Migration(microtime(true));
|
||||
}
|
||||
|
||||
function testItDefinesConstants() {
|
||||
expect(Migration::BATCH_SIZE)->equals(20);
|
||||
}
|
||||
|
||||
function testItChecksForACompletedMigrationBeforeRunning() {
|
||||
expect($this->worker->checkProcessingRequirements())->true();
|
||||
$this->createCompletedTask();
|
||||
expect($this->worker->checkProcessingRequirements())->false();
|
||||
}
|
||||
|
||||
function testItPausesSendingWhenPreparingATask() {
|
||||
$task = $this->createScheduledTask();
|
||||
expect(MailerLog::isSendingPaused())->false();
|
||||
$this->worker->prepareTask($task);
|
||||
expect($task->status)->null();
|
||||
expect(MailerLog::isSendingPaused())->true();
|
||||
}
|
||||
|
||||
function testItResumesSendingIfThereIsNothingToMigrate() {
|
||||
SendingQueue::deleteMany();
|
||||
$this->worker->pauseSending();
|
||||
expect(MailerLog::isSendingPaused())->true();
|
||||
$task = $this->createScheduledTask();
|
||||
$result = $this->worker->prepareTask($task);
|
||||
expect($result)->false();
|
||||
expect(MailerLog::isSendingPaused())->false();
|
||||
}
|
||||
|
||||
function testItCompletesTaskIfThereIsNothingToMigrate() {
|
||||
SendingQueue::deleteMany();
|
||||
$task = $this->createScheduledTask();
|
||||
$result = $this->worker->prepareTask($task);
|
||||
expect(ScheduledTask::findOne($task->id)->status)->equals(ScheduledTask::STATUS_COMPLETED);
|
||||
expect($result)->false();
|
||||
}
|
||||
|
||||
function testItMigratesSendingQueuesAndSubscribers() {
|
||||
expect($this->worker->getUnmigratedQueues()->count())->equals(4);
|
||||
expect(ScheduledTask::where('type', SendingTask::TASK_TYPE)->findMany())->count(0);
|
||||
expect(ScheduledTaskSubscriber::whereGt('task_id', 0)->count())->equals(0);
|
||||
|
||||
$task = $this->createRunningTask();
|
||||
$this->worker->processTask($task);
|
||||
|
||||
expect($this->worker->getUnmigratedQueues()->count())->equals(0);
|
||||
expect(ScheduledTask::where('type', SendingTask::TASK_TYPE)->findMany())->count(4);
|
||||
expect(ScheduledTaskSubscriber::whereGt('task_id', 0)->count())->equals(4); // 2 for running, 2 for paused
|
||||
|
||||
$queue = SendingQueue::findOne($this->queue_running->id);
|
||||
$task = ScheduledTask::findOne($queue->task_id);
|
||||
expect($task->type)->equals(SendingTask::TASK_TYPE);
|
||||
|
||||
$migrated_subscribers = ScheduledTaskSubscriber::where('task_id', $queue->task_id)
|
||||
->orderByAsc('subscriber_id')
|
||||
->findMany();
|
||||
expect($migrated_subscribers)->count(2);
|
||||
expect($migrated_subscribers[0]->processed)->equals(ScheduledTaskSubscriber::STATUS_UNPROCESSED);
|
||||
expect($migrated_subscribers[1]->processed)->equals(ScheduledTaskSubscriber::STATUS_PROCESSED);
|
||||
}
|
||||
|
||||
function testItResumesSendingAfterMigratingSendingQueuesAndSubscribers() {
|
||||
$this->worker->pauseSending();
|
||||
expect(MailerLog::isSendingPaused())->true();
|
||||
$task = $this->createRunningTask();
|
||||
$this->worker->processTask($task);
|
||||
expect(MailerLog::isSendingPaused())->false();
|
||||
}
|
||||
|
||||
private function createScheduledTask() {
|
||||
$task = ScheduledTask::create();
|
||||
$task->type = Migration::TASK_TYPE;
|
||||
$task->status = ScheduledTask::STATUS_SCHEDULED;
|
||||
$task->scheduled_at = Carbon::createFromTimestamp(current_time('timestamp'));
|
||||
$task->save();
|
||||
return $task;
|
||||
}
|
||||
|
||||
private function createRunningTask() {
|
||||
$task = ScheduledTask::create();
|
||||
$task->type = Migration::TASK_TYPE;
|
||||
$task->status = null;
|
||||
$task->scheduled_at = Carbon::createFromTimestamp(current_time('timestamp'));
|
||||
$task->save();
|
||||
return $task;
|
||||
}
|
||||
|
||||
private function createCompletedTask() {
|
||||
$task = ScheduledTask::create();
|
||||
$task->type = Migration::TASK_TYPE;
|
||||
$task->status = ScheduledTask::STATUS_COMPLETED;
|
||||
$task->scheduled_at = Carbon::createFromTimestamp(current_time('timestamp'));
|
||||
$task->save();
|
||||
return $task;
|
||||
}
|
||||
|
||||
private function createSendingQueue($status = null) {
|
||||
$queue = SendingQueue::create();
|
||||
$queue->newsletter_id = 0;
|
||||
$queue->task_id = 0;
|
||||
$queue->subscribers = serialize(array(
|
||||
'to_process' => array($this->subscriber_to_process->id),
|
||||
'processed' => array($this->subscriber_processed->id)
|
||||
));
|
||||
$queue->count_total = 2;
|
||||
$queue->count_processed = 1;
|
||||
$queue->count_to_process = 1;
|
||||
$queue->status = $status;
|
||||
return $queue->save();
|
||||
}
|
||||
|
||||
private function downgradeTable() {
|
||||
global $wpdb;
|
||||
$wpdb->query(
|
||||
'ALTER TABLE ' . SendingQueue::$_table . ' ' .
|
||||
'ADD COLUMN `type` varchar(90) NULL DEFAULT NULL,' .
|
||||
'ADD COLUMN `status` varchar(12) NULL DEFAULT NULL,' .
|
||||
'ADD COLUMN `priority` mediumint(9) NOT NULL DEFAULT 0,' .
|
||||
'ADD COLUMN `scheduled_at` TIMESTAMP NULL,' .
|
||||
'ADD COLUMN `processed_at` TIMESTAMP NULL'
|
||||
);
|
||||
}
|
||||
|
||||
private function restoreTable() {
|
||||
global $wpdb;
|
||||
$wpdb->query(
|
||||
'ALTER TABLE ' . SendingQueue::$_table . ' ' .
|
||||
'DROP COLUMN `type`,' .
|
||||
'DROP COLUMN `status`,' .
|
||||
'DROP COLUMN `priority`,' .
|
||||
'DROP COLUMN `scheduled_at`,' .
|
||||
'DROP COLUMN `processed_at`'
|
||||
);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
\ORM::raw_execute('TRUNCATE ' . Setting::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . ScheduledTaskSubscriber::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||
|
||||
// Restore table after testing
|
||||
if(!empty($this->altered)) {
|
||||
$this->restoreTable();
|
||||
$this->altered = false;
|
||||
}
|
||||
}
|
||||
}
|
156
tests/unit/Tasks/SendingTest.php
Normal file
156
tests/unit/Tasks/SendingTest.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\Tasks;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use MailPoet\Models\Newsletter;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
use MailPoet\Models\ScheduledTaskSubscriber;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Tasks\Sending as SendingTask;
|
||||
use MailPoet\Tasks\Subscribers;
|
||||
|
||||
class SendingTest extends \MailPoetTest {
|
||||
function _before() {
|
||||
$newsletter = Newsletter::create();
|
||||
$newsletter->type = Newsletter::TYPE_STANDARD;
|
||||
$this->newsletter = $newsletter->save();
|
||||
|
||||
$task = ScheduledTask::create();
|
||||
$task->type = SendingTask::TASK_TYPE;
|
||||
$this->task = $task->save();
|
||||
|
||||
$queue = SendingQueue::create();
|
||||
$queue->newsletter_id = $this->newsletter->id;
|
||||
$queue->task_id = $this->task->id;
|
||||
$this->queue = $queue->save();
|
||||
|
||||
$this->sending = SendingTask::create($this->task, $this->queue);
|
||||
$this->sending->setSubscribers(array(123, 456)); // random IDs
|
||||
}
|
||||
|
||||
function testItCanBeConstructed() {
|
||||
$sending = SendingTask::create();
|
||||
expect_that($sending instanceof SendingTask);
|
||||
expect_that($sending->queue() instanceof SendingQueue);
|
||||
expect_that($sending->task() instanceof ScheduledTask);
|
||||
expect_that($sending->taskSubscribers() instanceof Subscribers);
|
||||
}
|
||||
|
||||
function testItCanBeConstructedOnlyWithAProperTaskType() {
|
||||
$this->task->type = 'wrong_type';
|
||||
try {
|
||||
$sending = SendingTask::create($this->task, $this->queue);
|
||||
$this->fail('Exception was not thrown');
|
||||
} catch(\Exception $e) {
|
||||
// No exception handling necessary
|
||||
}
|
||||
}
|
||||
|
||||
function testItCanBeCreatedFromTask() {
|
||||
$sending = SendingTask::createFromTask($this->task);
|
||||
$queue = $sending->queue();
|
||||
expect($queue->task_id)->equals($this->task->id);
|
||||
}
|
||||
|
||||
function testItCanBeCreatedFromQueue() {
|
||||
$sending = SendingTask::createFromQueue($this->queue);
|
||||
$task = $sending->task();
|
||||
expect($task->id)->equals($this->queue->task_id);
|
||||
}
|
||||
|
||||
function testItCanBeInitializedByNewsletterId() {
|
||||
$sending = SendingTask::getByNewsletterId($this->newsletter->id);
|
||||
$queue = $sending->queue();
|
||||
$task = $sending->task();
|
||||
expect($queue->id)->equals($this->newsletter->id);
|
||||
expect($task->id)->equals($queue->task_id);
|
||||
}
|
||||
|
||||
function testItCanBeConvertedToArray() {
|
||||
$sending = $this->sending->asArray();
|
||||
expect($sending['id'])->equals($this->queue->id);
|
||||
expect($sending['task_id'])->equals($this->task->id);
|
||||
}
|
||||
|
||||
function testItSavesDataForUnderlyingModels() {
|
||||
$newsletter_rendered_subject = 'Abc';
|
||||
$status = ScheduledTask::STATUS_PAUSED;
|
||||
$this->sending->status = $status;
|
||||
$this->sending->newsletter_rendered_subject = $newsletter_rendered_subject;
|
||||
$this->sending->save();
|
||||
$task = ScheduledTask::findOne($this->task->id);
|
||||
$queue = SendingQueue::findOne($this->queue->id);
|
||||
expect($task->status)->equals($status);
|
||||
expect($queue->newsletter_rendered_subject)->equals($newsletter_rendered_subject);
|
||||
}
|
||||
|
||||
function testItDeletesUnderlyingModels() {
|
||||
$this->sending->delete();
|
||||
expect(ScheduledTask::findOne($this->task->id))->equals(false);
|
||||
expect(SendingQueue::findOne($this->queue->id))->equals(false);
|
||||
expect(ScheduledTaskSubscriber::where('task_id', $this->task->id)->findMany())->isEmpty();
|
||||
}
|
||||
|
||||
function testItGetsSubscribers() {
|
||||
expect($this->sending->getSubscribers())->equals(array(123, 456));
|
||||
}
|
||||
|
||||
function testItSetsSubscribers() {
|
||||
$subscriber_ids = array(1, 2, 3);
|
||||
$this->sending->setSubscribers($subscriber_ids);
|
||||
expect($this->sending->getSubscribers())->equals($subscriber_ids);
|
||||
expect($this->sending->count_total)->equals(count($subscriber_ids));
|
||||
}
|
||||
|
||||
function testItRemovesSubscribers() {
|
||||
$subscriber_ids = array(456);
|
||||
$this->sending->removeSubscribers($subscriber_ids);
|
||||
expect($this->sending->getSubscribers())->equals(array(123));
|
||||
expect($this->sending->count_total)->equals(1);
|
||||
}
|
||||
|
||||
function testItRemovesAllSubscribers() {
|
||||
$this->sending->removeAllSubscribers();
|
||||
expect($this->sending->getSubscribers())->equals(array());
|
||||
expect($this->sending->count_total)->equals(0);
|
||||
}
|
||||
|
||||
function testItUpdatesProcessedSubscribers() {
|
||||
expect($this->sending->count_to_process)->equals(2);
|
||||
expect($this->sending->count_processed)->equals(0);
|
||||
$subscriber_ids = array(456);
|
||||
$this->sending->updateProcessedSubscribers($subscriber_ids);
|
||||
expect($this->sending->count_to_process)->equals(1);
|
||||
expect($this->sending->count_processed)->equals(1);
|
||||
}
|
||||
|
||||
function testItGetsSchedulesQueues() {
|
||||
$this->sending->status = ScheduledTask::STATUS_SCHEDULED;
|
||||
$this->sending->scheduled_at = Carbon::createFromTimestamp(current_time('timestamp'))->subHours(1);
|
||||
$this->sending->save();
|
||||
$tasks = SendingTask::getScheduledQueues();
|
||||
expect($tasks)->notEmpty();
|
||||
foreach($tasks as $task) {
|
||||
expect_that($task instanceof SendingTask);
|
||||
}
|
||||
}
|
||||
|
||||
function testItGetsRunningQueues() {
|
||||
$this->sending->status = null;
|
||||
$this->sending->scheduled_at = Carbon::createFromTimestamp(current_time('timestamp'))->subHours(1);
|
||||
$this->sending->save();
|
||||
$tasks = SendingTask::getRunningQueues();
|
||||
expect($tasks)->notEmpty();
|
||||
foreach($tasks as $task) {
|
||||
expect_that($task instanceof SendingTask);
|
||||
}
|
||||
}
|
||||
|
||||
function _after() {
|
||||
\ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . ScheduledTaskSubscriber::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user