|
| 1 | +<?php |
| 2 | + |
| 3 | +use Devsrv\ScheduledAction\Action; |
| 4 | +use Devsrv\ScheduledAction\Enums\Status; |
| 5 | +use Devsrv\ScheduledAction\Models\ModelAction; |
| 6 | +use Devsrv\ScheduledAction\Tests\Setup\Models\Member; |
| 7 | + |
| 8 | +test('get actions needs to run today default next 10 in ascending time order', function() { |
| 9 | + $member = Member::factory()->create(); |
| 10 | + |
| 11 | + ModelAction::factory()->finished()->model($member)->create([ |
| 12 | + 'act_date' => now() |
| 13 | + ]); |
| 14 | + |
| 15 | + ModelAction::factory()->pending()->model($member)->create([ |
| 16 | + 'act_date' => now(), |
| 17 | + 'act_time' => '16:40:00' |
| 18 | + ]); |
| 19 | + ModelAction::factory()->pending()->model($member)->create([ |
| 20 | + 'act_date' => now(), |
| 21 | + 'act_time' => '16:50:00' |
| 22 | + ]); |
| 23 | + |
| 24 | + ModelAction::factory()->pending()->model($member)->create([ |
| 25 | + 'act_date' => now()->tomorrow() |
| 26 | + ]); |
| 27 | + |
| 28 | + $actionManager = new Action; |
| 29 | + $runToday = $actionManager->needsToRunToday(); |
| 30 | + |
| 31 | + expect($runToday) |
| 32 | + ->toHaveCount(2) |
| 33 | + ->sequence( |
| 34 | + fn($action) => $action->act_time->toTimeString()->toEqual('16:40:00'), |
| 35 | + fn($action) => $action->act_time->toTimeString()->toEqual('16:50:00') |
| 36 | + ); |
| 37 | +}); |
| 38 | + |
| 39 | +test('get actions needs to run today limit upcoming 2 in desc time order', function() { |
| 40 | + $member = Member::factory()->create(); |
| 41 | + |
| 42 | + ModelAction::factory()->pending()->model($member)->create([ |
| 43 | + 'act_date' => now(), |
| 44 | + 'act_time' => '16:40:00' |
| 45 | + ]); |
| 46 | + ModelAction::factory()->pending()->model($member)->create([ |
| 47 | + 'act_date' => now(), |
| 48 | + 'act_time' => '16:50:00' |
| 49 | + ]); |
| 50 | + ModelAction::factory()->pending()->model($member)->create([ |
| 51 | + 'act_date' => now(), |
| 52 | + 'act_time' => '17:00:00' |
| 53 | + ]); |
| 54 | + |
| 55 | + ModelAction::factory()->pending()->model($member)->create([ |
| 56 | + 'act_date' => now()->tomorrow() |
| 57 | + ]); |
| 58 | + |
| 59 | + $actionManager = new Action; |
| 60 | + $runToday = $actionManager->needsToRunToday(2, 'desc'); |
| 61 | + |
| 62 | + expect($runToday) |
| 63 | + ->toHaveCount(2) |
| 64 | + ->sequence( |
| 65 | + fn($action) => $action->act_time->toTimeString()->toEqual('17:00:00'), |
| 66 | + fn($action) => $action->act_time->toTimeString()->toEqual('16:50:00') |
| 67 | + ); |
| 68 | +}); |
| 69 | + |
| 70 | +test('get actions needs to run on target date', function() { |
| 71 | + $member = Member::factory()->create(); |
| 72 | + |
| 73 | + $tomorrow = now()->tomorrow(); |
| 74 | + |
| 75 | + ModelAction::factory()->model($member)->create([ |
| 76 | + 'status' => Status::CANCELLED, |
| 77 | + 'act_date' => $tomorrow, |
| 78 | + 'act_time' => '16:40:00' |
| 79 | + ]); |
| 80 | + |
| 81 | + ModelAction::factory()->pending()->model($member)->create([ |
| 82 | + 'act_date' => $tomorrow, |
| 83 | + 'act_time' => '16:50:00' |
| 84 | + ]); |
| 85 | + ModelAction::factory()->pending()->model($member)->create([ |
| 86 | + 'act_date' => $tomorrow, |
| 87 | + 'act_time' => '17:00:00' |
| 88 | + ]); |
| 89 | + |
| 90 | + ModelAction::factory()->pending()->model($member)->create([ |
| 91 | + 'act_date' => now() |
| 92 | + ]); |
| 93 | + |
| 94 | + $actionManager = new Action; |
| 95 | + $runTomorrow = $actionManager->needsToRunOn($tomorrow); |
| 96 | + |
| 97 | + expect($runTomorrow) |
| 98 | + ->toHaveCount(2) |
| 99 | + ->sequence( |
| 100 | + fn($action) => $action->act_time->toTimeString()->toEqual('16:50:00'), |
| 101 | + fn($action) => $action->act_time->toTimeString()->toEqual('17:00:00') |
| 102 | + ); |
| 103 | +}); |
| 104 | + |
0 commit comments