Completed
Push — master ( 200c6a...200e13 )
by Freek
01:43 queued 10s
created

it_can_set_a_new_order_by_custom_column_with_trashed_models()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
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_get_the_highest_order_number_with_trashed_models()
25
    {
26
        $this->setUpSoftDeletes();
27
28
        DummyWithSoftDeletes::first()->delete();
29
30
        $this->assertEquals(DummyWithSoftDeletes::withTrashed()->count(), (new DummyWithSoftDeletes())->getHighestOrderNumber());
31
    }
32
33
    /** @test */
34
    public function it_can_set_a_new_order()
35
    {
36
        $newOrder = Collection::make(Dummy::all()->pluck('id'))->shuffle()->toArray();
37
38
        Dummy::setNewOrder($newOrder);
39
40
        foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) {
41
            $this->assertEquals($newOrder[$i], $dummy->id);
42
        }
43
    }
44
45
    /** @test */
46 View Code Duplication
    public function it_can_set_a_new_order_by_custom_column()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
47
    {
48
        $newOrder = Collection::make(Dummy::all()->pluck('custom_column_sort'))->shuffle()->toArray();
49
50
        Dummy::setNewOrderByCustomColumn('custom_column_sort', $newOrder);
51
52
        foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) {
53
            $this->assertEquals($newOrder[$i], $dummy->custom_column_sort);
54
        }
55
    }
56
57
    /** @test */
58
    public function it_can_set_a_new_order_from_collection()
59
    {
60
        $newOrder = Collection::make(Dummy::all()->pluck('id'))->shuffle();
61
62
        Dummy::setNewOrder($newOrder);
63
64
        foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) {
65
            $this->assertEquals($newOrder[$i], $dummy->id);
66
        }
67
    }
68
69
    /** @test */
70 View Code Duplication
    public function it_can_set_a_new_order_by_custom_column_from_collection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
71
    {
72
        $newOrder = Collection::make(Dummy::all()->pluck('custom_column_sort'))->shuffle();
73
74
        Dummy::setNewOrderByCustomColumn('custom_column_sort', $newOrder);
75
76
        foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) {
77
            $this->assertEquals($newOrder[$i], $dummy->custom_column_sort);
78
        }
79
    }
80
81
    /** @test */
82 View Code Duplication
    public function it_can_set_a_new_order_with_trashed_models()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
83
    {
84
        $this->setUpSoftDeletes();
85
86
        $dummies = DummyWithSoftDeletes::all();
87
88
        $dummies->random()->delete();
89
90
        $newOrder = Collection::make($dummies->pluck('id'))->shuffle();
91
92
        DummyWithSoftDeletes::setNewOrder($newOrder);
93
94
        foreach (DummyWithSoftDeletes::withTrashed()->orderBy('order_column')->get() as $i => $dummy) {
95
            $this->assertEquals($newOrder[$i], $dummy->id);
96
        }
97
    }
98
99
    /** @test */
100 View Code Duplication
    public function it_can_set_a_new_order_by_custom_column_with_trashed_models()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
101
    {
102
        $this->setUpSoftDeletes();
103
104
        $dummies = DummyWithSoftDeletes::all();
105
106
        $dummies->random()->delete();
107
108
        $newOrder = Collection::make($dummies->pluck('custom_column_sort'))->shuffle();
109
110
        DummyWithSoftDeletes::setNewOrderByCustomColumn('custom_column_sort', $newOrder);
111
112
        foreach (DummyWithSoftDeletes::withTrashed()->orderBy('order_column')->get() as $i => $dummy) {
113
            $this->assertEquals($newOrder[$i], $dummy->custom_column_sort);
114
        }
115
    }
116
117
    /** @test */
118 View Code Duplication
    public function it_can_set_a_new_order_without_trashed_models()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
119
    {
120
        $this->setUpSoftDeletes();
121
122
        DummyWithSoftDeletes::first()->delete();
123
124
        $newOrder = Collection::make(DummyWithSoftDeletes::pluck('id'))->shuffle();
125
126
        DummyWithSoftDeletes::setNewOrder($newOrder);
127
128
        foreach (DummyWithSoftDeletes::orderBy('order_column')->get() as $i => $dummy) {
129
            $this->assertEquals($newOrder[$i], $dummy->id);
130
        }
131
    }
132
133
    /** @test */
134 View Code Duplication
    public function it_can_set_a_new_order_by_custom_column_without_trashed_models()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
135
    {
136
        $this->setUpSoftDeletes();
137
138
        DummyWithSoftDeletes::first()->delete();
139
140
        $newOrder = Collection::make(DummyWithSoftDeletes::pluck('custom_column_sort'))->shuffle();
141
142
        DummyWithSoftDeletes::setNewOrderByCustomColumn('custom_column_sort', $newOrder);
143
144
        foreach (DummyWithSoftDeletes::orderBy('order_column')->get() as $i => $dummy) {
145
            $this->assertEquals($newOrder[$i], $dummy->custom_column_sort);
146
        }
147
    }
148
149
    /** @test */
150
    public function it_will_determine_to_sort_when_creating_if_sortable_attribute_does_not_exist()
151
    {
152
        $model = new Dummy();
153
154
        $this->assertTrue($model->shouldSortWhenCreating());
155
    }
156
157
    /** @test */
158
    public function it_will_determine_to_sort_when_creating_if_sort_when_creating_setting_does_not_exist()
159
    {
160
        $model = new class extends Dummy {
161
            public $sortable = [];
162
        };
163
164
        $this->assertTrue($model->shouldSortWhenCreating());
165
    }
