Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CronTest.php
Go to the documentation of this file.
1 <?php
7 
9 use \Magento\Framework\App\Cron;
10 
11 class CronTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $_model;
17 
21  protected $_configScopeMock;
22 
26  protected $_stateMock;
27 
31  protected $_request;
32 
36  protected $_responseMock;
37 
41  private $objectManager;
42 
43  protected function setUp()
44  {
45  $this->_stateMock = $this->createMock(\Magento\Framework\App\State::class);
46  $this->_request = $this->createMock(\Magento\Framework\App\Console\Request::class);
47  $this->_responseMock = $this->createMock(\Magento\Framework\App\Console\Response::class);
48  $this->objectManager = $this->getMockForAbstractClass(\Magento\Framework\ObjectManagerInterface::class);
49  $this->_model = new Cron(
50  $this->_stateMock,
51  $this->_request,
52  $this->_responseMock,
53  $this->objectManager,
54  [],
55  $this->prepareAreaListMock()
56  );
57  }
58 
62  protected function prepareAreaListMock()
63  {
64  $areaMock = $this->createMock(\Magento\Framework\App\Area::class);
65  $areaMock->expects($this->once())
66  ->method('load')
67  ->with(Area::PART_TRANSLATE);
68 
69  $areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
70  $areaListMock->expects($this->any())
71  ->method('getArea')
72  ->with(Area::AREA_CRONTAB)
73  ->willReturn($areaMock);
74 
75  return $areaListMock;
76  }
77 
79  {
80  $configLoader = $this->getMockForAbstractClass(\Magento\Framework\ObjectManager\ConfigLoaderInterface::class);
81  $eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
82 
83  $this->objectManager->expects($this->any())
84  ->method('get')
85  ->will($this->returnValueMap([
86  [\Magento\Framework\ObjectManager\ConfigLoaderInterface::class, $configLoader],
87  [\Magento\Framework\Event\ManagerInterface::class, $eventManagerMock]
88  ]));
89  $crontabConfig = ['config'];
90  $configLoader->expects($this->once())
91  ->method('load')
92  ->with(Area::AREA_CRONTAB)
93  ->willReturn($crontabConfig);
94  $this->objectManager->expects($this->once())
95  ->method('configure')
96  ->with($crontabConfig);
97  $this->_stateMock->expects($this->once())->method('setAreaCode')->with(Area::AREA_CRONTAB);
98  $eventManagerMock->expects($this->once())->method('dispatch')->with('default');
99  $this->_responseMock->expects($this->once())->method('setCode')->with(0);
100  $this->assertEquals($this->_responseMock, $this->_model->launch());
101  }
102 }