IntervalTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php namespace GO\Job\Tests;
  2. use GO\Job;
  3. use PHPUnit\Framework\TestCase;
  4. class IntervalTest extends TestCase
  5. {
  6. public function testShouldRunEveryMinute()
  7. {
  8. $job = new Job('ls');
  9. $this->assertTrue($job->everyMinute()->isDue(\DateTime::createFromFormat('H:i', '00:00')));
  10. }
  11. public function testShouldRunHourly()
  12. {
  13. $job = new Job('ls');
  14. // Default run is at minute 00 every hour
  15. $this->assertTrue($job->hourly()->isDue(\DateTime::createFromFormat('H:i', '10:00')));
  16. $this->assertFalse($job->hourly()->isDue(\DateTime::createFromFormat('H:i', '10:01')));
  17. $this->assertTrue($job->hourly()->isDue(\DateTime::createFromFormat('H:i', '11:00')));
  18. }
  19. public function testShouldRunHourlyWithCustomInput()
  20. {
  21. $job = new Job('ls');
  22. $this->assertTrue($job->hourly(19)->isDue(\DateTime::createFromFormat('H:i', '10:19')));
  23. $this->assertTrue($job->hourly('07')->isDue(\DateTime::createFromFormat('H:i', '10:07')));
  24. $this->assertFalse($job->hourly(19)->isDue(\DateTime::createFromFormat('H:i', '10:01')));
  25. $this->assertTrue($job->hourly(19)->isDue(\DateTime::createFromFormat('H:i', '11:19')));
  26. }
  27. public function testShouldThrowExceptionWithInvalidHourlyMinuteInput()
  28. {
  29. $this->expectException(\InvalidArgumentException::class);
  30. $job = new Job('ls');
  31. $job->hourly('abc');
  32. }
  33. public function testShouldRunDaily()
  34. {
  35. $job = new Job('ls');
  36. // Default run is at 00:00 every day
  37. $this->assertTrue($job->daily()->isDue(\DateTime::createFromFormat('H:i', '00:00')));
  38. }
  39. public function testShouldRunDailyWithCustomInput()
  40. {
  41. $job = new Job('ls');
  42. $this->assertTrue($job->daily(19)->isDue(\DateTime::createFromFormat('H:i', '19:00')));
  43. $this->assertTrue($job->daily(19, 53)->isDue(\DateTime::createFromFormat('H:i', '19:53')));
  44. $this->assertFalse($job->daily(19)->isDue(\DateTime::createFromFormat('H:i', '18:00')));
  45. $this->assertFalse($job->daily(19, 53)->isDue(\DateTime::createFromFormat('H:i', '19:52')));
  46. // A string is also acceptable
  47. $this->assertTrue($job->daily('19')->isDue(\DateTime::createFromFormat('H:i', '19:00')));
  48. $this->assertTrue($job->daily('19:53')->isDue(\DateTime::createFromFormat('H:i', '19:53')));
  49. }
  50. public function testShouldThrowExceptionWithInvalidDailyHourInput()
  51. {
  52. $this->expectException(\InvalidArgumentException::class);
  53. $job = new Job('ls');
  54. $job->daily('abc');
  55. }
  56. public function testShouldThrowExceptionWithInvalidDailyMinuteInput()
  57. {
  58. $this->expectException(\InvalidArgumentException::class);
  59. $job = new Job('ls');
  60. $job->daily(2, 'abc');
  61. }
  62. public function testShouldRunWeekly()
  63. {
  64. $job = new Job('ls');
  65. // Default run is every Sunday at 00:00
  66. $this->assertTrue($job->weekly()->isDue(
  67. new \DateTime('Sunday'))
  68. );
  69. $this->assertFalse($job->weekly()->isDue(
  70. new \DateTime('Tuesday'))
  71. );
  72. }
  73. public function testShouldRunWeeklyOnCustomDay()
  74. {
  75. $job = new Job('ls');
  76. $this->assertTrue($job->weekly(6)->isDue(
  77. new \DateTime('Saturday'))
  78. );
  79. // Testing also the helpers to run weekly on custom day
  80. $this->assertTrue($job->monday()->isDue(
  81. new \DateTime('Monday'))
  82. );
  83. $this->assertFalse($job->monday()->isDue(
  84. new \DateTime('Saturday'))
  85. );
  86. $this->assertTrue($job->tuesday()->isDue(
  87. new \DateTime('Tuesday'))
  88. );
  89. $this->assertTrue($job->wednesday()->isDue(
  90. new \DateTime('Wednesday'))
  91. );
  92. $this->assertTrue($job->thursday()->isDue(
  93. new \DateTime('Thursday'))
  94. );
  95. $this->assertTrue($job->friday()->isDue(
  96. new \DateTime('Friday'))
  97. );
  98. $this->assertTrue($job->saturday()->isDue(
  99. new \DateTime('Saturday'))
  100. );
  101. $this->assertTrue($job->sunday()->isDue(
  102. new \DateTime('Sunday'))
  103. );
  104. }
  105. public function testShouldRunWeeklyOnCustomDayAndTime()
  106. {
  107. $job = new Job('ls');
  108. $date1 = new \DateTime('Saturday 03:45');
  109. $date2 = new \DateTime('Saturday 03:46');
  110. $this->assertTrue($job->weekly(6, 3, 45)->isDue($date1));
  111. $this->assertTrue($job->weekly(6, '03:45')->isDue($date1));
  112. $this->assertFalse($job->weekly(6, '03:45')->isDue($date2));
  113. }
  114. public function testShouldRunMonthly()
  115. {
  116. $job = new Job('ls');
  117. // Default run is every 1st of the month at 00:00
  118. $this->assertTrue($job->monthly()->isDue(
  119. new \DateTime('01 January'))
  120. );
  121. $this->assertTrue($job->monthly()->isDue(
  122. new \DateTime('01 December'))
  123. );
  124. $this->assertFalse($job->monthly()->isDue(
  125. new \DateTime('02 January'))
  126. );
  127. }
  128. public function testShouldRunMonthlyOnCustomMonth()
  129. {
  130. $job = new Job('ls');
  131. $this->assertTrue($job->monthly()->isDue(
  132. new \DateTime('01 January'))
  133. );
  134. // Testing also the helpers to run weekly on custom day
  135. $this->assertTrue($job->january()->isDue(
  136. new \DateTime('01 January'))
  137. );
  138. $this->assertFalse($job->january()->isDue(
  139. new \DateTime('01 February'))
  140. );
  141. $this->assertTrue($job->february()->isDue(
  142. new \DateTime('01 February'))
  143. );
  144. $this->assertTrue($job->march()->isDue(
  145. new \DateTime('01 March'))
  146. );
  147. $this->assertTrue($job->april()->isDue(
  148. new \DateTime('01 April'))
  149. );
  150. $this->assertTrue($job->may()->isDue(
  151. new \DateTime('01 May'))
  152. );
  153. $this->assertTrue($job->june()->isDue(
  154. new \DateTime('01 June'))
  155. );
  156. $this->assertTrue($job->july()->isDue(
  157. new \DateTime('01 July'))
  158. );
  159. $this->assertTrue($job->august()->isDue(
  160. new \DateTime('01 August'))
  161. );
  162. $this->assertTrue($job->september()->isDue(
  163. new \DateTime('01 September'))
  164. );
  165. $this->assertTrue($job->october()->isDue(
  166. new \DateTime('01 October'))
  167. );
  168. $this->assertTrue($job->november()->isDue(
  169. new \DateTime('01 November'))
  170. );
  171. $this->assertTrue($job->december()->isDue(
  172. new \DateTime('01 December'))
  173. );
  174. }
  175. public function testShouldRunMonthlyOnCustomDayAndTime()
  176. {
  177. $job = new Job('ls');
  178. $date1 = new \DateTime('May 15 12:21');
  179. $date2 = new \DateTime('February 15 12:21');
  180. $date3 = new \DateTime('February 16 12:21');
  181. $this->assertTrue($job->monthly(5, 15, 12, 21)->isDue($date1));
  182. $this->assertTrue($job->monthly(5, 15, '12:21')->isDue($date1));
  183. $this->assertFalse($job->monthly(5, 15, '12:21')->isDue($date2));
  184. // Every 15th at 12:21
  185. $this->assertTrue($job->monthly(null, 15, '12:21')->isDue($date1));
  186. $this->assertTrue($job->monthly(null, 15, '12:21')->isDue($date2));
  187. $this->assertFalse($job->monthly(null, 15, '12:21')->isDue($date3));
  188. }
  189. public function testShouldRunAtSpecificDate()
  190. {
  191. $job = new Job('ls');
  192. $date = '2018-01-01';
  193. // As instance of datetime
  194. $this->assertTrue($job->date(new \DateTime($date))->isDue(new \DateTime($date)));
  195. // As date string
  196. $this->assertTrue($job->date($date)->isDue(new \DateTime($date)));
  197. // Fail for different day
  198. $this->assertFalse($job->date($date)->isDue(new \DateTime('2018-01-02')));
  199. }
  200. public function testShouldRunAtSpecificDateTime()
  201. {
  202. $job = new Job('ls');
  203. $date = '2018-01-01 12:20';
  204. // As instance of datetime
  205. $this->assertTrue($job->date(new \DateTime($date))->isDue(new \DateTime($date)));
  206. // As date string
  207. $this->assertTrue($job->date($date)->isDue(new \DateTime($date)));
  208. // Fail for different time
  209. $this->assertFalse($job->date($date)->isDue(new \DateTime('2018-01-01 12:21')));
  210. }
  211. public function testShouldFailIfDifferentYear()
  212. {
  213. $job = new Job('ls');
  214. // As instance of datetime
  215. $this->assertFalse($job->date('2018-01-01')->isDue(new \DateTime('2019-01-01')));
  216. }
  217. public function testEveryMinuteWithParameter()
  218. {
  219. $job = new Job('ls');
  220. // Job should run at 10:00, 10:05, 10:10 etc., but not at 10:02
  221. $this->assertTrue($job->everyMinute(5)->isDue(\DateTime::createFromFormat('H:i', '10:00')));
  222. $this->assertFalse($job->everyMinute(5)->isDue(\DateTime::createFromFormat('H:i', '10:02')));
  223. $this->assertTrue($job->everyMinute(5)->isDue(\DateTime::createFromFormat('H:i', '10:05')));
  224. }
  225. }