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 */ public function testGetList() { $field = 'name'; $value = 'magento'; $condition = 'eq'; $total = 10; $currentPage = 3; $pageSize = 2; $sortField = 'id'; $criteria = $this->getMockBuilder('Magento\\Framework\\Api\\SearchCriteriaInterface')->getMock(); $filterGroup = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\FilterGroup')->getMock(); $filter = $this->getMockBuilder('Magento\\Framework\\Api\\Filter')->getMock(); $storeFilter = $this->getMockBuilder('Magento\\Framework\\Api\\Filter')->getMock(); $sortOrder = $this->getMockBuilder('Magento\\Framework\\Api\\SortOrder')->getMock(); $criteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]); $criteria->expects($this->once())->method('getSortOrders')->willReturn([$sortOrder]); $criteria->expects($this->once())->method('getCurrentPage')->willReturn($currentPage); $criteria->expects($this->once())->method('getPageSize')->willReturn($pageSize); $filterGroup->expects($this->once())->method('getFilters')->willReturn([$storeFilter, $filter]); $filter->expects($this->once())->method('getConditionType')->willReturn($condition); $filter->expects($this->any())->method('getField')->willReturn($field); $filter->expects($this->once())->method('getValue')->willReturn($value); $storeFilter->expects($this->any())->method('getField')->willReturn('store_id'); $storeFilter->expects($this->once())->method('getValue')->willReturn(1); $sortOrder->expects($this->once())->method('getField')->willReturn($sortField); $sortOrder->expects($this->once())->method('getDirection')->willReturn(SortOrder::SORT_DESC); /** @var \Magento\Framework\Api\SearchCriteriaInterface $criteria */ $this->collection->addItem($this->block); $this->blockSearchResult->expects($this->once())->method('setSearchCriteria')->with($criteria)->willReturnSelf(); $this->collection->expects($this->once())->method('addFieldToFilter')->with($field, [$condition => $value])->willReturnSelf(); $this->blockSearchResult->expects($this->once())->method('setTotalCount')->with($total)->willReturnSelf(); $this->collection->expects($this->once())->method('getSize')->willReturn($total); $this->collection->expects($this->once())->method('setCurPage')->with($currentPage)->willReturnSelf(); $this->collection->expects($this->once())->method('setPageSize')->with($pageSize)->willReturnSelf(); $this->collection->expects($this->once())->method('addOrder')->with($sortField, 'DESC')->willReturnSelf(); $this->block->expects($this->once())->method('getData')->willReturn(['data']); $this->blockSearchResult->expects($this->once())->method('setItems')->with(['someData'])->willReturnSelf(); $this->dataHelper->expects($this->once())->method('populateWithArray')->with($this->blockData, ['data'], 'Magento\\Cms\\Api\\Data\\BlockInterface'); $this->dataObjectProcessor->expects($this->once())->method('buildOutputDataArray')->with($this->blockData, 'Magento\\Cms\\Api\\Data\\BlockInterface')->willReturn('someData'); $this->assertEquals($this->blockSearchResult, $this->repository->getList($criteria)); }
/** * 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(''); }