This commit replaces usages by Carbon::now or in case we need a timestamp it keeps current_time but adds the gtm parameter as true. [MAILPOET-6142]
110 lines
4.0 KiB
PHP
110 lines
4.0 KiB
PHP
<?php declare(strict_types = 1);
|
|
|
|
namespace MailPoet\Cron\Workers;
|
|
|
|
use MailPoet\Config\ServicesChecker;
|
|
use MailPoet\Cron\CronWorkerScheduler;
|
|
use MailPoet\Entities\ScheduledTaskEntity;
|
|
use MailPoet\Services\SubscribersCountReporter;
|
|
use MailPoetVendor\Carbon\Carbon;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
/**
|
|
* Note: The test is written as a unit test but it can't be moved to unit test test suite because
|
|
* the parent class (SimpleWorker) requires DB connection and some dependencies can't be mocked.
|
|
* We can move the test to the unit test after SimpleWorker is fully refactored to DI
|
|
*/
|
|
class SubscribersStatsReportTest extends \MailPoetTest {
|
|
|
|
/** @var SubscribersStatsReport */
|
|
private $worker;
|
|
|
|
/** @var SubscribersCountReporter & MockObject */
|
|
private $countReporerMock;
|
|
|
|
/** @var CronWorkerScheduler & MockObject */
|
|
private $schedulerMock;
|
|
|
|
/** @var ServicesChecker & MockObject */
|
|
private $servicesCheckerMock;
|
|
|
|
public function _before() {
|
|
parent::_before();
|
|
$this->countReporerMock = $this->createMock(SubscribersCountReporter::class);
|
|
$this->schedulerMock = $this->createMock(CronWorkerScheduler::class);
|
|
$this->servicesCheckerMock = $this->createMock(ServicesChecker::class);
|
|
$this->worker = new SubscribersStatsReport(
|
|
$this->countReporerMock,
|
|
$this->servicesCheckerMock,
|
|
$this->schedulerMock,
|
|
);
|
|
}
|
|
|
|
public function testItFailsRequirementsCheckIfThereIsNoValidKey() {
|
|
$this->servicesCheckerMock->expects($this->once())
|
|
->method('getValidAccountKey')
|
|
->willReturn(null);
|
|
verify($this->worker->checkProcessingRequirements())->false();
|
|
}
|
|
|
|
public function testItSucceedsRequirementsCheckIfThereIsValidKey() {
|
|
$this->servicesCheckerMock->expects($this->once())
|
|
->method('getValidAccountKey')
|
|
->willReturn('a_valid_key');
|
|
verify($this->worker->checkProcessingRequirements())->true();
|
|
}
|
|
|
|
public function testItReportsCountToBridge() {
|
|
$task = new ScheduledTaskEntity();
|
|
$timer = time();
|
|
$this->servicesCheckerMock->expects($this->once())
|
|
->method('getValidAccountKey')
|
|
->willReturn('a_valid_key');
|
|
$this->countReporerMock->expects($this->once())
|
|
->method('report')
|
|
->willReturn(true);
|
|
verify($this->worker->processTaskStrategy($task, $timer))->true();
|
|
}
|
|
|
|
public function testItDontReportCountToBridgeIfThereIsNoValidKey() {
|
|
$task = new ScheduledTaskEntity();
|
|
$timer = time();
|
|
$this->servicesCheckerMock->expects($this->once())
|
|
->method('getValidAccountKey')
|
|
->willReturn(null);
|
|
$this->countReporerMock->expects($this->never())
|
|
->method('report');
|
|
verify($this->worker->processTaskStrategy($task, $timer))->false();
|
|
}
|
|
|
|
public function testItRescheduleTaskInCaseTheStatsReportFailed() {
|
|
$task = new ScheduledTaskEntity();
|
|
$timer = time();
|
|
$this->servicesCheckerMock->expects($this->once())
|
|
->method('getValidAccountKey')
|
|
->willReturn('a_valid_key');
|
|
$this->countReporerMock->expects($this->once())
|
|
->method('report')
|
|
->willReturn(false);
|
|
$this->schedulerMock->expects($this->once())
|
|
->method('rescheduleProgressively');
|
|
verify($this->worker->processTaskStrategy($task, $timer))->false();
|
|
}
|
|
|
|
public function testItGeneratesRandomNextRunDate() {
|
|
$time = time();
|
|
Carbon::setTestNow(Carbon::createFromTimestamp($time));
|
|
$maxExpectedScheduleInterval = 30 * 60 * 60; // 30 hours
|
|
$result = $this->worker->getNextRunDate();
|
|
verify($result)->instanceOf(Carbon::class);
|
|
verify($result->getTimestamp())->greaterThan($time);
|
|
verify($result->getTimestamp())->lessThan($time + $maxExpectedScheduleInterval);
|
|
$result2 = $this->worker->getNextRunDate();
|
|
verify($result2)->instanceOf(Carbon::class);
|
|
verify($result2->getTimestamp())->greaterThan($time);
|
|
verify($result2->getTimestamp())->lessThan($time + $maxExpectedScheduleInterval);
|
|
verify($result2->getTimestamp())->notEquals($result->getTimestamp());
|
|
Carbon::setTestNow();
|
|
}
|
|
}
|