File tree Expand file tree Collapse file tree 3 files changed +81
-7
lines changed Expand file tree Collapse file tree 3 files changed +81
-7
lines changed Original file line number Diff line number Diff line change @@ -198,13 +198,8 @@ protected function shouldRun()
198
198
}
199
199
}
200
200
201
- $ schedule = \DateTime::createFromFormat ('Y-m-d H:i:s ' , $ this ->config ['schedule ' ]);
202
- if ($ schedule !== false ) {
203
- return $ schedule ->format ('Y-m-d H:i ' ) == (date ('Y-m-d H:i ' ));
204
- }
205
-
206
- $ cron = CronExpression::factory ($ this ->config ['schedule ' ]);
207
- if (!$ cron ->isDue ()) {
201
+ $ scheduleChecker = new ScheduleChecker ();
202
+ if (!$ scheduleChecker ->isDue ($ this ->config ['schedule ' ])) {
208
203
return false ;
209
204
}
210
205
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Jobby ;
4
+
5
+ use Cron \CronExpression ;
6
+
7
+ class ScheduleChecker
8
+ {
9
+ /**
10
+ * @param string $schedule
11
+ * @return bool
12
+ */
13
+ public function isDue ($ schedule )
14
+ {
15
+ $ dateTime = \DateTime::createFromFormat ('Y-m-d H:i:s ' , $ schedule );
16
+ if ($ dateTime !== false ) {
17
+ return $ dateTime ->format ('Y-m-d H:i ' ) == (date ('Y-m-d H:i ' ));
18
+ }
19
+
20
+ return CronExpression::factory ($ schedule )->isDue ();
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Jobby \Tests ;
4
+
5
+ use Jobby \ScheduleChecker ;
6
+ use PHPUnit_Framework_TestCase ;
7
+
8
+ class ScheduleCheckerTest extends PHPUnit_Framework_TestCase
9
+ {
10
+ /**
11
+ * @var ScheduleChecker
12
+ */
13
+ private $ scheduleChecker ;
14
+
15
+ /**
16
+ * @return void
17
+ */
18
+ protected function setUp ()
19
+ {
20
+ parent ::setUp ();
21
+
22
+ $ this ->scheduleChecker = new ScheduleChecker ();
23
+ }
24
+
25
+ /**
26
+ * @return void
27
+ */
28
+ public function test_it_can_detect_a_due_job_from_a_datetime_string ()
29
+ {
30
+ $ this ->assertTrue ($ this ->scheduleChecker ->isDue (date ('Y-m-d H:i:s ' )));
31
+ }
32
+
33
+ /**
34
+ * @return void
35
+ */
36
+ public function test_it_can_detect_a_non_due_job_from_a_datetime_string ()
37
+ {
38
+ $ this ->assertFalse ($ this ->scheduleChecker ->isDue (date ('Y-m-d H:i:s ' , strtotime ('tomorrow ' ))));
39
+ }
40
+
41
+ /**
42
+ * @return void
43
+ */
44
+ public function test_it_can_detect_a_due_job_from_a_cron_expression ()
45
+ {
46
+ $ this ->assertTrue ($ this ->scheduleChecker ->isDue ("* * * * * " ));
47
+ }
48
+
49
+ /**
50
+ * @return void
51
+ */
52
+ public function test_it_can_detect_a_non_due_job_from_a_cron_expression ()
53
+ {
54
+ $ hour = date ("H " , strtotime ('+1 hour ' ));
55
+ $ this ->assertFalse ($ this ->scheduleChecker ->isDue ("* {$ hour } * * * " ));
56
+ }
57
+ }
You can’t perform that action at this time.
0 commit comments