Add factory method to create ScheduledTasks
[MAILPOET-2996]
This commit is contained in:
@@ -8,10 +8,21 @@ use MailPoet\Cron\Workers\SendingQueue\Migration;
|
||||
use MailPoet\Cron\Workers\SubscriberLinkTokens;
|
||||
use MailPoet\Cron\Workers\UnsubscribeTokens;
|
||||
use MailPoet\Cron\Workers\WooCommercePastOrders;
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
use MailPoet\Entities\ScheduledTaskEntity;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoetVendor\Carbon\Carbon;
|
||||
use MailPoetVendor\Doctrine\ORM\EntityManager;
|
||||
|
||||
class ScheduledTask {
|
||||
private $diContainer;
|
||||
private $entityManager;
|
||||
|
||||
public function __construct() {
|
||||
$this->diContainer = ContainerWrapper::getInstance();
|
||||
$this->entityManager = $this->diContainer->get(EntityManager::class);
|
||||
}
|
||||
|
||||
public function deleteAll() {
|
||||
$tasks = \MailPoet\Models\ScheduledTask::findMany();
|
||||
foreach ($tasks as $task) {
|
||||
@@ -19,6 +30,22 @@ class ScheduledTask {
|
||||
}
|
||||
}
|
||||
|
||||
public function create(string $type, ?string $status, \DateTimeInterface $scheduledAt, \DateTimeInterface $deletedAt = null) {
|
||||
$task = new ScheduledTaskEntity();
|
||||
$task->setType($type);
|
||||
$task->setStatus($status);
|
||||
$task->setScheduledAt($scheduledAt);
|
||||
|
||||
if ($deletedAt) {
|
||||
$task->setDeletedAt($deletedAt);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($task);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $task;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reschedules tasks created after plugin activation so that they don't block cron tasks in tests
|
||||
*/
|
||||
|
@@ -3,25 +3,30 @@
|
||||
namespace MailPoet\Newsletter\Sending;
|
||||
|
||||
use MailPoet\Entities\ScheduledTaskEntity;
|
||||
use MailPoet\Test\DataFactories\ScheduledTask as ScheduledTaskFactory;
|
||||
use MailPoetVendor\Carbon\Carbon;
|
||||
|
||||
class ScheduledTasksRepositoryTest extends \MailPoetTest {
|
||||
/** @var ScheduledTasksRepository */
|
||||
private $repository;
|
||||
|
||||
/** @var ScheduledTaskFactory */
|
||||
private $scheduledTaskFactory;
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
$this->cleanup();
|
||||
$this->repository = $this->diContainer->get(ScheduledTasksRepository::class);
|
||||
$this->scheduledTaskFactory = new ScheduledTaskFactory();
|
||||
}
|
||||
|
||||
public function testItCanGetDueTasks() {
|
||||
$this->createScheduledTask('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->subDay(), Carbon::now()); // deleted (should not be fetched)
|
||||
$this->createScheduledTask('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->addDay()); // scheduled in future (should not be fetched)
|
||||
$this->createScheduledTask('test', '', Carbon::now()->subDay()); // wrong status (should not be fetched)
|
||||
$expectedResult[] = $this->createScheduledTask('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->subDay()); // due (scheduled in past)
|
||||
$expectedResult[] = $this->createScheduledTask('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->subDay()); // due (scheduled in past)
|
||||
$this->createScheduledTask('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->subDay()); // due (scheduled in past)
|
||||
$this->scheduledTaskFactory->create('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->subDay(), Carbon::now()); // deleted (should not be fetched)
|
||||
$this->scheduledTaskFactory->create('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->addDay()); // scheduled in future (should not be fetched)
|
||||
$this->scheduledTaskFactory->create('test', '', Carbon::now()->subDay()); // wrong status (should not be fetched)
|
||||
$expectedResult[] = $this->scheduledTaskFactory->create('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->subDay()); // due (scheduled in past)
|
||||
$expectedResult[] = $this->scheduledTaskFactory->create('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->subDay()); // due (scheduled in past)
|
||||
$this->scheduledTaskFactory->create('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->subDay()); // due (scheduled in past)
|
||||
|
||||
$tasks = $this->repository->findDueByType('test', 2);
|
||||
$this->assertCount(2, $tasks);
|
||||
@@ -29,20 +34,20 @@ class ScheduledTasksRepositoryTest extends \MailPoetTest {
|
||||
}
|
||||
|
||||
public function testItCanGetRunningTasks() {
|
||||
$expectedResult[] = $this->createScheduledTask('test', null, Carbon::now()->subDay()); // running (scheduled in past)
|
||||
$this->createScheduledTask('test', null, Carbon::now()->subDay(), Carbon::now()); // deleted (should not be fetched)
|
||||
$this->createScheduledTask('test', null, Carbon::now()->addDay()); // scheduled in future (should not be fetched)
|
||||
$this->createScheduledTask('test', ScheduledTaskEntity::STATUS_COMPLETED, Carbon::now()->subDay()); // wrong status (should not be fetched)
|
||||
$expectedResult[] = $this->scheduledTaskFactory->create('test', null, Carbon::now()->subDay()); // running (scheduled in past)
|
||||
$this->scheduledTaskFactory->create('test', null, Carbon::now()->subDay(), Carbon::now()); // deleted (should not be fetched)
|
||||
$this->scheduledTaskFactory->create('test', null, Carbon::now()->addDay()); // scheduled in future (should not be fetched)
|
||||
$this->scheduledTaskFactory->create('test', ScheduledTaskEntity::STATUS_COMPLETED, Carbon::now()->subDay()); // wrong status (should not be fetched)
|
||||
|
||||
$tasks = $this->repository->findRunningByType('test', 10);
|
||||
$this->assertSame($expectedResult, $tasks);
|
||||
}
|
||||
|
||||
public function testItCanGetCompletedTasks() {
|
||||
$expectedResult[] = $this->createScheduledTask('test', ScheduledTaskEntity::STATUS_COMPLETED, Carbon::now()->subDay()); // completed (scheduled in past)
|
||||
$this->createScheduledTask('test', ScheduledTaskEntity::STATUS_COMPLETED, Carbon::now()->subDay(), Carbon::now()); // deleted (should not be fetched)
|
||||
$this->createScheduledTask('test', ScheduledTaskEntity::STATUS_COMPLETED, Carbon::now()->addDay()); // scheduled in future (should not be fetched)
|
||||
$this->createScheduledTask('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->subDay()); // wrong status (should not be fetched)
|
||||
$expectedResult[] = $this->scheduledTaskFactory->create('test', ScheduledTaskEntity::STATUS_COMPLETED, Carbon::now()->subDay()); // completed (scheduled in past)
|
||||
$this->scheduledTaskFactory->create('test', ScheduledTaskEntity::STATUS_COMPLETED, Carbon::now()->subDay(), Carbon::now()); // deleted (should not be fetched)
|
||||
$this->scheduledTaskFactory->create('test', ScheduledTaskEntity::STATUS_COMPLETED, Carbon::now()->addDay()); // scheduled in future (should not be fetched)
|
||||
$this->scheduledTaskFactory->create('test', ScheduledTaskEntity::STATUS_SCHEDULED, Carbon::now()->subDay()); // wrong status (should not be fetched)
|
||||
|
||||
$tasks = $this->repository->findCompletedByType('test', 10);
|
||||
$this->assertSame($expectedResult, $tasks);
|
||||
@@ -51,20 +56,4 @@ class ScheduledTasksRepositoryTest extends \MailPoetTest {
|
||||
public function cleanup() {
|
||||
$this->truncateEntity(ScheduledTaskEntity::class);
|
||||
}
|
||||
|
||||
private function createScheduledTask(string $type, ?string $status, \DateTimeInterface $scheduledAt, \DateTimeInterface $deletedAt = null) {
|
||||
$task = new ScheduledTaskEntity();
|
||||
$task->setType($type);
|
||||
$task->setStatus($status);
|
||||
$task->setScheduledAt($scheduledAt);
|
||||
|
||||
if ($deletedAt) {
|
||||
$task->setDeletedAt($deletedAt);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($task);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $task;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user