166
167
    /** @test */
168
    public function it_will_respect_the_sort_when_creating_setting()
169
    {
170
        $model = new class extends Dummy {
171
            public $sortable = ['sort_when_creating' => true];
172
        };
173
174
        $this->assertTrue($model->shouldSortWhenCreating());
175
176
        $model = new class extends Dummy {
177
            public $sortable = ['sort_when_creating' => false];
178
        };
179
        $this->assertFalse($model->shouldSortWhenCreating());
180
    }
181
182
    /** @test */
183
    public function it_provides_an_ordered_trait()
184
    {
185
        $i = 1;
186
187
        foreach (Dummy::ordered()->get()->pluck('order_column') as $order) {
188
            $this->assertEquals($i++, $order);
189
        }
190
    }
191
192
    /** @test */
193 View Code Duplication
    public function it_can_move_the_order_down()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
194
    {
195
        $firstModel = Dummy::find(3);
196
        $secondModel = Dummy::find(4);
197
198
        $this->assertEquals($firstModel->order_column, 3);
199
        $this->assertEquals($secondModel->order_column, 4);
200
201
        $this->assertNotFalse($firstModel->moveOrderDown());
202
203
        $firstModel = Dummy::find(3);
204
        $secondModel = Dummy::find(4);
205
206
        $this->assertEquals($firstModel->order_column, 4);
207
        $this->assertEquals($secondModel->order_column, 3);
208
    }
209
210
    /** @test */
211
    public function it_will_not_fail_when_it_cant_move_the_order_down()
212
    {
213
        $lastModel = Dummy::all()->last();
214
215
        $this->assertEquals($lastModel->order_column, 20);
216
        $this->assertEquals($lastModel, $lastModel->moveOrderDown());
217
    }
218
219
    /** @test */
220 View Code Duplication
    public function it_can_move_the_order_up()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
221
    {
222
        $firstModel = Dummy::find(3);
223
        $secondModel = Dummy::find(4);
224
225
        $this->assertEquals($firstModel->order_column, 3);
226
        $this->assertEquals($secondModel->order_column, 4);
227
228
        $this->assertNotFalse($secondModel->moveOrderUp());
229
230
        $firstModel = Dummy::find(3);
231
        $secondModel = Dummy::find(4);
232
233
        $this->assertEquals($firstModel->order_column, 4);
234
        $this->assertEquals($secondModel->order_column, 3);
235
    }
236
237
    /** @test */
238
    public function it_will_not_break_when_it_cant_move_the_order_up()
239
    {
240
        $lastModel = Dummy::first();
241
242
        $this->assertEquals($lastModel->order_column, 1);
243
        $this->assertEquals($lastModel, $lastModel->moveOrderUp());
244
    }
245
246
    /** @test */
247 View Code Duplication
    public function it_can_swap_the_position_of_two_given_models()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
248
    {
249
        $firstModel = Dummy::find(3);
250
        $secondModel = Dummy::find(4);
251
252
        $this->assertEquals($firstModel->order_column, 3);
253
        $this->assertEquals($secondModel->order_column, 4);
254
255
        Dummy::swapOrder($firstModel, $secondModel);
256
257
        $this->assertEquals($firstModel->order_column, 4);
258
        $this->assertEquals($secondModel->order_column, 3);
259
    }
260
261
    /** @test */
262 View Code Duplication
    public function it_can_swap_itself_with_another_model()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
263
    {
264
        $firstModel = Dummy::find(3);
265
        $secondModel = Dummy::find(4);
266
267
        $this->assertEquals($firstModel->order_column, 3);
268
        $this->assertEquals($secondModel->order_column, 4);
269
270
        $firstModel->swapOrderWithModel($secondModel);
271
272
        $this->assertEquals($firstModel->order_column, 4);
273
        $this->assertEquals($secondModel->order_column, 3);
274
    }
275
276
    /** @test */
277
    public function it_can_move_a_model_to_the_first_place()
278
    {
279
        $position = 3;
280
281
        $oldModels = Dummy::whereNot('id', $position)->get();
282
283
        $model = Dummy::find($position);
284
285
        $this->assertEquals(3, $model->order_column);
286
287
        $model = $model->moveToStart();
288
289
        $this->assertEquals(1, $model->order_column);
290
291
        $oldModels = $oldModels->pluck('order_column', 'id');
292
        $newModels = Dummy::whereNot('id', $position)->get()->pluck('order_column', 'id');
293
294
        foreach ($oldModels as $key => $oldModel) {
295
            $this->assertEquals($oldModel + 1, $newModels[$key]);
296
        }
297
    }
298
299
    /**
300
     * @test
301
     */
302
    public function it_can_move_a_model_to_the_last_place()
303
    {
304
        $position = 3;
305
306
        $oldModels = Dummy::whereNot('id', $position)->get();
307
308
        $model = Dummy::find($position);
309
310
        $this->assertNotEquals(20, $model->order_column);
311
312
        $model = $model->moveToEnd();
313
314
        $this->assertEquals(20, $model->order_column);
315
316
        $oldModels = $oldModels->pluck('order_column', 'id');
317
318
        $newModels = Dummy::whereNot('id', $position)->get()->pluck('order_column', 'id');
319
320
        foreach ($oldModels as $key => $order) {
321
            if ($order > $position) {
322
                $this->assertEquals($order - 1, $newModels[$key]);
323
            } else {
324
                $this->assertEquals($order, $newModels[$key]);
325
            }
326
        }
327
    }
328
}
329