Add integration tests
This commit is contained in:
committed by
M. Shull
parent
0d27d0f7a6
commit
fd134f288c
213
tests/integration/API/JSON/v1/SendingTaskSubscribersTest.php
Normal file
213
tests/integration/API/JSON/v1/SendingTaskSubscribersTest.php
Normal file
@ -0,0 +1,213 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Test\API\JSON\v1;
|
||||
|
||||
use Codeception\Util\Fixtures;
|
||||
use MailPoet\Models\Newsletter;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
use MailPoet\Models\ScheduledTaskSubscriber;
|
||||
use MailPoet\API\JSON\Response as APIResponse;
|
||||
use MailPoet\API\JSON\v1\SendingTaskSubscribers;
|
||||
|
||||
class SendingTaskSubscribersTest extends \MailPoetTest {
|
||||
|
||||
function _before() {
|
||||
parent::_before();
|
||||
$this->endpoint = ContainerWrapper::getInstance()->get(SendingTaskSubscribers::class);
|
||||
$this->newsletter_id = Newsletter::createOrUpdate([
|
||||
'type' => Newsletter::TYPE_STANDARD,
|
||||
'subject' => 'My Standard Newsletter',
|
||||
'body' => Fixtures::get('newsletter_body_template'),
|
||||
])->id;
|
||||
$this->task_id = ScheduledTask::createOrUpdate([
|
||||
'status' => ScheduledTask::STATUS_SCHEDULED,
|
||||
])->id;
|
||||
SendingQueue::createOrUpdate([
|
||||
'task_id' => $this->task_id,
|
||||
'newsletter_id' => $this->newsletter_id,
|
||||
]);
|
||||
$this->sent_subscriber = Subscriber::createOrUpdate([
|
||||
'last_name' => 'Test',
|
||||
'first_name' => 'Sent',
|
||||
'email' => 'sent@example.com',
|
||||
]);
|
||||
ScheduledTaskSubscriber::createOrUpdate([
|
||||
'failed' => 0,
|
||||
'processed' => 1,
|
||||
'task_id' => $this->task_id,
|
||||
'subscriber_id' => $this->sent_subscriber->id,
|
||||
]);
|
||||
$this->failed_subscriber = Subscriber::createOrUpdate([
|
||||
'last_name' => 'Test',
|
||||
'first_name' => 'Failed',
|
||||
'email' => 'failed@example.com',
|
||||
]);
|
||||
ScheduledTaskSubscriber::createOrUpdate([
|
||||
'failed' => 1,
|
||||
'processed' => 1,
|
||||
'task_id' => $this->task_id,
|
||||
'error' => 'Something went wrong!',
|
||||
'subscriber_id' => $this->failed_subscriber->id,
|
||||
]);
|
||||
$this->unprocessed_subscriber = Subscriber::createOrUpdate([
|
||||
'last_name' => 'Test',
|
||||
'first_name' => 'Unprocessed',
|
||||
'email' => 'unprocessed@example.com',
|
||||
]);
|
||||
ScheduledTaskSubscriber::createOrUpdate([
|
||||
'failed' => 0,
|
||||
'processed' => 0,
|
||||
'task_id' => $this->task_id,
|
||||
'subscriber_id' => $this->unprocessed_subscriber->id,
|
||||
]);
|
||||
}
|
||||
|
||||
function testListingReturnsErrorIfMissingNewsletter() {
|
||||
$res = $this->endpoint->listing([
|
||||
'sort_by' => 'created_at',
|
||||
'params' => ['id' => $this->newsletter_id + 1],
|
||||
]);
|
||||
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
||||
expect($res->errors[0]['message'])
|
||||
->equals('This newsletter is not being sent to any subcriber yet.');
|
||||
}
|
||||
|
||||
function testListingReturnsErrorIfNewsletterNotBeingSent() {
|
||||
$newsletter = Newsletter::createOrUpdate([
|
||||
'type' => Newsletter::TYPE_STANDARD,
|
||||
'subject' => 'Draft',
|
||||
'body' => '',
|
||||
]);
|
||||
$res = $this->endpoint->listing([
|
||||
'sort_by' => 'created_at',
|
||||
'params' => ['id' => $newsletter->id],
|
||||
]);
|
||||
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
||||
expect($res->errors[0]['message'])
|
||||
->equals('This newsletter is not being sent to any subcriber yet.');
|
||||
}
|
||||
|
||||
function testItReturnsListing() {
|
||||
$sent_subscriber_status = [
|
||||
'error' => '',
|
||||
'failed' => 0,
|
||||
'processed' => 1,
|
||||
'taskId' => $this->task_id,
|
||||
'email' => $this->sent_subscriber->email,
|
||||
'subscriberId' => $this->sent_subscriber->id,
|
||||
'lastName' => $this->sent_subscriber->last_name,
|
||||
'firstName' => $this->sent_subscriber->first_name,
|
||||
];
|
||||
$unprocessed_subscriber_status = [
|
||||
'error' => '',
|
||||
'failed' => 0,
|
||||
'processed' => 0,
|
||||
'taskId' => $this->task_id,
|
||||
'email' => $this->unprocessed_subscriber->email,
|
||||
'subscriberId' => $this->unprocessed_subscriber->id,
|
||||
'lastName' => $this->unprocessed_subscriber->last_name,
|
||||
'firstName' => $this->unprocessed_subscriber->first_name,
|
||||
];
|
||||
$failed_subscriber_status = [
|
||||
'error' => 'Something went wrong!',
|
||||
'failed' => 1,
|
||||
'processed' => 1,
|
||||
'taskId' => $this->task_id,
|
||||
'email' => $this->failed_subscriber->email,
|
||||
'subscriberId' => $this->failed_subscriber->id,
|
||||
'lastName' => $this->failed_subscriber->last_name,
|
||||
'firstName' => $this->failed_subscriber->first_name,
|
||||
];
|
||||
|
||||
$res = $this->endpoint->listing([
|
||||
'sort_by' => 'created_at',
|
||||
'params' => ['id' => $this->newsletter_id],
|
||||
]);
|
||||
expect($res->status)->equals(APIResponse::STATUS_OK);
|
||||
expect($res->data)->equals([
|
||||
$sent_subscriber_status,
|
||||
$failed_subscriber_status,
|
||||
$unprocessed_subscriber_status,
|
||||
]);
|
||||
|
||||
$res = $this->endpoint->listing([
|
||||
'group' => 'sent',
|
||||
'sort_by' => 'created_at',
|
||||
'params' => ['id' => $this->newsletter_id],
|
||||
]);
|
||||
expect($res->status)->equals(APIResponse::STATUS_OK);
|
||||
expect($res->data)->equals([
|
||||
$sent_subscriber_status,
|
||||
]);
|
||||
|
||||
$res = $this->endpoint->listing([
|
||||
'group' => 'failed',
|
||||
'sort_by' => 'created_at',
|
||||
'params' => ['id' => $this->newsletter_id],
|
||||
]);
|
||||
expect($res->status)->equals(APIResponse::STATUS_OK);
|
||||
expect($res->data)->equals([
|
||||
$failed_subscriber_status,
|
||||
]);
|
||||
|
||||
$res = $this->endpoint->listing([
|
||||
'group' => 'unprocessed',
|
||||
'sort_by' => 'created_at',
|
||||
'params' => ['id' => $this->newsletter_id],
|
||||
]);
|
||||
expect($res->status)->equals(APIResponse::STATUS_OK);
|
||||
expect($res->data)->equals([
|
||||
$unprocessed_subscriber_status,
|
||||
]);
|
||||
}
|
||||
|
||||
function testResendReturnsErrorIfWrongData() {
|
||||
$res = $this->endpoint->resend([
|
||||
'taskId' => $this->task_id + 1,
|
||||
'subscriberId' => $this->sent_subscriber->id,
|
||||
]);
|
||||
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
||||
expect($res->errors[0]['message'])
|
||||
->equals('Failed sending task not found!');
|
||||
|
||||
$res = $this->endpoint->resend([
|
||||
'taskId' => $this->task_id,
|
||||
'subscriberId' => $this->sent_subscriber->id,
|
||||
]);
|
||||
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
||||
expect($res->errors[0]['message'])
|
||||
->equals('Failed sending task not found!');
|
||||
}
|
||||
|
||||
function testItCanResend() {
|
||||
$res = $this->endpoint->resend([
|
||||
'taskId' => $this->task_id,
|
||||
'subscriberId' => $this->failed_subscriber->id,
|
||||
]);
|
||||
expect($res->status)->equals(APIResponse::STATUS_OK);
|
||||
|
||||
$task_subscriber = ScheduledTaskSubscriber::where('task_id', $this->task_id)
|
||||
->where('subscriber_id', $this->failed_subscriber->id)
|
||||
->findOne();
|
||||
expect($task_subscriber->error)->equals('');
|
||||
expect($task_subscriber->failed)->equals(0);
|
||||
expect($task_subscriber->processed)->equals(0);
|
||||
|
||||
$task = ScheduledTask::findOne($this->task_id);
|
||||
expect($task->status)->equals(null);
|
||||
|
||||
$newsletter = Newsletter::findOne($this->newsletter_id);
|
||||
expect($newsletter->status)->equals(Newsletter::STATUS_SENDING);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
\ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . ScheduledTaskSubscriber::$_table);
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ class ScheduledTaskSubscriberTest extends \MailPoetTest {
|
||||
'task_id' => $this->task->id,
|
||||
'subscriber_id' => $this->subscriber->id,
|
||||
]);
|
||||
$this->subscribers_counter = 0;
|
||||
}
|
||||
|
||||
function testItCanBeCreated() {
|
||||
@ -81,6 +82,123 @@ class ScheduledTaskSubscriberTest extends \MailPoetTest {
|
||||
expect($count)->equals(2);
|
||||
}
|
||||
|
||||
function testItCanQueryListing() {
|
||||
$task_ids = $this->makeTasksWithSubscribers();
|
||||
|
||||
$all = ScheduledTaskSubscriber::listingQuery([
|
||||
'params' => ['task_ids' => $task_ids],
|
||||
])->findMany();
|
||||
expect(count($all))->equals(12);
|
||||
|
||||
$sent = ScheduledTaskSubscriber::listingQuery([
|
||||
'group' => ScheduledTaskSubscriber::SENDING_STATUS_SENT,
|
||||
'params' => ['task_ids' => $task_ids],
|
||||
])->findMany();
|
||||
expect(count($sent))->equals(4);
|
||||
foreach ($sent as $task) {
|
||||
expect($task->processed)->equals(1);
|
||||
expect($task->failed)->equals(0);
|
||||
}
|
||||
|
||||
$unprocessed = ScheduledTaskSubscriber::listingQuery([
|
||||
'group' => ScheduledTaskSubscriber::SENDING_STATUS_UNPROCESSED,
|
||||
'params' => ['task_ids' => $task_ids],
|
||||
])->findMany();
|
||||
expect(count($unprocessed))->equals(4);
|
||||
foreach ($unprocessed as $task) {
|
||||
expect($task->processed)->equals(0);
|
||||
expect($task->failed)->equals(0);
|
||||
}
|
||||
|
||||
$failed = ScheduledTaskSubscriber::listingQuery([
|
||||
'group' => ScheduledTaskSubscriber::SENDING_STATUS_FAILED,
|
||||
'params' => ['task_ids' => $task_ids],
|
||||
])->findMany();
|
||||
expect(count($failed))->equals(4);
|
||||
foreach ($failed as $task) {
|
||||
expect($task->processed)->equals(1);
|
||||
expect($task->failed)->equals(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function testItCanGetGroupsWithCounts() {
|
||||
$task_ids = $this->makeTasksWithSubscribers();
|
||||
$groups = ScheduledTaskSubscriber::groups([
|
||||
'params' => ['task_ids' => $task_ids],
|
||||
]);
|
||||
expect($groups)->equals([
|
||||
[
|
||||
'name' => 'all',
|
||||
'label' => 'All',
|
||||
'count' => 12,
|
||||
],
|
||||
[
|
||||
'name' => ScheduledTaskSubscriber::SENDING_STATUS_SENT,
|
||||
'label' => 'Sent',
|
||||
'count' => 4,
|
||||
],
|
||||
[
|
||||
'name' => ScheduledTaskSubscriber::SENDING_STATUS_FAILED,
|
||||
'label' => 'Failed',
|
||||
'count' => 4,
|
||||
],
|
||||
[
|
||||
'name' => ScheduledTaskSubscriber::SENDING_STATUS_UNPROCESSED,
|
||||
'label' => 'Unprocessed',
|
||||
'count' => 4,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates completed, scheduled, paued and running tasks.
|
||||
* Each one with unprocessed, sent and failed subscriber tasks.
|
||||
* @return array the ids of the 4 tasks.
|
||||
*/
|
||||
private function makeTasksWithSubscribers() {
|
||||
$tasks = [
|
||||
ScheduledTask::createOrUpdate(['status' => ScheduledTask::STATUS_COMPLETED]),
|
||||
ScheduledTask::createOrUpdate(['status' => ScheduledTask::STATUS_SCHEDULED]),
|
||||
ScheduledTask::createOrUpdate(['status' => ScheduledTask::STATUS_PAUSED]),
|
||||
ScheduledTask::createOrUpdate(['status' => null]), // running
|
||||
];
|
||||
foreach ($tasks as $task) {
|
||||
ScheduledTaskSubscriber::createOrUpdate([
|
||||
'task_id' => $task->id,
|
||||
'subscriber_id' => $this->makeSubscriber()->id,
|
||||
'processed' => 0,
|
||||
'failed' => 0,
|
||||
]);
|
||||
ScheduledTaskSubscriber::createOrUpdate([
|
||||
'task_id' => $task->id,
|
||||
'subscriber_id' => $this->makeSubscriber()->id,
|
||||
'processed' => 1,
|
||||
'failed' => 0,
|
||||
]);
|
||||
ScheduledTaskSubscriber::createOrUpdate([
|
||||
'task_id' => $task->id,
|
||||
'subscriber_id' => $this->makeSubscriber()->id,
|
||||
'processed' => 1,
|
||||
'failed' => 1,
|
||||
'error' => 'Something went wrong!',
|
||||
]);
|
||||
}
|
||||
|
||||
return array_map(function($task) {
|
||||
return $task->id;
|
||||
}, $tasks);
|
||||
}
|
||||
|
||||
private function makeSubscriber() {
|
||||
$number = $this->subscribers_counter ++;
|
||||
return $subscriber = Subscriber::createOrUpdate([
|
||||
'last_name' => 'Last Name ' . $number,
|
||||
'first_name' => 'First Name ' . $number,
|
||||
'email' => 'john.doe.' . $number . '@example.com',
|
||||
]);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
\ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . ScheduledTaskSubscriber::$_table);
|
||||
|
Reference in New Issue
Block a user