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(); } }