1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EloquentSortable\Test; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
class SortableTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
/** @test */ |
10
|
|
|
public function it_sets_the_order_column_on_creation() |
11
|
|
|
{ |
12
|
|
|
foreach (Dummy::all() as $dummy) { |
13
|
|
|
$this->assertEquals($dummy->name, $dummy->order_column); |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** @test */ |
18
|
|
|
public function it_can_get_the_highest_order_number() |
19
|
|
|
{ |
20
|
|
|
$this->assertEquals(Dummy::all()->count(), (new Dummy())->getHighestOrderNumber()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** @test */ |
24
|
|
|
public function it_can_set_a_new_order() |
25
|
|
|
{ |
26
|
|
|
$newOrder = Collection::make(Dummy::all()->pluck('id'))->shuffle()->toArray(); |
27
|
|
|
|
28
|
|
|
Dummy::setNewOrder($newOrder); |
29
|
|
|
|
30
|
|
|
foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) { |
31
|
|
|
$this->assertEquals($newOrder[$i], $dummy->id); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** @test */ |
36
|
|
|
public function it_will_determine_to_sort_when_creating_if_sortable_attribute_does_not_exist() |
37
|
|
|
{ |
38
|
|
|
$model = new Dummy(); |
39
|
|
|
|
40
|
|
|
$this->assertTrue($model->shouldSortWhenCreating()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @test */ |
44
|
|
|
public function it_will_determine_to_sort_when_creating_if_sort_when_creating_setting_does_not_exist() |
45
|
|
|
{ |
46
|
|
|
$model = new DummyWithSortableSetting(); |
47
|
|
|
|
48
|
|
|
$this->assertTrue($model->shouldSortWhenCreating()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @test */ |
52
|
|
|
public function it_will_respect_the_sort_when_creating_setting() |
53
|
|
|
{ |
54
|
|
|
$model = new DummyWithSortableSetting(); |
55
|
|
|
|
56
|
|
|
$model->sortable['sort_when_creating'] = true; |
57
|
|
|
$this->assertTrue($model->shouldSortWhenCreating()); |
58
|
|
|
|
59
|
|
|
$model->sortable['sort_when_creating'] = false; |
60
|
|
|
$this->assertFalse($model->shouldSortWhenCreating()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @test */ |
64
|
|
|
public function it_provides_an_ordered_trait() |
65
|
|
|
{ |
66
|
|
|
$i = 1; |
67
|
|
|
|
68
|
|
|
foreach (Dummy::ordered()->get()->pluck('order_column') as $order) { |
69
|
|
|
$this->assertEquals($i++, $order); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @test */ |
74
|
|
View Code Duplication |
public function it_can_move_the_order_down() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$firstModel = Dummy::find(3); |
77
|
|
|
$secondModel = Dummy::find(4); |
78
|
|
|
|
79
|
|
|
$this->assertEquals($firstModel->order_column, 3); |
80
|
|
|
$this->assertEquals($secondModel->order_column, 4); |
81
|
|
|
|
82
|
|
|
$this->assertNotFalse($firstModel->moveOrderDown()); |
83
|
|
|
|
84
|
|
|
$firstModel = Dummy::find(3); |
85
|
|
|
$secondModel = Dummy::find(4); |
86
|
|
|
|
87
|
|
|
$this->assertEquals($firstModel->order_column, 4); |
88
|
|
|
$this->assertEquals($secondModel->order_column, 3); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** @test */ |
92
|
|
|
public function it_will_not_fail_when_it_cant_move_the_order_down() |
93
|
|
|
{ |
94
|
|
|
$lastModel = Dummy::all()->last(); |
95
|
|
|
|
96
|
|
|
$this->assertEquals($lastModel->order_column, 20); |
97
|
|
|
$this->assertEquals($lastModel, $lastModel->moveOrderDown()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** @test */ |
101
|
|
View Code Duplication |
public function it_can_move_the_order_up() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$firstModel = Dummy::find(3); |
104
|
|
|
$secondModel = Dummy::find(4); |
105
|
|
|
|
106
|
|
|
$this->assertEquals($firstModel->order_column, 3); |
107
|
|
|
$this->assertEquals($secondModel->order_column, 4); |
108
|
|
|
|
109
|
|
|
$this->assertNotFalse($secondModel->moveOrderUp()); |
110
|
|
|
|
111
|
|
|
$firstModel = Dummy::find(3); |
112
|
|
|
$secondModel = Dummy::find(4); |
113
|
|
|
|
114
|
|
|
$this->assertEquals($firstModel->order_column, 4); |
115
|
|
|
$this->assertEquals($secondModel->order_column, 3); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** @test */ |
119
|
|
|
public function it_will_not_break_when_it_cant_move_the_order_up() |
120
|
|
|
{ |
121
|
|
|
$lastModel = Dummy::first(); |
122
|
|
|
|
123
|
|
|
$this->assertEquals($lastModel->order_column, 1); |
124
|
|
|
$this->assertEquals($lastModel, $lastModel->moveOrderUp()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** @test */ |
128
|
|
View Code Duplication |
public function it_can_swap_the_position_of_two_given_models() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$firstModel = Dummy::find(3); |
131
|
|
|
$secondModel = Dummy::find(4); |
132
|
|
|
|
133
|
|
|
$this->assertEquals($firstModel->order_column, 3); |
134
|
|
|
$this->assertEquals($secondModel->order_column, 4); |
135
|
|
|
|
136
|
|
|
Dummy::swapOrder($firstModel, $secondModel); |
137
|
|
|
|
138
|
|
|
$this->assertEquals($firstModel->order_column, 4); |
139
|
|
|
$this->assertEquals($secondModel->order_column, 3); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** @test */ |
143
|
|
View Code Duplication |
public function it_can_swap_itself_with_another_model() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
$firstModel = Dummy::find(3); |
146
|
|
|
$secondModel = Dummy::find(4); |
147
|
|
|
|
148
|
|
|
$this->assertEquals($firstModel->order_column, 3); |
149
|
|
|
$this->assertEquals($secondModel->order_column, 4); |
150
|
|
|
|
151
|
|
|
$firstModel->swapOrderWithModel($secondModel); |
152
|
|
|
|
153
|
|
|
$this->assertEquals($firstModel->order_column, 4); |
154
|
|
|
$this->assertEquals($secondModel->order_column, 3); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** @test */ |
158
|
|
|
public function it_can_move_a_model_to_the_first_place() |
159
|
|
|
{ |
160
|
|
|
$position = 3; |
161
|
|
|
|
162
|
|
|
$oldModels = Dummy::whereNot('id', $position)->get(); |
163
|
|
|
|
164
|
|
|
$model = Dummy::find($position); |
165
|
|
|
|
166
|
|
|
$this->assertEquals(3, $model->order_column); |
167
|
|
|
|
168
|
|
|
$model = $model->moveToStart(); |
169
|
|
|
|
170
|
|
|
$this->assertEquals(1, $model->order_column); |
171
|
|
|
|
172
|
|
|
$oldModels = $oldModels->pluck('order_column', 'id'); |
173
|
|
|
$newModels = Dummy::whereNot('id', $position)->get()->pluck('order_column', 'id'); |
174
|
|
|
|
175
|
|
|
foreach ($oldModels as $key => $oldModel) { |
176
|
|
|
$this->assertEquals($oldModel + 1, $newModels[$key]); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @test |
182
|
|
|
*/ |
183
|
|
|
public function it_can_move_a_model_to_the_last_place() |
184
|
|
|
{ |
185
|
|
|
$position = 3; |
186
|
|
|
|
187
|
|
|
$oldModels = Dummy::whereNot('id', $position)->get(); |
188
|
|
|
|
189
|
|
|
$model = Dummy::find($position); |
190
|
|
|
|
191
|
|
|
$this->assertNotEquals(20, $model->order_column); |
192
|
|
|
|
193
|
|
|
$model = $model->moveToEnd(); |
194
|
|
|
|
195
|
|
|
$this->assertEquals(20, $model->order_column); |
196
|
|
|
|
197
|
|
|
$oldModels = $oldModels->pluck('order_column', 'id'); |
198
|
|
|
|
199
|
|
|
$newModels = Dummy::whereNot('id', $position)->get()->pluck('order_column', 'id'); |
200
|
|
|
|
201
|
|
|
foreach ($oldModels as $key => $order) { |
202
|
|
|
if ($order > $position) { |
203
|
|
|
$this->assertEquals($order - 1, $newModels[$key]); |
204
|
|
|
} else { |
205
|
|
|
$this->assertEquals($order, $newModels[$key]); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.