Completed
Push — master ( f2378d...f54db0 )
by Freek
02:28
created

SortableTest::it_swaps_orders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Spatie\EloquentSortable\Test;
4
5
use Illuminate\Support\Collection;
6
7
class SortableTest extends TestCase
8
{
9
    /**
10
     * @test
11
     */
12
    public function it_sets_the_order_column_on_creation()
13
    {
14
        foreach (Dummy::all() as $dummy) {
15
            $this->assertEquals($dummy->name, $dummy->order_column);
16
        }
17
    }
18
19
    /**
20
     * @test
21
     */
22
    public function it_can_get_the_highest_order_number()
23
    {
24
        $this->assertEquals(Dummy::all()->count(), (new Dummy())->getHighestOrderNumber());
25
    }
26
27
    /**
28
     * @test
29
     */
30
    public function it_can_set_a_new_order()
31
    {
32
        $newOrder = Collection::make(Dummy::all()->pluck('id'))->shuffle()->toArray();
33
34
        Dummy::setNewOrder($newOrder);
35
36
        foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) {
37
            $this->assertEquals($newOrder[$i], $dummy->id);
38
        }
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function it_will_determine_to_sort_when_creating_if_sortable_attribute_does_not_exist()
45
    {
46
        $model = new Dummy();
47
48
        $this->assertTrue($model->shouldSortWhenCreating());
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function it_will_determine_to_sort_when_creating_if_sort_when_creating_setting_does_not_exist()
55
    {
56
        $model = new DummyWithSortableSetting();
57
58
        $this->assertTrue($model->shouldSortWhenCreating());
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function it_will_respect_the_sort_when_creating_setting()
65
    {
66
        $model = new DummyWithSortableSetting();
67
68
        $model->sortable['sort_when_creating'] = true;
69
        $this->assertTrue($model->shouldSortWhenCreating());
70
71
        $model->sortable['sort_when_creating'] = false;
72
        $this->assertFalse($model->shouldSortWhenCreating());
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function it_provides_an_ordered_trait()
79
    {
80
        $i = 1;
81
82
        foreach (Dummy::ordered()->get()->pluck('order_column') as $order) {
83
            $this->assertEquals($i++, $order);
84
        }
85
    }
86
87
    /**
88
     * @test
89
     */
90 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...
91
    {
92
        $firstModel = Dummy::find(3);
93
        $secondModel = Dummy::find(4);
94
95
        $this->assertEquals($firstModel->order_column, 3);
96
        $this->assertEquals($secondModel->order_column, 4);
97
98
        $this->assertNotFalse($firstModel->moveOrderDown());
99
100
        $firstModel = Dummy::find(3);
101
        $secondModel = Dummy::find(4);
102
103
        $this->assertEquals($firstModel->order_column, 4);
104
        $this->assertEquals($secondModel->order_column, 3);
105
    }
106
107
    /**
108
     * @test
109
     */
110
    public function it_will_not_fail_when_it_cant_move_the_order_down()
111
    {
112
        $lastModel = Dummy::all()->last();
113
114
        $this->assertEquals($lastModel->order_column, 20);
115
        $this->assertEquals($lastModel, $lastModel->moveOrderDown());
116
    }
117
118
    /**
119
     * @test
120
     */
121 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...
122
    {
123
        $firstModel = Dummy::find(3);
124
        $secondModel = Dummy::find(4);
125
126
        $this->assertEquals($firstModel->order_column, 3);
127
        $this->assertEquals($secondModel->order_column, 4);
128
129
        $this->assertNotFalse($secondModel->moveOrderUp());
130
131
        $firstModel = Dummy::find(3);
132
        $secondModel = Dummy::find(4);
133
134
        $this->assertEquals($firstModel->order_column, 4);
135
        $this->assertEquals($secondModel->order_column, 3);
136
    }
137
138
    /**
139
     * @test
140
     */
141
    public function it_will_not_when_it_cant_move_the_order_up()
142
    {
143
        $lastModel = Dummy::first();
144
145
        $this->assertEquals($lastModel->order_column, 1);
146
        $this->assertEquals($lastModel, $lastModel->moveOrderUp());
147
    }
148
149
    /**
150
     * @test
151
     */
152 View Code Duplication
    public function it_swaps_orders()
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...
153
    {
154
        $firstModel = Dummy::find(3);
155
        $secondModel = Dummy::find(4);
156
157
        $this->assertEquals($firstModel->order_column, 3);
158
        $this->assertEquals($secondModel->order_column, 4);
159
160
        Dummy::swapOrder($firstModel, $secondModel);
161
162
        $firstModel = Dummy::find(3);
163
        $secondModel = Dummy::find(4);
164
165
        $this->assertEquals($firstModel->order_column, 4);
166
        $this->assertEquals($secondModel->order_column, 3);
167
    }
168
169
    /**
170
     * @test
171
     */
172
    public function it_can_move_the_order_first()
173
    {
174
        $position = 3;
175
176
        $oldModels = Dummy::whereNot('id', $position)->get();
177
178
        $model = Dummy::find($position);
179
180
        $this->assertEquals(3, $model->order_column);
181
182
        $model = $model->moveToStart();
183
184
        $this->assertEquals(1, $model->order_column);
185
186
        $oldModels = $oldModels->pluck('order_column', 'id');
187
        $newModels = Dummy::whereNot('id', $position)->get()->pluck('order_column', 'id');
188
189
        foreach ($oldModels as $key => $oldModel) {
190
            $this->assertEquals($oldModel + 1, $newModels[$key]);
191
        }
192
    }
193
194
    /**
195
     * @test
196
     */
197
    public function it_can_move_the_order_last()
198
    {
199
        $position = 3;
200
201
        $oldModels = Dummy::whereNot('id', $position)->get();
202
203
        $model = Dummy::find($position);
204
205
        $this->assertNotEquals(20, $model->order_column);
206
207
        $model = $model->moveToEnd();
208
209
        $this->assertEquals(20, $model->order_column);
210
211
        $oldModels = $oldModels->pluck('order_column', 'id');
212
213
        $newModels = Dummy::whereNot('id', $position)->get()->pluck('order_column', 'id');
214
215
        foreach ($oldModels as $key => $order) {
216
            if ($order > $position) {
217
                $this->assertEquals($order - 1, $newModels[$key]);
218
            } else {
219
                $this->assertEquals($order, $newModels[$key]);
220
            }
221
        }
222
    }
223
}
224