public function testCronRegistry() { Registry::register('test', '* * * * *', array($this, 'dummy'), array()); $expectedCronRegistry = array('test' => array('frequency' => '* * * * *', 'callback' => array($this, 'dummy'), 'args' => array())); $cronRegistry = Registry::getCronRegistry(); $this->assertSame($expectedCronRegistry, $cronRegistry); }
public function testProcess() { $past = time() - 100; // only past should run $job = $this->getJob(Repository\Job::STATUS_PENDING, $past); $cron = $this->getMock('Heartsentwined\\Cron\\Service\\Cron', array('getPending'))->setEm($this->em); $cron->expects($this->any())->method('getPending')->will($this->returnValue(array($job))); $dummy = $this->getDummy(); $dummy->expects($this->once())->method('run'); $cron->register('time', '* * * * *', array($dummy, 'run'), array()); $cron->process(); $this->assertSame(Repository\Job::STATUS_SUCCESS, $job->getStatus()); $this->assertSame(null, $job->getErrorMsg()); $this->assertSame(null, $job->getStackTrace()); $this->assertNotEmpty($job->getExecuteTime()); $this->assertNotEmpty($job->getFinishTime()); // future should not run $job = $this->getJob(Repository\Job::STATUS_PENDING, time() + 100); $cron = $this->getMock('Heartsentwined\\Cron\\Service\\Cron', array('getPending'))->setEm($this->em); $cron->expects($this->any())->method('getPending')->will($this->returnValue(array($job))); $dummy = $this->getDummy(); $dummy->expects($this->never())->method('run'); $cron->register('time', '* * * * *', array($dummy, 'run'), array()); $cron->process(); $this->assertSame(Repository\Job::STATUS_PENDING, $job->getStatus()); $this->assertSame(null, $job->getErrorMsg()); $this->assertSame(null, $job->getStackTrace()); $this->assertNull($job->getExecuteTime()); $this->assertNull($job->getFinishTime()); // cron job throwing exceptions $job = $this->getJob(Repository\Job::STATUS_PENDING, $past); $cron = $this->getMock('Heartsentwined\\Cron\\Service\\Cron', array('getPending'))->setEm($this->em); $cron->expects($this->any())->method('getPending')->will($this->returnValue(array($job))); $dummy = $this->getDummy(); $dummy->expects($this->once())->method('run')->will($this->throwException(new \RuntimeException('foo runtime exception'))); $cron->register('time', '* * * * *', array($dummy, 'run'), array()); $cron->process(); $this->assertSame(Repository\Job::STATUS_ERROR, $job->getStatus()); $this->assertSame('foo runtime exception', $job->getErrorMsg()); $this->assertNotEmpty($job->getStackTrace()); $this->assertNotEmpty($job->getExecuteTime()); $this->assertNull($job->getFinishTime()); // too late for job $job = $this->getJob(Repository\Job::STATUS_PENDING, $past); $cron = $this->getMock('Heartsentwined\\Cron\\Service\\Cron', array('getPending'))->setScheduleLifetime(0)->setEm($this->em); $cron->expects($this->any())->method('getPending')->will($this->returnValue(array($job))); $dummy = $this->getDummy(); $dummy->expects($this->never())->method('run'); $cron->register('time', '* * * * *', array($dummy, 'run'), array()); $cron->process(); $this->assertSame(Repository\Job::STATUS_MISSED, $job->getStatus()); $this->assertSame('too late for job', $job->getErrorMsg()); $this->assertNotEmpty($job->getStackTrace()); $this->assertNull($job->getExecuteTime()); $this->assertNull($job->getFinishTime()); // job not registered $job = $this->getJob(Repository\Job::STATUS_PENDING, $past); $cron = $this->getMock('Heartsentwined\\Cron\\Service\\Cron', array('getPending'))->setEm($this->em); $cron->expects($this->any())->method('getPending')->will($this->returnValue(array($job))); $dummy = $this->getDummy(); $dummy->expects($this->never())->method('run'); Registry::destroy(); $cron->process(); $this->assertSame(Repository\Job::STATUS_ERROR, $job->getStatus()); $this->assertSame('job "time" undefined in cron registry', $job->getErrorMsg()); $this->assertNotEmpty($job->getStackTrace()); $this->assertNull($job->getExecuteTime()); $this->assertNull($job->getFinishTime()); }
/** * wrapper function * @see Registry::register() */ public static function register($code, $frequency, $callback, array $args = array()) { Registry::register($code, $frequency, $callback, $args); }