Beispiel #1
0
 /**
  * Test method call
  */
 public function testGetJobs()
 {
     $jobList = ['jobname1' => ['instance' => 'TestInstance', 'method' => 'testMethod', 'schedule' => '* * * * *']];
     $this->_configData->expects($this->once())->method('getJobs')->will($this->returnValue($jobList));
     $result = $this->_config->getJobs();
     $this->assertEquals($jobList, $result);
 }
Beispiel #2
0
 public function execute()
 {
     if (!$this->_isAllowed()) {
         echo $this->__('Access Denied');
         return;
     }
     $SCHEDULE_EVERY_MINUTES = 30;
     //Flooring the minutes
     $startTimeSeconds = (int) ($this->_timezone->scopeTimeStamp() / 60) * 60;
     //Ceiling to the next 5 minutes
     $startTimeMinutes = $startTimeSeconds / 60;
     $startTimeMinutes = (int) ($startTimeMinutes / 5) * 5 + 5;
     $startTimeSeconds = $startTimeMinutes * 60;
     $jobs = $this->_cronConfig->getJobs();
     if (isset($jobs[self::CRON_GROUP])) {
         $i = 0;
         foreach ($jobs[self::CRON_GROUP] as $jobCode => $jobConfig) {
             if (strpos($jobCode, self::CRON_JOB) === false) {
                 continue;
             }
             $timecreated = strftime('%Y-%m-%d %H:%M:%S', $this->_timezone->scopeTimeStamp());
             $timescheduled = strftime('%Y-%m-%d %H:%M:%S', $startTimeSeconds + $i * 60 * $SCHEDULE_EVERY_MINUTES);
             try {
                 $lastItem = $this->_cronSchedule->getCollection()->addFieldToFilter('job_code', 'celebros_export')->addFieldToFilter('scheduled_at', $timescheduled)->getLastItem();
                 if (!$lastItem->getScheduleId()) {
                     $this->_cronSchedule->setJobCode($jobCode)->setCreatedAt($timecreated)->setScheduledAt($timescheduled)->setStatus('pending')->save();
                     echo "{$jobCode} cron job is scheduled at {$timescheduled} <br/>";
                 } else {
                     echo "{$jobCode} cron job are already exist at {$timescheduled} <br/>";
                 }
             } catch (\Exception $e) {
                 throw new \Exception(__('Unable to schedule Cron'));
             }
             $i++;
         }
     }
 }
 public function testMissedJobsCleanedInTime()
 {
     /* 1. Initialize dependencies of _generate() method which is called first */
     $jobConfig = ['test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']]];
     // This item was scheduled 2 days ago
     /** @var \Magento\Cron\Model\Schedule|\PHPUnit_Framework_MockObject_MockObject $schedule1 */
     $schedule1 = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->setMethods(['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup'])->getMock();
     $schedule1->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null));
     $schedule1->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-2 day -2 hour'));
     $schedule1->expects($this->any())->method('getStatus')->will($this->returnValue(Schedule::STATUS_MISSED));
     //we expect this job be deleted from the list
     $schedule1->expects($this->once())->method('delete')->will($this->returnValue(true));
     // This item was scheduled 1 day ago
     $schedule2 = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->setMethods(['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup'])->getMock();
     $schedule2->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null));
     $schedule2->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-1 day'));
     $schedule2->expects($this->any())->method('getStatus')->will($this->returnValue(Schedule::STATUS_MISSED));
     //we don't expect this job be deleted from the list
     $schedule2->expects($this->never())->method('delete');
     $this->_collection->addItem($schedule1);
     $this->_config->expects($this->once())->method('getJobs')->will($this->returnValue($jobConfig));
     //get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_generate()"
     $this->_cache->expects($this->at(0))->method('load')->will($this->returnValue(time() + 10000000));
     //get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_cleanup()"
     $this->_cache->expects($this->at(1))->method('load')->will($this->returnValue(time() - 10000000));
     $this->_scopeConfig->expects($this->at(0))->method('getValue')->with($this->equalTo('system/cron/test_group/use_separate_process'))->will($this->returnValue(0));
     $this->_scopeConfig->expects($this->at(1))->method('getValue')->with($this->equalTo('system/cron/test_group/schedule_generate_every'))->will($this->returnValue(0));
     $this->_scopeConfig->expects($this->at(2))->method('getValue')->with($this->equalTo('system/cron/test_group/history_cleanup_every'))->will($this->returnValue(0));
     $this->_scopeConfig->expects($this->at(3))->method('getValue')->with($this->equalTo('system/cron/test_group/schedule_lifetime'))->will($this->returnValue(2 * 24 * 60));
     $this->_scopeConfig->expects($this->at(4))->method('getValue')->with($this->equalTo('system/cron/test_group/history_success_lifetime'))->will($this->returnValue(0));
     $this->_scopeConfig->expects($this->at(5))->method('getValue')->with($this->equalTo('system/cron/test_group/history_failure_lifetime'))->will($this->returnValue(0));
     /* 2. Initialize dependencies of _cleanup() method which is called second */
     $scheduleMock = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->getMock();
     $scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
     $this->_scheduleFactory->expects($this->at(0))->method('create')->will($this->returnValue($scheduleMock));
     $collection = $this->getMockBuilder('Magento\\Cron\\Model\\ResourceModel\\Schedule\\Collection')->setMethods(['addFieldToFilter', 'load', '__wakeup'])->disableOriginalConstructor()->getMock();
     $collection->expects($this->any())->method('addFieldToFilter')->will($this->returnSelf());
     $collection->expects($this->any())->method('load')->will($this->returnSelf());
     $collection->addItem($schedule1);
     $collection->addItem($schedule2);
     $scheduleMock = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->getMock();
     $scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($collection));
     $this->_scheduleFactory->expects($this->at(1))->method('create')->will($this->returnValue($scheduleMock));
     $this->_observer->execute($this->observer);
 }
 /**
  * Test case without saved cron jobs in data base
  */
 public function testDispatchCleanup()
 {
     $jobConfig = ['test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']]];
     $schedule = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->setMethods(['getExecutedAt', 'getStatus', 'delete', '__wakeup'])->getMock();
     $schedule->expects($this->any())->method('getExecutedAt')->will($this->returnValue('-1 day'));
     $schedule->expects($this->any())->method('getStatus')->will($this->returnValue('success'));
     $this->_collection->addItem($schedule);
     $this->_config->expects($this->once())->method('getJobs')->will($this->returnValue($jobConfig));
     $this->_cache->expects($this->at(0))->method('load')->will($this->returnValue(time() + 10000000));
     $this->_cache->expects($this->at(1))->method('load')->will($this->returnValue(time() - 10000000));
     $this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
     $scheduleMock = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->getMock();
     $scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
     $this->_scheduleFactory->expects($this->at(0))->method('create')->will($this->returnValue($scheduleMock));
     $collection = $this->getMockBuilder('Magento\\Cron\\Model\\Resource\\Schedule\\Collection')->setMethods(['addFieldToFilter', 'load', '__wakeup'])->disableOriginalConstructor()->getMock();
     $collection->expects($this->any())->method('addFieldToFilter')->will($this->returnSelf());
     $collection->expects($this->any())->method('load')->will($this->returnSelf());
     $collection->addItem($schedule);
     $scheduleMock = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->getMock();
     $scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($collection));
     $this->_scheduleFactory->expects($this->at(1))->method('create')->will($this->returnValue($scheduleMock));
     $this->_observer->dispatch('');
 }