Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ScheduleTest.php
Go to the documentation of this file.
1 <?php
7 
9 
14 class ScheduleTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $helper;
20 
21  protected $resourceJobMock;
22 
23  protected function setUp()
24  {
25  $this->helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
26 
27  $this->resourceJobMock = $this->getMockBuilder(\Magento\Cron\Model\ResourceModel\Schedule::class)
28  ->disableOriginalConstructor()
29  ->setMethods(['trySetJobUniqueStatusAtomic', '__wakeup', 'getIdFieldName'])
30  ->getMockForAbstractClass();
31 
32  $this->resourceJobMock->expects($this->any())
33  ->method('getIdFieldName')
34  ->will($this->returnValue('id'));
35  }
36 
42  public function testSetCronExpr($cronExpression, $expected)
43  {
44  // 1. Create mocks
46  $model = $this->helper->getObject(\Magento\Cron\Model\Schedule::class);
47 
48  // 2. Run tested method
49  $model->setCronExpr($cronExpression);
50 
51  // 3. Compare actual result with expected result
52  $result = $model->getCronExprArr();
53  $this->assertEquals($result, $expected);
54  }
55 
64  public function setCronExprDataProvider()
65  {
66  return [
67  ['1 2 3 4 5', [1, 2, 3, 4, 5]],
68  ['1 2 3 4 5 6', [1, 2, 3, 4, 5, 6]],
69  ['a b c d e', ['a', 'b', 'c', 'd', 'e']], //should fail if validation will be added
70  ['* * * * *', ['*', '*', '*', '*', '*']],
71 
72  ['0 * * * *', ['0', '*', '*', '*', '*']],
73  ['59 * * * *', ['59', '*', '*', '*', '*']],
74  [', * * * *', [',', '*', '*', '*', '*']],
75  ['1-2 * * * *', ['1-2', '*', '*', '*', '*']],
76  ['0/5 * * * *', ['0/5', '*', '*', '*', '*']],
77 
78  ['* 0 * * *', ['*', '0', '*', '*', '*']],
79  ['* 59 * * *', ['*', '59', '*', '*', '*']],
80  ['* , * * *', ['*', ',', '*', '*', '*']],
81  ['* 1-2 * * *', ['*', '1-2', '*', '*', '*']],
82  ['* 0/5 * * *', ['*', '0/5', '*', '*', '*']],
83 
84  ['* * 0 * *', ['*', '*', '0', '*', '*']],
85  ['* * 23 * *', ['*', '*', '23', '*', '*']],
86  ['* * , * *', ['*', '*', ',', '*', '*']],
87  ['* * 1-2 * *', ['*', '*', '1-2', '*', '*']],
88  ['* * 0/5 * *', ['*', '*', '0/5', '*', '*']],
89 
90  ['* * * 1 *', ['*', '*', '*', '1', '*']],
91  ['* * * 31 *', ['*', '*', '*', '31', '*']],
92  ['* * * , *', ['*', '*', '*', ',', '*']],
93  ['* * * 1-2 *', ['*', '*', '*', '1-2', '*']],
94  ['* * * 0/5 *', ['*', '*', '*', '0/5', '*']],
95  ['* * * ? *', ['*', '*', '*', '?', '*']],
96  ['* * * L *', ['*', '*', '*', 'L', '*']],
97  ['* * * W *', ['*', '*', '*', 'W', '*']],
98  ['* * * C *', ['*', '*', '*', 'C', '*']],
99 
100  ['* * * * 0', ['*', '*', '*', '*', '0']],
101  ['* * * * 11', ['*', '*', '*', '*', '11']],
102  ['* * * * ,', ['*', '*', '*', '*', ',']],
103  ['* * * * 1-2', ['*', '*', '*', '*', '1-2']],
104  ['* * * * 0/5', ['*', '*', '*', '*', '0/5']],
105  ['* * * * JAN', ['*', '*', '*', '*', 'JAN']],
106  ['* * * * DEC', ['*', '*', '*', '*', 'DEC']],
107  ['* * * * JAN-DEC', ['*', '*', '*', '*', 'JAN-DEC']],
108 
109  ['* * * * * 1', ['*', '*', '*', '*', '*', '1']],
110  ['* * * * * 7', ['*', '*', '*', '*', '*', '7']],
111  ['* * * * * ,', ['*', '*', '*', '*', '*', ',']],
112  ['* * * * * 1-2', ['*', '*', '*', '*', '*', '1-2']],
113  ['* * * * * 0/5', ['*', '*', '*', '*', '*', '0/5']],
114  ['* * * * * ?', ['*', '*', '*', '*', '*', '?']],
115  ['* * * * * L', ['*', '*', '*', '*', '*', 'L']],
116  ['* * * * * 6#3', ['*', '*', '*', '*', '*', '6#3']],
117  ['* * * * * SUN', ['*', '*', '*', '*', '*', 'SUN']],
118  ['* * * * * SAT', ['*', '*', '*', '*', '*', 'SAT']],
119  ['* * * * * SUN-SAT', ['*', '*', '*', '*', '*', 'SUN-SAT']],
120  ];
121  }
122 
128  public function testSetCronExprException($cronExpression)
129  {
130  // 1. Create mocks
132  $model = $this->helper->getObject(\Magento\Cron\Model\Schedule::class);
133 
134  // 2. Run tested method
135  $model->setCronExpr($cronExpression);
136  }
137 
145  {
146  return [
147  [''],
148  [null],
149  [false],
150  ['1 2 3 4'],
151  ['1 2 3 4 5 6 7']
152  ];
153  }
154 
161  public function testTrySchedule($scheduledAt, $cronExprArr, $expected)
162  {
163  // 1. Create mocks
165  $model = $this->helper->getObject(
166  \Magento\Cron\Model\Schedule::class
167  );
168 
169  // 2. Set fixtures
170  $model->setScheduledAt($scheduledAt);
171  $model->setCronExprArr($cronExprArr);
172 
173  // 3. Run tested method
174  $result = $model->trySchedule();
175 
176  // 4. Compare actual result with expected result
177  $this->assertEquals($expected, $result);
178  }
179 
180  public function testTryScheduleWithConversionToAdminStoreTime()
181  {
182  $scheduledAt = '2011-12-13 14:15:16';
183  $cronExprArr = ['*', '*', '*', '*', '*'];
184 
185  // 1. Create mocks
186  $timezoneConverter = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
187  $timezoneConverter->expects($this->once())
188  ->method('date')
189  ->with($scheduledAt)
190  ->willReturn(new \DateTime($scheduledAt));
191 
193  $model = $this->helper->getObject(
194  \Magento\Cron\Model\Schedule::class,
195  ['timezoneConverter' => $timezoneConverter]
196  );
197 
198  // 2. Set fixtures
199  $model->setScheduledAt($scheduledAt);
200  $model->setCronExprArr($cronExprArr);
201 
202  // 3. Run tested method
203  $result = $model->trySchedule();
204 
205  // 4. Compare actual result with expected result
206  $this->assertTrue($result);
207  }
208 
212  public function tryScheduleDataProvider()
213  {
214  $date = '2011-12-13 14:15:16';
215  return [
216  [$date, [], false],
217  [$date, null, false],
218  [$date, false, false],
219  [$date, [], false],
220  [$date, null, false],
221  [$date, false, false],
222  [strtotime($date), ['*', '*', '*', '*', '*'], true],
223  [strtotime($date), ['15', '*', '*', '*', '*'], true],
224  [strtotime($date), ['*', '14', '*', '*', '*'], true],
225  [strtotime($date), ['*', '*', '13', '*', '*'], true],
226  [strtotime($date), ['*', '*', '*', '12', '*'], true],
227  [strtotime('Monday'), ['*', '*', '*', '*', '1'], true],
228  ];
229  }
230 
237  public function testMatchCronExpression($cronExpressionPart, $dateTimePart, $expectedResult)
238  {
239  // 1. Create mocks
241  $model = $this->helper->getObject(\Magento\Cron\Model\Schedule::class);
242 
243  // 2. Run tested method
244  $result = $model->matchCronExpression($cronExpressionPart, $dateTimePart);
245 
246  // 3. Compare actual result with expected result
247  $this->assertEquals($expectedResult, $result);
248  }
249 
254  {
255  return [
256  ['*', 0, true],
257  ['*', 1, true],
258  ['*', 59, true],
259 
260  ['0,1,20', 0, true],
261  ['0,1,20', 1, true],
262  ['0,1,20', 20, true],
263  ['0,1,22', 2, false],
264  ['0,1,*', 2, true],
265 
266  ['0-20', 0, true],
267  ['0-20', 1, true],
268  ['0-20', 20, true],
269  ['0-20', 21, false],
270 
271  ['*/2', 0, true],
272  ['*/2', 2, true],
273  ['*/2', 4, true],
274  ['*/2', 3, false],
275  ['*/20', 40, true],
276 
277  ['0-20/5', 0, true],
278  ['0-20/5', 5, true],
279  ['0-20/5', 10, true],
280  ['0-20/5', 21, false],
281  ['0-20/5', 25, false],
282 
283  ['1/5', 5, false],
284  ['5/5', 5, true],
285  ['10/5', 10, true],
286  ];
287  }
288 
294  public function testMatchCronExpressionException($cronExpressionPart)
295  {
296  $dateTimePart = 10;
297 
298  // 1 Create mocks
300  $model = $this->helper->getObject(\Magento\Cron\Model\Schedule::class);
301 
302  // 2. Run tested method
303  $model->matchCronExpression($cronExpressionPart, $dateTimePart);
304  }
305 
310  {
311  return [
312  ['1/2/3'], //Invalid cron expression, expecting 'match/modulus': 1/2/3
313  ['1/'], //Invalid cron expression, expecting numeric modulus: 1/
314  ['-'], //Invalid cron expression
315  ['1-2-3'], //Invalid cron expression, expecting 'from-to' structure: 1-2-3
316  ];
317  }
318 
324  public function testGetNumeric($param, $expectedResult)
325  {
326  // 1. Create mocks
328  $model = $this->helper->getObject(\Magento\Cron\Model\Schedule::class);
329 
330  // 2. Run tested method
331  $result = $model->getNumeric($param);
332 
333  // 3. Compare actual result with expected result
334  $this->assertEquals($expectedResult, $result);
335  }
336 
340  public function getNumericDataProvider()
341  {
342  return [
343  [null, false],
344  ['', false],
345  ['0', 0],
346  [0, 0],
347  [1, 1],
348  [PHP_INT_MAX, PHP_INT_MAX],
349  [1.1, 1.1],
350 
351  ['feb', 2],
352  ['Feb', 2],
353  ['FEB', 2],
354  ['february', 2],
355  ['febXXX', 2],
356 
357  ['wed', 3],
358  ['Wed', 3],
359  ['WED', 3],
360  ['Wednesday', 3],
361  ['wedXXX', 3],
362  ];
363  }
364 
365  public function testTryLockJobSuccess()
366  {
367  $scheduleId = 1;
368 
369  $this->resourceJobMock->expects($this->once())
370  ->method('trySetJobUniqueStatusAtomic')
372  ->will($this->returnValue(true));
373 
375  $model = $this->helper->getObject(
376  \Magento\Cron\Model\Schedule::class,
377  [
378  'resource' => $this->resourceJobMock
379  ]
380  );
381  $model->setId($scheduleId);
382  $this->assertEquals(0, $model->getStatus());
383 
384  $model->tryLockJob();
385 
386  $this->assertEquals(Schedule::STATUS_RUNNING, $model->getStatus());
387  }
388 
389  public function testTryLockJobFailure()
390  {
391  $scheduleId = 1;
392 
393  $this->resourceJobMock->expects($this->once())
394  ->method('trySetJobUniqueStatusAtomic')
396  ->will($this->returnValue(false));
397 
399  $model = $this->helper->getObject(
400  \Magento\Cron\Model\Schedule::class,
401  [
402  'resource' => $this->resourceJobMock
403  ]
404  );
405  $model->setId($scheduleId);
406  $this->assertEquals(0, $model->getStatus());
407 
408  $model->tryLockJob();
409 
410  $this->assertEquals(0, $model->getStatus());
411  }
412 }