protected function setUp()
 {
     \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\App\\AreaList')->getArea('crontab')->load(\Magento\Framework\App\Area::PART_CONFIG);
     $request = Bootstrap::getObjectManager()->create('Magento\\Framework\\App\\Console\\Request');
     $request->setParams(['group' => 'default', 'standaloneProcessStarted' => '0']);
     $this->_model = Bootstrap::getObjectManager()->create('Magento\\Cron\\Observer\\ProcessCronQueueObserver', ['request' => $request]);
     $this->_model->execute(new \Magento\Framework\Event\Observer());
 }
 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);
 }