1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App\Test\Unit;
use Magento\Framework\App\Area;
use \Magento\Framework\App\Cron;
class CronTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\App\Cron
*/
protected $_model;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $_configScopeMock;
/**
* @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_stateMock;
/**
* @var \Magento\Framework\App\Console\Request|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_request;
/**
* @var \Magento\Framework\App\Console\Response|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_responseMock;
/**
* @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $objectManager;
protected function setUp()
{
$this->_stateMock = $this->createMock(\Magento\Framework\App\State::class);
$this->_request = $this->createMock(\Magento\Framework\App\Console\Request::class);
$this->_responseMock = $this->createMock(\Magento\Framework\App\Console\Response::class);
$this->objectManager = $this->getMockForAbstractClass(\Magento\Framework\ObjectManagerInterface::class);
$this->_model = new Cron(
$this->_stateMock,
$this->_request,
$this->_responseMock,
$this->objectManager,
[],
$this->prepareAreaListMock()
);
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function prepareAreaListMock()
{
$areaMock = $this->createMock(\Magento\Framework\App\Area::class);
$areaMock->expects($this->once())
->method('load')
->with(Area::PART_TRANSLATE);
$areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
$areaListMock->expects($this->any())
->method('getArea')
->with(Area::AREA_CRONTAB)
->willReturn($areaMock);
return $areaListMock;
}
public function testLaunchDispatchesCronEvent()
{
$configLoader = $this->getMockForAbstractClass(\Magento\Framework\ObjectManager\ConfigLoaderInterface::class);
$eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
$this->objectManager->expects($this->any())
->method('get')
->will($this->returnValueMap([
[\Magento\Framework\ObjectManager\ConfigLoaderInterface::class, $configLoader],
[\Magento\Framework\Event\ManagerInterface::class, $eventManagerMock]
]));
$crontabConfig = ['config'];
$configLoader->expects($this->once())
->method('load')
->with(Area::AREA_CRONTAB)
->willReturn($crontabConfig);
$this->objectManager->expects($this->once())
->method('configure')
->with($crontabConfig);
$this->_stateMock->expects($this->once())->method('setAreaCode')->with(Area::AREA_CRONTAB);
$eventManagerMock->expects($this->once())->method('dispatch')->with('default');
$this->_responseMock->expects($this->once())->method('setCode')->with(0);
$this->assertEquals($this->_responseMock, $this->_model->launch());
}
}