|
| 1 | +<?php |
| 2 | + |
| 3 | +use Cachet\Enums\ScheduleStatusEnum; |
| 4 | +use Cachet\Filters\ScheduleStatusFilter; |
| 5 | +use Cachet\Models\Schedule; |
| 6 | + |
| 7 | +it('filters by a numeric status value', function () { |
| 8 | + $upcoming = Schedule::factory()->inTheFuture()->create(); |
| 9 | + Schedule::factory()->inProgress()->create(); |
| 10 | + Schedule::factory()->inThePast()->create(); |
| 11 | + |
| 12 | + $query = Schedule::query(); |
| 13 | + |
| 14 | + (new ScheduleStatusFilter)($query, ScheduleStatusEnum::upcoming->value, 'status'); |
| 15 | + |
| 16 | + expect($query->get()) |
| 17 | + ->toHaveCount(1) |
| 18 | + ->first()->id->toBe($upcoming->id); |
| 19 | +}); |
| 20 | + |
| 21 | +it('filters by an enum instance', function () { |
| 22 | + Schedule::factory()->inTheFuture()->create(); |
| 23 | + $inProgress = Schedule::factory()->inProgress()->create(); |
| 24 | + Schedule::factory()->inThePast()->create(); |
| 25 | + |
| 26 | + $query = Schedule::query(); |
| 27 | + |
| 28 | + (new ScheduleStatusFilter)($query, ScheduleStatusEnum::in_progress, 'status'); |
| 29 | + |
| 30 | + expect($query->get()) |
| 31 | + ->toHaveCount(1) |
| 32 | + ->first()->id->toBe($inProgress->id); |
| 33 | +}); |
| 34 | + |
| 35 | +it('leaves the query untouched when given an invalid value', function () { |
| 36 | + Schedule::factory()->inTheFuture()->create(); |
| 37 | + Schedule::factory()->inProgress()->create(); |
| 38 | + Schedule::factory()->inThePast()->create(); |
| 39 | + |
| 40 | + $query = Schedule::query(); |
| 41 | + |
| 42 | + (new ScheduleStatusFilter)($query, 999, 'status'); |
| 43 | + |
| 44 | + expect($query->get())->toHaveCount(3); |
| 45 | +}); |
| 46 | + |
| 47 | +it('leaves the query untouched when given a non-numeric string', function () { |
| 48 | + Schedule::factory()->inTheFuture()->create(); |
| 49 | + Schedule::factory()->inProgress()->create(); |
| 50 | + |
| 51 | + $query = Schedule::query(); |
| 52 | + |
| 53 | + (new ScheduleStatusFilter)($query, 'not-a-status', 'status'); |
| 54 | + |
| 55 | + expect($query->get())->toHaveCount(2); |
| 56 | +}); |
| 57 | + |
| 58 | +it('filters by multiple statuses', function () { |
| 59 | + Schedule::factory()->inTheFuture()->create(); |
| 60 | + $inProgress = Schedule::factory()->inProgress()->create(); |
| 61 | + $completed = Schedule::factory()->completed()->create(); |
| 62 | + |
| 63 | + $query = Schedule::query(); |
| 64 | + |
| 65 | + (new ScheduleStatusFilter)($query, [ |
| 66 | + ScheduleStatusEnum::in_progress->value, |
| 67 | + ScheduleStatusEnum::complete->value, |
| 68 | + ], 'status'); |
| 69 | + |
| 70 | + expect($query->pluck('id')->all()) |
| 71 | + ->toHaveCount(2) |
| 72 | + ->toContain($inProgress->id, $completed->id); |
| 73 | +}); |
| 74 | + |
| 75 | +it('ignores invalid entries in a multi-value filter', function () { |
| 76 | + Schedule::factory()->inTheFuture()->create(); |
| 77 | + $inProgress = Schedule::factory()->inProgress()->create(); |
| 78 | + Schedule::factory()->inThePast()->create(); |
| 79 | + |
| 80 | + $query = Schedule::query(); |
| 81 | + |
| 82 | + (new ScheduleStatusFilter)($query, [ |
| 83 | + ScheduleStatusEnum::in_progress->value, |
| 84 | + 999, |
| 85 | + 'bogus', |
| 86 | + ], 'status'); |
| 87 | + |
| 88 | + expect($query->get()) |
| 89 | + ->toHaveCount(1) |
| 90 | + ->first()->id->toBe($inProgress->id); |
| 91 | +}); |
| 92 | + |
| 93 | +it('applies no status constraint when every multi-value entry is invalid', function () { |
| 94 | + Schedule::factory()->inTheFuture()->create(); |
| 95 | + Schedule::factory()->inProgress()->create(); |
| 96 | + Schedule::factory()->inThePast()->create(); |
| 97 | + |
| 98 | + $query = Schedule::query(); |
| 99 | + |
| 100 | + (new ScheduleStatusFilter)($query, [999, 'bogus'], 'status'); |
| 101 | + |
| 102 | + expect($query->get())->toHaveCount(3); |
| 103 | +}); |
0 commit comments