CronTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Event\Test\Unit\Observer;
  7. use \Magento\Framework\Event\Observer\Cron;
  8. /**
  9. * Class CronTest
  10. */
  11. class CronTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var Cron
  15. */
  16. protected $cron;
  17. protected function setUp()
  18. {
  19. $this->cron = new Cron();
  20. }
  21. protected function tearDown()
  22. {
  23. $this->cron = null;
  24. }
  25. /**
  26. * @dataProvider numericValueProvider
  27. * @param string|int $value
  28. * @param int|bool $expectedResult
  29. */
  30. public function testGetNumeric($value, $expectedResult)
  31. {
  32. $this->assertEquals($expectedResult, $this->cron->getNumeric($value));
  33. }
  34. /**
  35. * @return array
  36. */
  37. public function numericValueProvider()
  38. {
  39. return [
  40. ['jan', 1],
  41. ['feb', 2],
  42. ['mar', 3],
  43. ['apr', 4],
  44. ['may', 5],
  45. ['jun', 6],
  46. ['jul', 7],
  47. ['aug', 8],
  48. ['sep', 9],
  49. ['oct', 10],
  50. ['nov', 11],
  51. ['dec', 12],
  52. ['sun', 0],
  53. ['mon', 1],
  54. ['tue', 2],
  55. ['wed', 3],
  56. ['thu', 4],
  57. ['fri', 5],
  58. ['sat', 6],
  59. ['negative', false],
  60. ['SATupper-case & suffix', 6],
  61. [154, 154],
  62. [3.14, 3.14],
  63. ['12', '12']
  64. ];
  65. }
  66. /**
  67. * @dataProvider matchCronExpressionProvider
  68. * @param string $expression
  69. * @param int $number
  70. * @param bool $expectedResult
  71. */
  72. public function testMatchCronExpression($expression, $number, $expectedResult)
  73. {
  74. $this->assertEquals($expectedResult, $this->cron->matchCronExpression($expression, $number));
  75. }
  76. /**
  77. * @return array
  78. */
  79. public function matchCronExpressionProvider()
  80. {
  81. return [
  82. ['mon-fri', 2, true],
  83. ['mon-fri', 0, false],
  84. ['january-june', 3, true],
  85. ['january-june', 11, false],
  86. [1, 1, true],
  87. ['*', 1214, true],
  88. [13, 11, false],
  89. ];
  90. }
  91. /**
  92. * @dataProvider isValidForProvider
  93. * @param int $time
  94. * @param string $expression
  95. * @param bool $expectedResult
  96. */
  97. public function testIsValidFor($time, $expression, $expectedResult)
  98. {
  99. $eventMock = $this->createMock(\Magento\Framework\Event::class);
  100. $this->cron->setCronExpr($expression);
  101. $this->cron->setNow($time);
  102. $this->assertEquals($expectedResult, $this->cron->isValidFor($eventMock));
  103. }
  104. /**
  105. * @return array
  106. */
  107. public function isValidForProvider()
  108. {
  109. return [
  110. [mktime(0, 0, 12, 7, 1, 2000), '* * * * *', true],
  111. [mktime(0, 0, 12, 7, 1, 2000), '* * * * * *', false],
  112. [mktime(12, 0, 0, 7, 1, 2000), '0 12 * * *', true],
  113. [mktime(11, 0, 0, 7, 1, 2000), '0 12 * * *', false]
  114. ];
  115. }
  116. }