CronInstallCommandTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 Symfony\Component\Console\Tester\CommandTester;
  8. use Magento\Cron\Console\Command\CronInstallCommand;
  9. use Magento\Framework\Crontab\CrontabManagerInterface;
  10. use Magento\Framework\Crontab\TasksProviderInterface;
  11. use Magento\Framework\Console\Cli;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\Phrase;
  14. class CronInstallCommandTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var CrontabManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $crontabManagerMock;
  20. /**
  21. * @var TasksProviderInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $tasksProviderMock;
  24. /**
  25. * @var CommandTester
  26. */
  27. private $commandTester;
  28. /**
  29. * @return void
  30. */
  31. protected function setUp()
  32. {
  33. $this->crontabManagerMock = $this->getMockBuilder(CrontabManagerInterface::class)
  34. ->getMockForAbstractClass();
  35. $this->tasksProviderMock = $this->getMockBuilder(TasksProviderInterface::class)
  36. ->getMockForAbstractClass();
  37. $this->commandTester = new CommandTester(
  38. new CronInstallCommand($this->crontabManagerMock, $this->tasksProviderMock)
  39. );
  40. }
  41. /**
  42. * @return void
  43. */
  44. public function testExecuteAlreadyInstalled()
  45. {
  46. $this->crontabManagerMock->expects($this->once())
  47. ->method('getTasks')
  48. ->willReturn([['* * * * * /bin/php /var/run.php']]);
  49. $this->tasksProviderMock->expects($this->never())
  50. ->method('getTasks');
  51. $this->commandTester->execute([]);
  52. $this->assertEquals(
  53. 'Crontab has already been generated and saved' . PHP_EOL,
  54. $this->commandTester->getDisplay()
  55. );
  56. $this->assertEquals(Cli::RETURN_FAILURE, $this->commandTester->getStatusCode());
  57. }
  58. /**
  59. * @return void
  60. */
  61. public function testExecuteWithException()
  62. {
  63. $this->crontabManagerMock->expects($this->once())
  64. ->method('getTasks')
  65. ->willReturn([]);
  66. $this->tasksProviderMock->expects($this->once())
  67. ->method('getTasks')
  68. ->willReturn([]);
  69. $this->crontabManagerMock->expects($this->once())
  70. ->method('saveTasks')
  71. ->willThrowException(new LocalizedException(new Phrase('Some error')));
  72. $this->commandTester->execute([]);
  73. $this->assertEquals(
  74. 'Some error' . PHP_EOL,
  75. $this->commandTester->getDisplay()
  76. );
  77. $this->assertEquals(Cli::RETURN_FAILURE, $this->commandTester->getStatusCode());
  78. }
  79. /**
  80. * @param array $existingTasks
  81. * @param array $options
  82. * @return void
  83. * @dataProvider executeDataProvider
  84. */
  85. public function testExecute($existingTasks, $options)
  86. {
  87. $this->crontabManagerMock->expects($this->once())
  88. ->method('getTasks')
  89. ->willReturn($existingTasks);
  90. $this->tasksProviderMock->expects($this->once())
  91. ->method('getTasks')
  92. ->willReturn([]);
  93. $this->crontabManagerMock->expects($this->once())
  94. ->method('saveTasks')
  95. ->with([]);
  96. $this->commandTester->execute($options);
  97. $this->assertEquals(
  98. 'Crontab has been generated and saved' . PHP_EOL,
  99. $this->commandTester->getDisplay()
  100. );
  101. $this->assertEquals(Cli::RETURN_SUCCESS, $this->commandTester->getStatusCode());
  102. }
  103. /**
  104. * @return array
  105. */
  106. public function executeDataProvider()
  107. {
  108. return [
  109. ['existingTasks' => [], 'options' => []],
  110. ['existingTasks' => ['* * * * * /bin/php /var/www/run.php'], 'options' => ['-f'=> true]]
  111. ];
  112. }
  113. }