Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CrontabManagerTest.php
Go to the documentation of this file.
1 <?php
8 
18 
19 class CrontabManagerTest extends \PHPUnit\Framework\TestCase
20 {
24  private $shellMock;
25 
29  private $filesystemMock;
30 
34  private $crontabManager;
35 
39  protected function setUp()
40  {
41  $this->shellMock = $this->getMockBuilder(ShellInterface::class)
42  ->getMockForAbstractClass();
43  $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
44  ->disableOriginalClone()
45  ->disableOriginalConstructor()
46  ->getMock();
47 
48  $this->crontabManager = new CrontabManager($this->shellMock, $this->filesystemMock);
49  }
50 
54  public function testGetTasksNoCrontab()
55  {
56  $exception = new \Exception('crontab: no crontab for user');
57  $localizedException = new LocalizedException(new Phrase('Some error'), $exception);
58 
59  $this->shellMock->expects($this->once())
60  ->method('execute')
61  ->with('crontab -l', [])
62  ->willThrowException($localizedException);
63 
64  $this->assertEquals([], $this->crontabManager->getTasks());
65  }
66 
73  public function testGetTasks($content, $tasks)
74  {
75  $this->shellMock->expects($this->once())
76  ->method('execute')
77  ->with('crontab -l', [])
78  ->willReturn($content);
79 
80  $this->assertEquals($tasks, $this->crontabManager->getTasks());
81  }
82 
86  public function getTasksDataProvider()
87  {
88  return [
89  [
90  'content' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
91  . CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
92  . '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
93  . CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
94  'tasks' => ['* * * * * /bin/php /var/www/magento/bin/magento cron:run'],
95  ],
96  [
97  'content' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
98  . CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
99  . '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
100  . '* * * * * /bin/php /var/www/magento/bin/magento setup:cron:run' . PHP_EOL
101  . CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
102  'tasks' => [
103  '* * * * * /bin/php /var/www/magento/bin/magento cron:run',
104  '* * * * * /bin/php /var/www/magento/bin/magento setup:cron:run',
105  ],
106  ],
107  [
108  'content' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL,
109  'tasks' => [],
110  ],
111  [
112  'content' => '',
113  'tasks' => [],
114  ],
115  ];
116  }
117 
124  {
125  $exception = new \Exception('Shell error');
126  $localizedException = new LocalizedException(new Phrase('Some error'), $exception);
127 
128  $this->shellMock->expects($this->at(0))
129  ->method('execute')
130  ->with('crontab -l', [])
131  ->willReturn('');
132 
133  $this->shellMock->expects($this->at(1))
134  ->method('execute')
135  ->with('echo "" | crontab -', [])
136  ->willThrowException($localizedException);
137 
138  $this->crontabManager->removeTasks();
139  }
140 
147  public function testRemoveTasks($contentBefore, $contentAfter)
148  {
149  $this->shellMock->expects($this->at(0))
150  ->method('execute')
151  ->with('crontab -l', [])
152  ->willReturn($contentBefore);
153 
154  $this->shellMock->expects($this->at(1))
155  ->method('execute')
156  ->with('echo "' . $contentAfter . '" | crontab -', []);
157 
158  $this->crontabManager->removeTasks();
159  }
160 
164  public function removeTasksDataProvider()
165  {
166  return [
167  [
168  'contentBefore' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
169  . CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
170  . '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
171  . CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
172  'contentAfter' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
173  ],
174  [
175  'contentBefore' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
176  . CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
177  . '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
178  . '* * * * * /bin/php /var/www/magento/bin/magento setup:cron:run' . PHP_EOL
179  . CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
180  'contentAfter' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
181  ],
182  [
183  'contentBefore' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL,
184  'contentAfter' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
185  ],
186  [
187  'contentBefore' => '',
188  'contentAfter' => ''
189  ],
190  ];
191  }
192 
199  {
200  $baseDirMock = $this->getMockBuilder(ReadInterface::class)
201  ->getMockForAbstractClass();
202  $baseDirMock->expects($this->never())
203  ->method('getAbsolutePath')
204  ->willReturn('/var/www/magento2/');
205  $logDirMock = $this->getMockBuilder(ReadInterface::class)
206  ->getMockForAbstractClass();
207  $logDirMock->expects($this->never())
208  ->method('getAbsolutePath');
209 
210  $this->filesystemMock->expects($this->any())
211  ->method('getDirectoryRead')
212  ->willReturnMap([
213  [DirectoryList::ROOT, DriverPool::FILE, $baseDirMock],
214  [DirectoryList::LOG, DriverPool::FILE, $logDirMock],
215  ]);
216 
217  $this->crontabManager->saveTasks([]);
218  }
219 
225  public function testSaveTasksWithoutCommand()
226  {
227  $baseDirMock = $this->getMockBuilder(ReadInterface::class)
228  ->getMockForAbstractClass();
229  $baseDirMock->expects($this->once())
230  ->method('getAbsolutePath')
231  ->willReturn('/var/www/magento2/');
232  $logDirMock = $this->getMockBuilder(ReadInterface::class)
233  ->getMockForAbstractClass();
234  $logDirMock->expects($this->once())
235  ->method('getAbsolutePath')
236  ->willReturn('/var/www/magento2/var/log/');
237 
238  $this->filesystemMock->expects($this->any())
239  ->method('getDirectoryRead')
240  ->willReturnMap([
241  [DirectoryList::ROOT, DriverPool::FILE, $baseDirMock],
242  [DirectoryList::LOG, DriverPool::FILE, $logDirMock],
243  ]);
244 
245  $this->crontabManager->saveTasks([
246  'myCron' => ['expression' => '* * * * *']
247  ]);
248  }
249 
257  public function testSaveTasks($tasks, $content, $contentToSave)
258  {
259  $baseDirMock = $this->getMockBuilder(ReadInterface::class)
260  ->getMockForAbstractClass();
261  $baseDirMock->expects($this->once())
262  ->method('getAbsolutePath')
263  ->willReturn('/var/www/magento2/');
264  $logDirMock = $this->getMockBuilder(ReadInterface::class)
265  ->getMockForAbstractClass();
266  $logDirMock->expects($this->once())
267  ->method('getAbsolutePath')
268  ->willReturn('/var/www/magento2/var/log/');
269 
270  $this->filesystemMock->expects($this->any())
271  ->method('getDirectoryRead')
272  ->willReturnMap([
273  [DirectoryList::ROOT, DriverPool::FILE, $baseDirMock],
274  [DirectoryList::LOG, DriverPool::FILE, $logDirMock],
275  ]);
276 
277  $this->shellMock->expects($this->at(0))
278  ->method('execute')
279  ->with('crontab -l', [])
280  ->willReturn($content);
281 
282  $this->shellMock->expects($this->at(1))
283  ->method('execute')
284  ->with('echo "' . $contentToSave . '" | crontab -', []);
285 
286  $this->crontabManager->saveTasks($tasks);
287  }
288 
292  public function saveTasksDataProvider()
293  {
294  $content = '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
295  . CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
296  . '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
297  . CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL;
298 
299  return [
300  [
301  'tasks' => [
302  ['expression' => '* * * * *', 'command' => 'run.php']
303  ],
304  'content' => $content,
305  'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
306  . CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
307  . '* * * * * ' . PHP_BINARY . ' run.php' . PHP_EOL
308  . CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
309  ],
310  [
311  'tasks' => [
312  ['expression' => '1 2 3 4 5', 'command' => 'run.php']
313  ],
314  'content' => $content,
315  'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
316  . CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
317  . '1 2 3 4 5 ' . PHP_BINARY . ' run.php' . PHP_EOL
318  . CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
319  ],
320  [
321  'tasks' => [
322  ['command' => '{magentoRoot}run.php >> {magentoLog}cron.log']
323  ],
324  'content' => $content,
325  'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
326  . CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
327  . '* * * * * ' . PHP_BINARY . ' /var/www/magento2/run.php >>'
328  . ' /var/www/magento2/var/log/cron.log' . PHP_EOL
329  . CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
330  ],
331  [
332  'tasks' => [
333  ['command' => '{magentoRoot}run.php % cron:run | grep -v "Ran \'jobs\' by schedule"']
334  ],
335  'content' => $content,
336  'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
337  . CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
338  . '* * * * * ' . PHP_BINARY . ' /var/www/magento2/run.php'
339  . ' %% cron:run | grep -v \"Ran \'jobs\' by schedule\"' . PHP_EOL
340  . CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
341  ],
342  [
343  'tasks' => [
344  ['command' => '{magentoRoot}run.php % cron:run | grep -v "Ran \'jobs\' by schedule"']
345  ],
346  'content' => '* * * * * /bin/php /var/www/cron.php',
347  'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
348  . CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
349  . '* * * * * ' . PHP_BINARY . ' /var/www/magento2/run.php'
350  . ' %% cron:run | grep -v \"Ran \'jobs\' by schedule\"' . PHP_EOL
351  . CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
352  ],
353  ];
354  }
355 }
const BP
Definition: autoload.php:14