CronTest.php 3.4 KB

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