1+ <?php
2+
3+ use Devsrv \ScheduledAction \Enums \Status ;
4+ use Devsrv \ScheduledAction \Models \ModelAction ;
5+ use Devsrv \ScheduledAction \Tests \Setup \Models \Member ;
6+ use Illuminate \Support \Facades \Artisan ;
7+ use Illuminate \Support \Facades \Config ;
8+
9+ test ('poll and run via command ' , function () {
10+ Config::set ('scheduled-action.receiver ' , ActionReceiver::class);
11+
12+ $ member = Member::factory ()->create ();
13+
14+ ModelAction::factory ()->pending ()->model ($ member )->create ([
15+ 'properties ' => ['mailable ' => 'App\Mail\Foo ' ],
16+ 'act_date ' => now (),
17+ 'act_time ' => '16:40:00 '
18+ ]);
19+ ModelAction::factory ()->pending ()->model ($ member )->create ([
20+ 'properties ' => ['mailable ' => 'App\Mail\Foo ' ],
21+ 'act_date ' => now (),
22+ 'act_time ' => '16:50:00 '
23+ ]);
24+
25+ ModelAction::factory ()->pending ()->model ($ member )->create ([
26+ 'act_date ' => now ()->tomorrow (),
27+ 'act_time ' => '16:50:00 '
28+ ]);
29+
30+ Artisan::call ('scheduledaction:poll --tasks=1 ' );
31+
32+ expect (ModelAction::whereActDate (now ())->whereStatus (Status::FINISHED )->count ())
33+ ->toEqual (1 );
34+
35+ Artisan::call ('scheduledaction:poll --tasks=1 ' );
36+
37+ expect (ModelAction::whereActDate (now ())->whereStatus (Status::FINISHED )->count ())
38+ ->toEqual (2 );
39+ });
40+
41+ class ActionReceiver {
42+ public function __invoke ($ tasks )
43+ {
44+ foreach ($ tasks as $ task ) {
45+ $ model = $ task ->actionable ;
46+ $ mailable = $ task ->getExtraProperty ('mailable ' );
47+ $ time = $ task ->act_time ;
48+
49+ // assume action handled successfully
50+ $ task ->setFinished ()->save ();
51+ }
52+ }
53+ }
0 commit comments