|
3 | 3 | use Oddvalue\EloquentSortable\Tests\Dummy; |
4 | 4 |
|
5 | 5 | it('can_be_move_records', function (int $from, int $to, array $expected) { |
6 | | - $featuredProducts = Dummy::all(); |
| 6 | + $this->assertEquals(range(1, 10), Dummy::pluck('order_column')->all()); |
7 | 7 |
|
8 | | - $this->assertEquals(range(1, 10), $featuredProducts->pluck('order_column')->all()); |
| 8 | + Dummy::where('order_column', $from)->first()->moveTo(Dummy::find($to)); |
9 | 9 |
|
10 | | - $featuredProducts->firstWhere('order_column', $from)->moveTo($to); |
11 | | - |
12 | | - $this->assertEquals($expected, $featuredProducts->fresh()->pluck('order_column')->all()); |
| 10 | + $this->assertEquals($expected, Dummy::ordered()->pluck('original_position')->all()); |
13 | 11 | })->with([ |
14 | 12 | [1, 2, [2, 1, 3, 4, 5, 6, 7, 8, 9, 10]], |
15 | | - [2, 6, [1, 6, 2, 3, 4, 5, 7, 8, 9, 10]], |
16 | | - [3, 10, [1, 2, 10, 3, 4, 5, 6, 7, 8, 9]], |
17 | | - [4, 1, [2, 3, 4, 1, 5, 6, 7, 8, 9, 10]], |
| 13 | + [2, 6, [1, 3, 4, 5, 6, 2, 7, 8, 9, 10]], |
| 14 | + [3, 10, [1, 2, 4, 5, 6, 7, 8, 9, 10, 3]], |
| 15 | + [4, 1, [4, 1, 2, 3, 5, 6, 7, 8, 9, 10]], |
18 | 16 | [5, 5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], |
19 | | - [6, 10, [1, 2, 3, 4, 5, 10, 6, 7, 8, 9]], |
20 | | - [7, 1, [2, 3, 4, 5, 6, 7, 1, 8, 9, 10]], |
21 | | - [8, 5, [1, 2, 3, 4, 6, 7, 8, 5, 9, 10]], |
22 | | - [9, 3, [1, 2, 4, 5, 6, 7, 8, 9, 3, 10]], |
23 | | - [10, 1, [2, 3, 4, 5, 6, 7, 8, 9, 10, 1]], |
| 17 | + [6, 10, [1, 2, 3, 4, 5, 7, 8, 9, 10, 6]], |
| 18 | + [7, 1, [7, 1, 2, 3, 4, 5, 6, 8, 9, 10]], |
| 19 | + [8, 5, [1, 2, 3, 4, 8, 5, 6, 7, 9, 10]], |
| 20 | + [9, 3, [1, 2, 9, 3, 4, 5, 6, 7, 8, 10]], |
| 21 | + [10, 1, [10, 1, 2, 3, 4, 5, 6, 7, 8, 9]], |
24 | 22 | ]); |
25 | 23 |
|
26 | 24 | it('can_move_items_between', function (int $from, array $to, array $expected) { |
27 | | - $featuredProducts = Dummy::all(); |
28 | | - |
29 | 25 | [$before, $after] = $to; |
30 | 26 |
|
31 | | - $featuredProducts->firstWhere('order_column', $from) |
| 27 | + Dummy::where('order_column', $from)->first() |
32 | 28 | ->moveBetween( |
33 | | - $featuredProducts->firstWhere('order_column', $before), |
34 | | - $featuredProducts->firstWhere('order_column', $after) |
| 29 | + Dummy::where('order_column', $before)->first(), |
| 30 | + Dummy::where('order_column', $after)->first() |
35 | 31 | ); |
36 | 32 |
|
37 | | - $this->assertEquals($expected, $featuredProducts->fresh()->pluck('order_column')->all()); |
| 33 | + $this->assertEquals($expected, Dummy::ordered()->pluck('original_position')->all()); |
38 | 34 | })->with([ |
39 | 35 | [1, [1, 2], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], |
40 | | - [2, [5, 6], [1, 5, 2, 3, 4, 6, 7, 8, 9, 10]], |
41 | | - [3, [10, null], [1, 2, 10, 3, 4, 5, 6, 7, 8, 9]], |
42 | | - [4, [null, 1], [2, 3, 4, 1, 5, 6, 7, 8, 9, 10]], |
| 36 | + [2, [5, 6], [1, 3, 4, 5, 2, 6, 7, 8, 9, 10]], |
| 37 | + [3, [10, null], [1, 2, 4, 5, 6, 7, 8, 9, 10, 3]], |
| 38 | + [4, [null, 1], [4, 1, 2, 3, 5, 6, 7, 8, 9, 10]], |
43 | 39 | [5, [4, 5], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], |
44 | | - [6, [10, null], [1, 2, 3, 4, 5, 10, 6, 7, 8, 9]], |
45 | | - [7, [null, 1], [2, 3, 4, 5, 6, 7, 1, 8, 9, 10]], |
46 | | - [8, [4, 5], [1, 2, 3, 4, 6, 7, 8, 5, 9, 10]], |
| 40 | + [6, [10, null], [1, 2, 3, 4, 5, 7, 8, 9, 10, 6]], |
| 41 | + [7, [null, 1], [7, 1, 2, 3, 4, 5, 6, 8, 9, 10]], |
| 42 | + [8, [4, 5], [1, 2, 3, 4, 8, 5, 6, 7, 9, 10]], |
47 | 43 | [9, [10, null], [1, 2, 3, 4, 5, 6, 7, 8, 10, 9]], |
48 | | - [10, [1, null], [1, 3, 4, 5, 6, 7, 8, 9, 10, 2]], |
| 44 | + [10, [3, 4], [1, 2, 3, 10, 4, 5, 6, 7, 8, 9]], |
| 45 | + [10, [100, 100], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], |
49 | 46 | ]); |
50 | 47 |
|
51 | 48 | it('can_move_items_before', function ($from, $to, $expected) { |
52 | | - $featuredProducts = Dummy::all(); |
53 | | - |
54 | | - $featuredProducts->firstWhere('order_column', $from)->moveBefore($featuredProducts->firstWhere('order_column', $to)); |
| 49 | + Dummy::where('order_column', $from)->first()->moveBefore($to); |
55 | 50 |
|
56 | | - $this->assertEquals($expected, $featuredProducts->fresh()->pluck('order_column')->all()); |
| 51 | + $this->assertEquals($expected, Dummy::ordered()->pluck('original_position')->all()); |
57 | 52 | })->with([ |
58 | 53 | [1, 2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], |
59 | | - [2, 6, [1, 5, 2, 3, 4, 6, 7, 8, 9, 10]], |
60 | | - [3, 10, [1, 2, 9, 3, 4, 5, 6, 7, 8, 10]], |
61 | | - [4, 1, [2, 3, 4, 1, 5, 6, 7, 8, 9, 10]], |
| 54 | + [2, 6, [1, 3, 4, 5, 2, 6, 7, 8, 9, 10]], |
| 55 | + [3, 10, [1, 2, 4, 5, 6, 7, 8, 9, 3, 10]], |
| 56 | + [4, 1, [4, 1, 2, 3, 5, 6, 7, 8, 9, 10]], |
62 | 57 | [5, 5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], |
63 | 58 | [6, 8, [1, 2, 3, 4, 5, 7, 6, 8, 9, 10]], |
64 | | - [7, 2, [1, 3, 4, 5, 6, 7, 2, 8, 9, 10]], |
65 | | - [8, 5, [1, 2, 3, 4, 6, 7, 8, 5, 9, 10]], |
66 | | - [9, 3, [1, 2, 4, 5, 6, 7, 8, 9, 3, 10]], |
67 | | - [10, 1, [2, 3, 4, 5, 6, 7, 8, 9, 10, 1]], |
| 59 | + [7, 2, [1, 7, 2, 3, 4, 5, 6, 8, 9, 10]], |
| 60 | + [8, 5, [1, 2, 3, 4, 8, 5, 6, 7, 9, 10]], |
| 61 | + [9, 3, [1, 2, 9, 3, 4, 5, 6, 7, 8, 10]], |
| 62 | + [10, 1, [10, 1, 2, 3, 4, 5, 6, 7, 8, 9]], |
68 | 63 | ]); |
69 | 64 |
|
70 | | -/** |
71 | | - * @dataProvider itemsToMoveAfter |
72 | | - */ |
73 | 65 | it('can_move_items_after', function ($from, $to, $expected) { |
74 | | - $featuredProducts = Dummy::all(); |
| 66 | + Dummy::where('order_column', $from)->first()->moveAfter($to); |
75 | 67 |
|
76 | | - $featuredProducts->firstWhere('order_column', $from)->moveAfter($featuredProducts->firstWhere('order_column', $to)); |
77 | | - |
78 | | - $this->assertEquals($expected, $featuredProducts->fresh()->pluck('order_column')->all()); |
| 68 | + $this->assertEquals($expected, Dummy::ordered()->pluck('original_position')->all()); |
79 | 69 | })->with([ |
80 | 70 | [1, 2, [2, 1, 3, 4, 5, 6, 7, 8, 9, 10]], |
81 | | - [2, 6, [1, 6, 2, 3, 4, 5, 7, 8, 9, 10]], |
82 | | - [3, 10, [1, 2, 10, 3, 4, 5, 6, 7, 8, 9]], |
83 | | - [4, 1, [1, 3, 4, 2, 5, 6, 7, 8, 9, 10]], |
| 71 | + [2, 6, [1, 3, 4, 5, 6, 2, 7, 8, 9, 10]], |
| 72 | + [3, 10, [1, 2, 4, 5, 6, 7, 8, 9, 10, 3]], |
| 73 | + [4, 1, [1, 4, 2, 3, 5, 6, 7, 8, 9, 10]], |
84 | 74 | [5, 5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], |
85 | | - [6, 8, [1, 2, 3, 4, 5, 8, 6, 7, 9, 10]], |
86 | | - [7, 2, [1, 2, 4, 5, 6, 7, 3, 8, 9, 10]], |
87 | | - [8, 5, [1, 2, 3, 4, 5, 7, 8, 6, 9, 10]], |
88 | | - [9, 3, [1, 2, 3, 5, 6, 7, 8, 9, 4, 10]], |
89 | | - [10, 1, [1, 3, 4, 5, 6, 7, 8, 9, 10, 2]], |
| 75 | + [6, 8, [1, 2, 3, 4, 5, 7, 8, 6, 9, 10]], |
| 76 | + [7, 2, [1, 2, 7, 3, 4, 5, 6, 8, 9, 10]], |
| 77 | + [8, 5, [1, 2, 3, 4, 5, 8, 6, 7, 9, 10]], |
| 78 | + [9, 3, [1, 2, 3, 9, 4, 5, 6, 7, 8, 10]], |
| 79 | + [10, 1, [1, 10, 2, 3, 4, 5, 6, 7, 8, 9]], |
90 | 80 | ]); |
| 81 | + |
| 82 | +it('performs_acceptably', function () { |
| 83 | + Dummy::query()->delete(); |
| 84 | + collect(range(1, 5000))->each(fn ($i) => Dummy::create()); |
| 85 | + $start = microtime(true); |
| 86 | + Dummy::first()->moveAfter(Dummy::inRandomOrder()->first()); |
| 87 | + expect(microtime(true) - $start)->toBeLessThanOrEqual(2); |
| 88 | +}); |
0 commit comments