Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CronInstallCommandTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Symfony\Component\Console\Tester\CommandTester;
15 
16 class CronInstallCommandTest extends \PHPUnit\Framework\TestCase
17 {
21  private $crontabManagerMock;
22 
26  private $tasksProviderMock;
27 
31  private $commandTester;
32 
36  protected function setUp()
37  {
38  $this->crontabManagerMock = $this->getMockBuilder(CrontabManagerInterface::class)
39  ->getMockForAbstractClass();
40  $this->tasksProviderMock = $this->getMockBuilder(TasksProviderInterface::class)
41  ->getMockForAbstractClass();
42 
43  $this->commandTester = new CommandTester(
44  new CronInstallCommand($this->crontabManagerMock, $this->tasksProviderMock)
45  );
46  }
47 
51  public function testExecuteAlreadyInstalled()
52  {
53  $this->crontabManagerMock->expects($this->once())
54  ->method('getTasks')
55  ->willReturn([['* * * * * /bin/php /var/run.php']]);
56  $this->tasksProviderMock->expects($this->never())
57  ->method('getTasks');
58 
59  $this->commandTester->execute([]);
60  $this->assertEquals(
61  'Crontab has already been generated and saved' . PHP_EOL,
62  $this->commandTester->getDisplay()
63  );
64  $this->assertEquals(Cli::RETURN_FAILURE, $this->commandTester->getStatusCode());
65  }
66 
70  public function testExecuteWithException()
71  {
72  $this->crontabManagerMock->expects($this->once())
73  ->method('getTasks')
74  ->willReturn([]);
75  $this->tasksProviderMock->expects($this->once())
76  ->method('getTasks')
77  ->willReturn([]);
78  $this->crontabManagerMock->expects($this->once())
79  ->method('saveTasks')
80  ->willThrowException(new LocalizedException(new Phrase('Some error')));
81 
82  $this->commandTester->execute([]);
83  $this->assertEquals(
84  'Some error' . PHP_EOL,
85  $this->commandTester->getDisplay()
86  );
87  $this->assertEquals(Cli::RETURN_FAILURE, $this->commandTester->getStatusCode());
88  }
89 
96  public function testExecute($existingTasks, $options)
97  {
98  $this->crontabManagerMock->expects($this->once())
99  ->method('getTasks')
100  ->willReturn($existingTasks);
101  $this->tasksProviderMock->expects($this->once())
102  ->method('getTasks')
103  ->willReturn([]);
104  $this->crontabManagerMock->expects($this->once())
105  ->method('saveTasks')
106  ->with([]);
107 
108  $this->commandTester->execute($options);
109  $this->assertEquals(
110  'Crontab has been generated and saved' . PHP_EOL,
111  $this->commandTester->getDisplay()
112  );
113  $this->assertEquals(Cli::RETURN_SUCCESS, $this->commandTester->getStatusCode());
114  }
115 
119  public function executeDataProvider()
120  {
121  return [
122  ['existingTasks' => [], 'options' => []],
123  ['existingTasks' => ['* * * * * /bin/php /var/www/run.php'], 'options' => ['-f'=> true]]
124  ];
125  }
126 }