CronCommandTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cron\Test\Unit\Console\Command;
  7. use Magento\Cron\Console\Command\CronCommand;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\App\ObjectManagerFactory;
  10. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  11. use Symfony\Component\Console\Tester\CommandTester;
  12. class CronCommandTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var ObjectManagerFactory|MockObject
  16. */
  17. private $objectManagerFactory;
  18. /**
  19. * @var DeploymentConfig|MockObject
  20. */
  21. private $deploymentConfigMock;
  22. protected function setUp()
  23. {
  24. $this->objectManagerFactory = $this->createMock(ObjectManagerFactory::class);
  25. $this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
  26. }
  27. /**
  28. * Test command with disables cron
  29. *
  30. * @return void
  31. */
  32. public function testExecuteWithDisabledCrons()
  33. {
  34. $this->objectManagerFactory->expects($this->never())
  35. ->method('create');
  36. $this->deploymentConfigMock->expects($this->once())
  37. ->method('get')
  38. ->with('cron/enabled', 1)
  39. ->willReturn(0);
  40. $commandTester = new CommandTester(
  41. new CronCommand($this->objectManagerFactory, $this->deploymentConfigMock)
  42. );
  43. $commandTester->execute([]);
  44. $expectedMsg = 'Cron is disabled. Jobs were not run.' . PHP_EOL;
  45. $this->assertEquals($expectedMsg, $commandTester->getDisplay());
  46. }
  47. /**
  48. * Test command with enabled cron
  49. *
  50. * @return void
  51. */
  52. public function testExecute()
  53. {
  54. $objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  55. $cron = $this->createMock(\Magento\Framework\App\Cron::class);
  56. $objectManager->expects($this->once())
  57. ->method('create')
  58. ->willReturn($cron);
  59. $cron->expects($this->once())
  60. ->method('launch');
  61. $this->objectManagerFactory->expects($this->once())
  62. ->method('create')
  63. ->willReturn($objectManager);
  64. $this->deploymentConfigMock->expects($this->once())
  65. ->method('get')
  66. ->with('cron/enabled', 1)
  67. ->willReturn(1);
  68. $commandTester = new CommandTester(
  69. new CronCommand($this->objectManagerFactory, $this->deploymentConfigMock)
  70. );
  71. $commandTester->execute([]);
  72. $expectedMsg = 'Ran jobs by schedule.' . PHP_EOL;
  73. $this->assertEquals($expectedMsg, $commandTester->getDisplay());
  74. }
  75. }