|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\DB; |
| 6 | +use PDPhilip\Elasticsearch\Tests\Models\Item; |
| 7 | +use PDPhilip\Elasticsearch\Tests\Models\User; |
| 8 | + |
| 9 | +beforeEach(function () { |
| 10 | + User::executeSchema(); |
| 11 | + Item::executeSchema(); |
| 12 | + |
| 13 | + DB::table('items')->insert([ |
| 14 | + ['name' => 'sword alpha', 'type' => 'sharp', 'amount' => 1500, 'stock' => 1], |
| 15 | + ['name' => 'sword beta', 'type' => 'sharp', 'amount' => 900, 'stock' => 10], |
| 16 | + ['name' => 'teddy', 'type' => 'fluffy', 'amount' => 10, 'stock' => 5], |
| 17 | + ['name' => 'spoon', 'type' => 'round', 'amount' => 3, 'stock' => 15], |
| 18 | + ['name' => 'sword charlie', 'type' => 'sharp', 'amount' => 350, 'stock' => 111], |
| 19 | + ['name' => 'sword delta', 'type' => 'sharp', 'amount' => 99, 'stock' => 12], |
| 20 | + ['name' => 'knife', 'type' => 'sharp', 'amount' => 8, 'stock' => 1], |
| 21 | + ['name' => 'fork', 'type' => 'sharp', 'amount' => 6, 'stock' => 5], |
| 22 | + ['name' => 'golf ball', 'type' => 'round', 'amount' => 14, 'stock' => 7], |
| 23 | + ['name' => 'ball', 'type' => 'round', 'amount' => 14, 'stock' => 7], |
| 24 | + ]); |
| 25 | + |
| 26 | +}); |
| 27 | + |
| 28 | +it('groups by ranges using arrays for ranges', function () { |
| 29 | + |
| 30 | + $groups = Item::groupByRanges('stock', [ |
| 31 | + [null, 5], |
| 32 | + [5, 15], |
| 33 | + [15, null], |
| 34 | + ])->get(); |
| 35 | + expect($groups)->toHaveCount(3) |
| 36 | + ->and($groups[0]['key'])->toBe('*-5.0') |
| 37 | + ->and($groups[1]['key'])->toBe('5.0-15.0') |
| 38 | + ->and($groups[2]['key'])->toBe('15.0-*') |
| 39 | + ->and($groups[0]['count'])->toBe(2) |
| 40 | + ->and($groups[1]['count'])->toBe(6) |
| 41 | + ->and($groups[2]['count'])->toBe(2); |
| 42 | + |
| 43 | +}); |
| 44 | +it('groups by ranges using associative arrays for ranges', function () { |
| 45 | + |
| 46 | + $groups = Item::groupByRanges('stock', [ |
| 47 | + [ |
| 48 | + 'to' => 5, |
| 49 | + ], |
| 50 | + [ |
| 51 | + 'from' => 5, |
| 52 | + 'to' => 15, |
| 53 | + ], |
| 54 | + [ |
| 55 | + 'from' => 15, |
| 56 | + ], |
| 57 | + ])->get(); |
| 58 | + expect($groups)->toHaveCount(3) |
| 59 | + ->and($groups[0]['key'])->toBe('*-5.0') |
| 60 | + ->and($groups[1]['key'])->toBe('5.0-15.0') |
| 61 | + ->and($groups[2]['key'])->toBe('15.0-*') |
| 62 | + ->and($groups[0]['count'])->toBe(2) |
| 63 | + ->and($groups[1]['count'])->toBe(6) |
| 64 | + ->and($groups[2]['count'])->toBe(2); |
| 65 | +}); |
| 66 | + |
| 67 | +it('groups by ranges using associative arrays for ranges with custom keys', function () { |
| 68 | + |
| 69 | + $groups = Item::groupByRanges('stock', [ |
| 70 | + [ |
| 71 | + 'key' => 'low-stock', |
| 72 | + 'to' => 5, |
| 73 | + ], |
| 74 | + [ |
| 75 | + 'key' => 'medium-stock', |
| 76 | + 'from' => 5, |
| 77 | + 'to' => 15, |
| 78 | + ], |
| 79 | + [ |
| 80 | + 'key' => 'high-stock', |
| 81 | + 'from' => 15, |
| 82 | + ], |
| 83 | + ])->get(); |
| 84 | + expect($groups)->toHaveCount(3) |
| 85 | + ->and($groups[0]['key'])->toBe('low-stock') |
| 86 | + ->and($groups[1]['key'])->toBe('medium-stock') |
| 87 | + ->and($groups[2]['key'])->toBe('high-stock') |
| 88 | + ->and($groups[0]['count'])->toBe(2) |
| 89 | + ->and($groups[1]['count'])->toBe(6) |
| 90 | + ->and($groups[2]['count'])->toBe(2); |
| 91 | +}); |
| 92 | + |
| 93 | +it('groups by ranges and aggregates', function () { |
| 94 | + |
| 95 | + $groups = Item::groupByRanges('stock', [ |
| 96 | + [ |
| 97 | + 'key' => 'low-stock', |
| 98 | + 'to' => 5, |
| 99 | + ], |
| 100 | + [ |
| 101 | + 'key' => 'medium-stock', |
| 102 | + 'from' => 5, |
| 103 | + 'to' => 15, |
| 104 | + ], |
| 105 | + [ |
| 106 | + 'key' => 'high-stock', |
| 107 | + 'from' => 15, |
| 108 | + ], |
| 109 | + ])->agg(['min', 'max', 'count'], 'amount'); |
| 110 | + expect($groups)->toHaveCount(3) |
| 111 | + ->and($groups[1]['key'])->toBe('medium-stock') |
| 112 | + ->and($groups[1]['count'])->toBe(6) |
| 113 | + ->and($groups[1]['count'])->toBe($groups[1]['count_amount']) // the aggregate `count_amount` reflects the same as the group count |
| 114 | + ->and((int) $groups[1]['min_amount'])->toBe(6) |
| 115 | + ->and((int) $groups[1]['max_amount'])->toBe(900); |
| 116 | +}); |
0 commit comments