Refactor SendingTaskSubscriber API Test to doctrine
[MAILPOET-4006]
This commit is contained in:
committed by
Veljko V
parent
8291b2f19c
commit
d1b8ddee91
@ -6,76 +6,84 @@ use Codeception\Util\Fixtures;
|
|||||||
use MailPoet\API\JSON\Response as APIResponse;
|
use MailPoet\API\JSON\Response as APIResponse;
|
||||||
use MailPoet\API\JSON\v1\SendingTaskSubscribers;
|
use MailPoet\API\JSON\v1\SendingTaskSubscribers;
|
||||||
use MailPoet\DI\ContainerWrapper;
|
use MailPoet\DI\ContainerWrapper;
|
||||||
use MailPoet\Models\Newsletter;
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
use MailPoet\Models\ScheduledTask;
|
use MailPoet\Entities\ScheduledTaskEntity;
|
||||||
use MailPoet\Models\ScheduledTaskSubscriber;
|
use MailPoet\Entities\ScheduledTaskSubscriberEntity;
|
||||||
use MailPoet\Models\SendingQueue;
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
use MailPoet\Test\DataFactories\Newsletter as NewsletterFactory;
|
||||||
|
use MailPoet\Test\DataFactories\ScheduledTaskSubscriber as TaskSubscriberFactory;
|
||||||
|
use MailPoet\Test\DataFactories\Subscriber as SubscriberFactory;
|
||||||
|
|
||||||
class SendingTaskSubscribersTest extends \MailPoetTest {
|
class SendingTaskSubscribersTest extends \MailPoetTest {
|
||||||
public $unprocessedSubscriber;
|
/** @var SubscriberEntity */
|
||||||
public $failedSubscriber;
|
private $unprocessedSubscriber;
|
||||||
public $sentSubscriber;
|
|
||||||
public $taskId;
|
/** @var SubscriberEntity */
|
||||||
public $newsletterId;
|
private $failedSubscriber;
|
||||||
public $endpoint;
|
|
||||||
|
/** @var ScheduledTaskSubscriberEntity */
|
||||||
|
private $failedSubscriberTask;
|
||||||
|
|
||||||
|
/** @var SubscriberEntity */
|
||||||
|
private $sentSubscriber;
|
||||||
|
|
||||||
|
/** @var ScheduledTaskEntity */
|
||||||
|
private $task;
|
||||||
|
|
||||||
|
/** @var NewsletterEntity */
|
||||||
|
private $newsletter;
|
||||||
|
|
||||||
|
/** @var SendingTaskSubscribers */
|
||||||
|
private $endpoint;
|
||||||
|
|
||||||
|
/** @var SubscriberFactory */
|
||||||
|
private $subscriberFactory;
|
||||||
|
|
||||||
|
/** @var TaskSubscriberFactory */
|
||||||
|
private $taskSubscriberFactory;
|
||||||
|
|
||||||
public function _before() {
|
public function _before() {
|
||||||
parent::_before();
|
parent::_before();
|
||||||
$this->endpoint = ContainerWrapper::getInstance()->get(SendingTaskSubscribers::class);
|
$this->endpoint = ContainerWrapper::getInstance()->get(SendingTaskSubscribers::class);
|
||||||
$this->newsletterId = Newsletter::createOrUpdate([
|
$this->subscriberFactory = new SubscriberFactory();
|
||||||
'type' => Newsletter::TYPE_STANDARD,
|
$this->taskSubscriberFactory = new TaskSubscriberFactory();
|
||||||
'subject' => 'My Standard Newsletter',
|
|
||||||
'body' => Fixtures::get('newsletter_body_template'),
|
|
||||||
])->id;
|
$this->newsletter = (new NewsletterFactory())->withSubject('My Standard Newsletter')
|
||||||
$this->taskId = ScheduledTask::createOrUpdate([
|
->withBody(Fixtures::get('newsletter_body_template'))
|
||||||
'status' => ScheduledTask::STATUS_SCHEDULED,
|
->withSendingQueue()
|
||||||
])->id;
|
->create();
|
||||||
SendingQueue::createOrUpdate([
|
|
||||||
'task_id' => $this->taskId,
|
$queue = $this->newsletter->getLatestQueue();
|
||||||
'newsletter_id' => $this->newsletterId,
|
$this->task = $queue->getTask();
|
||||||
]);
|
|
||||||
$this->sentSubscriber = Subscriber::createOrUpdate([
|
$this->sentSubscriber = $this->subscriberFactory
|
||||||
'last_name' => 'Test',
|
->withEmail('sent@example.com')
|
||||||
'first_name' => 'Sent',
|
->withFirstName('Sent')
|
||||||
'email' => 'sent@example.com',
|
->withLastName('Test')
|
||||||
]);
|
->create();
|
||||||
ScheduledTaskSubscriber::createOrUpdate([
|
$this->taskSubscriberFactory->createProcessed($this->task, $this->sentSubscriber);
|
||||||
'failed' => 0,
|
|
||||||
'processed' => 1,
|
$this->failedSubscriber = $this->subscriberFactory
|
||||||
'task_id' => $this->taskId,
|
->withEmail('failed@example.com')
|
||||||
'subscriber_id' => $this->sentSubscriber->id,
|
->withFirstName('Failed')
|
||||||
]);
|
->withLastName('Test')
|
||||||
$this->failedSubscriber = Subscriber::createOrUpdate([
|
->create();
|
||||||
'last_name' => 'Test',
|
$this->failedSubscriberTask = $this->taskSubscriberFactory->createFailed($this->task, $this->failedSubscriber, 'Something went wrong!');
|
||||||
'first_name' => 'Failed',
|
|
||||||
'email' => 'failed@example.com',
|
$this->unprocessedSubscriber = $this->subscriberFactory
|
||||||
]);
|
->withEmail('unprocessed@example.com')
|
||||||
ScheduledTaskSubscriber::createOrUpdate([
|
->withFirstName('Unprocessed')
|
||||||
'failed' => 1,
|
->withLastName('Test')
|
||||||
'processed' => 1,
|
->create();
|
||||||
'task_id' => $this->taskId,
|
$this->taskSubscriberFactory->createUnprocessed($this->task, $this->unprocessedSubscriber);
|
||||||
'error' => 'Something went wrong!',
|
|
||||||
'subscriber_id' => $this->failedSubscriber->id,
|
|
||||||
]);
|
|
||||||
$this->unprocessedSubscriber = Subscriber::createOrUpdate([
|
|
||||||
'last_name' => 'Test',
|
|
||||||
'first_name' => 'Unprocessed',
|
|
||||||
'email' => 'unprocessed@example.com',
|
|
||||||
]);
|
|
||||||
ScheduledTaskSubscriber::createOrUpdate([
|
|
||||||
'failed' => 0,
|
|
||||||
'processed' => 0,
|
|
||||||
'task_id' => $this->taskId,
|
|
||||||
'subscriber_id' => $this->unprocessedSubscriber->id,
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testListingReturnsErrorIfMissingNewsletter() {
|
public function testListingReturnsErrorIfMissingNewsletter() {
|
||||||
$res = $this->endpoint->listing([
|
$res = $this->endpoint->listing([
|
||||||
'sort_by' => 'created_at',
|
'sort_by' => 'created_at',
|
||||||
'params' => ['id' => $this->newsletterId + 1],
|
'params' => ['id' => $this->newsletter->getId() + 1],
|
||||||
]);
|
]);
|
||||||
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
||||||
expect($res->errors[0]['message'])
|
expect($res->errors[0]['message'])
|
||||||
@ -83,14 +91,10 @@ class SendingTaskSubscribersTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testListingReturnsErrorIfNewsletterNotBeingSent() {
|
public function testListingReturnsErrorIfNewsletterNotBeingSent() {
|
||||||
$newsletter = Newsletter::createOrUpdate([
|
$newsletterWithoutTask = ((new NewsletterFactory()))->create();
|
||||||
'type' => Newsletter::TYPE_STANDARD,
|
|
||||||
'subject' => 'Draft',
|
|
||||||
'body' => '',
|
|
||||||
]);
|
|
||||||
$res = $this->endpoint->listing([
|
$res = $this->endpoint->listing([
|
||||||
'sort_by' => 'created_at',
|
'sort_by' => 'created_at',
|
||||||
'params' => ['id' => $newsletter->id],
|
'params' => ['id' => $newsletterWithoutTask->getId()],
|
||||||
]);
|
]);
|
||||||
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
||||||
expect($res->errors[0]['message'])
|
expect($res->errors[0]['message'])
|
||||||
@ -102,36 +106,36 @@ class SendingTaskSubscribersTest extends \MailPoetTest {
|
|||||||
'error' => null,
|
'error' => null,
|
||||||
'failed' => 0,
|
'failed' => 0,
|
||||||
'processed' => 1,
|
'processed' => 1,
|
||||||
'taskId' => (int)$this->taskId,
|
'taskId' => $this->task->getId(),
|
||||||
'email' => $this->sentSubscriber->email,
|
'email' => $this->sentSubscriber->getEmail(),
|
||||||
'subscriberId' => (int)$this->sentSubscriber->id,
|
'subscriberId' => $this->sentSubscriber->getId(),
|
||||||
'lastName' => $this->sentSubscriber->last_name,
|
'lastName' => $this->sentSubscriber->getLastName(),
|
||||||
'firstName' => $this->sentSubscriber->first_name,
|
'firstName' => $this->sentSubscriber->getFirstName(),
|
||||||
];
|
];
|
||||||
$unprocessedSubscriberStatus = [
|
$unprocessedSubscriberStatus = [
|
||||||
'error' => null,
|
'error' => null,
|
||||||
'failed' => 0,
|
'failed' => 0,
|
||||||
'processed' => 0,
|
'processed' => 0,
|
||||||
'taskId' => (int)$this->taskId,
|
'taskId' => $this->task->getId(),
|
||||||
'email' => $this->unprocessedSubscriber->email,
|
'email' => $this->unprocessedSubscriber->getEmail(),
|
||||||
'subscriberId' => (int)$this->unprocessedSubscriber->id,
|
'subscriberId' => $this->unprocessedSubscriber->getId(),
|
||||||
'lastName' => $this->unprocessedSubscriber->last_name,
|
'lastName' => $this->unprocessedSubscriber->getLastName(),
|
||||||
'firstName' => $this->unprocessedSubscriber->first_name,
|
'firstName' => $this->unprocessedSubscriber->getFirstName(),
|
||||||
];
|
];
|
||||||
$failedSubscriberStatus = [
|
$failedSubscriberStatus = [
|
||||||
'error' => 'Something went wrong!',
|
'error' => 'Something went wrong!',
|
||||||
'failed' => 1,
|
'failed' => 1,
|
||||||
'processed' => 1,
|
'processed' => 1,
|
||||||
'taskId' => (int)$this->taskId,
|
'taskId' => $this->task->getId(),
|
||||||
'email' => $this->failedSubscriber->email,
|
'email' => $this->failedSubscriber->getEmail(),
|
||||||
'subscriberId' => (int)$this->failedSubscriber->id,
|
'subscriberId' => $this->failedSubscriber->getId(),
|
||||||
'lastName' => $this->failedSubscriber->last_name,
|
'lastName' => $this->failedSubscriber->getLastName(),
|
||||||
'firstName' => $this->failedSubscriber->first_name,
|
'firstName' => $this->failedSubscriber->getFirstName(),
|
||||||
];
|
];
|
||||||
|
|
||||||
$res = $this->endpoint->listing([
|
$res = $this->endpoint->listing([
|
||||||
'sort_by' => 'created_at',
|
'sort_by' => 'subscriber',
|
||||||
'params' => ['id' => $this->newsletterId],
|
'params' => ['id' => $this->newsletter->getId()],
|
||||||
]);
|
]);
|
||||||
expect($res->status)->equals(APIResponse::STATUS_OK);
|
expect($res->status)->equals(APIResponse::STATUS_OK);
|
||||||
expect($res->data)->equals([
|
expect($res->data)->equals([
|
||||||
@ -143,7 +147,7 @@ class SendingTaskSubscribersTest extends \MailPoetTest {
|
|||||||
$res = $this->endpoint->listing([
|
$res = $this->endpoint->listing([
|
||||||
'group' => 'sent',
|
'group' => 'sent',
|
||||||
'sort_by' => 'created_at',
|
'sort_by' => 'created_at',
|
||||||
'params' => ['id' => $this->newsletterId],
|
'params' => ['id' => $this->newsletter->getId()],
|
||||||
]);
|
]);
|
||||||
expect($res->status)->equals(APIResponse::STATUS_OK);
|
expect($res->status)->equals(APIResponse::STATUS_OK);
|
||||||
expect($res->data)->equals([
|
expect($res->data)->equals([
|
||||||
@ -153,7 +157,7 @@ class SendingTaskSubscribersTest extends \MailPoetTest {
|
|||||||
$res = $this->endpoint->listing([
|
$res = $this->endpoint->listing([
|
||||||
'group' => 'failed',
|
'group' => 'failed',
|
||||||
'sort_by' => 'created_at',
|
'sort_by' => 'created_at',
|
||||||
'params' => ['id' => $this->newsletterId],
|
'params' => ['id' => $this->newsletter->getId()],
|
||||||
]);
|
]);
|
||||||
expect($res->status)->equals(APIResponse::STATUS_OK);
|
expect($res->status)->equals(APIResponse::STATUS_OK);
|
||||||
expect($res->data)->equals([
|
expect($res->data)->equals([
|
||||||
@ -163,7 +167,7 @@ class SendingTaskSubscribersTest extends \MailPoetTest {
|
|||||||
$res = $this->endpoint->listing([
|
$res = $this->endpoint->listing([
|
||||||
'group' => 'unprocessed',
|
'group' => 'unprocessed',
|
||||||
'sort_by' => 'created_at',
|
'sort_by' => 'created_at',
|
||||||
'params' => ['id' => $this->newsletterId],
|
'params' => ['id' => $this->newsletter->getId()],
|
||||||
]);
|
]);
|
||||||
expect($res->status)->equals(APIResponse::STATUS_OK);
|
expect($res->status)->equals(APIResponse::STATUS_OK);
|
||||||
expect($res->data)->equals([
|
expect($res->data)->equals([
|
||||||
@ -173,16 +177,16 @@ class SendingTaskSubscribersTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testResendReturnsErrorIfWrongData() {
|
public function testResendReturnsErrorIfWrongData() {
|
||||||
$res = $this->endpoint->resend([
|
$res = $this->endpoint->resend([
|
||||||
'taskId' => $this->taskId + 1,
|
'taskId' => $this->task->getId() + 1,
|
||||||
'subscriberId' => $this->sentSubscriber->id,
|
'subscriberId' => $this->sentSubscriber->getId(),
|
||||||
]);
|
]);
|
||||||
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
||||||
expect($res->errors[0]['message'])
|
expect($res->errors[0]['message'])
|
||||||
->equals('Failed sending task not found!');
|
->equals('Failed sending task not found!');
|
||||||
|
|
||||||
$res = $this->endpoint->resend([
|
$res = $this->endpoint->resend([
|
||||||
'taskId' => $this->taskId,
|
'taskId' => $this->task->getId(),
|
||||||
'subscriberId' => $this->sentSubscriber->id,
|
'subscriberId' => $this->sentSubscriber->getId(),
|
||||||
]);
|
]);
|
||||||
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
expect($res->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
||||||
expect($res->errors[0]['message'])
|
expect($res->errors[0]['message'])
|
||||||
@ -191,33 +195,28 @@ class SendingTaskSubscribersTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testItCanResend() {
|
public function testItCanResend() {
|
||||||
$res = $this->endpoint->resend([
|
$res = $this->endpoint->resend([
|
||||||
'taskId' => $this->taskId,
|
'taskId' => $this->task->getId(),
|
||||||
'subscriberId' => $this->failedSubscriber->id,
|
'subscriberId' => $this->failedSubscriber->getId(),
|
||||||
]);
|
]);
|
||||||
expect($res->status)->equals(APIResponse::STATUS_OK);
|
expect($res->status)->equals(APIResponse::STATUS_OK);
|
||||||
|
|
||||||
$taskSubscriber = ScheduledTaskSubscriber::where('task_id', $this->taskId)
|
$this->entityManager->refresh($this->failedSubscriberTask);
|
||||||
->where('subscriber_id', $this->failedSubscriber->id)
|
expect($this->failedSubscriberTask->getError())->equals(null);
|
||||||
->findOne();
|
expect($this->failedSubscriberTask->getFailed())->equals(0);
|
||||||
assert($taskSubscriber instanceof ScheduledTaskSubscriber);
|
expect($this->failedSubscriberTask->getProcessed())->equals(0);
|
||||||
expect($taskSubscriber->error)->equals('');
|
|
||||||
expect($taskSubscriber->failed)->equals(0);
|
|
||||||
expect($taskSubscriber->processed)->equals(0);
|
|
||||||
|
|
||||||
$task = ScheduledTask::findOne($this->taskId);
|
$this->entityManager->refresh($this->task);
|
||||||
assert($task instanceof ScheduledTask);
|
expect($this->task->getStatus())->equals(null);
|
||||||
expect($task->status)->equals(null);
|
|
||||||
|
|
||||||
$newsletter = Newsletter::findOne($this->newsletterId);
|
$this->entityManager->refresh($this->newsletter);
|
||||||
assert($newsletter instanceof Newsletter);
|
expect($this->newsletter->getStatus())->equals(NewsletterEntity::STATUS_SENDING);
|
||||||
expect($newsletter->status)->equals(Newsletter::STATUS_SENDING);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function _after() {
|
public function _after() {
|
||||||
ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
|
$this->truncateEntity(ScheduledTaskEntity::class);
|
||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
$this->truncateEntity(ScheduledTaskSubscriberEntity::class);
|
||||||
ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table);
|
$this->truncateEntity(NewsletterEntity::class);
|
||||||
ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
|
$this->truncateEntity(SendingQueueEntity::class);
|
||||||
ORM::raw_execute('TRUNCATE ' . ScheduledTaskSubscriber::$_table);
|
$this->truncateEntity(SubscriberEntity::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user