Completed
Push — master ( 9c7b17...2ce128 )
by Freek
04:25
created

it_can_set_a_new_order_from_collection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 2
eloc 5
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 View Code Duplication
    public function it_can_set_a_new_order()
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...
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 View Code Duplication
    public function it_can_set_a_new_order_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...
37
    {
38
        $newOrder = Collection::make(Dummy::all()->pluck('id'))->shuffle();
39
40
        Dummy::setNewOrder($newOrder);
0 ignored issues
show
Documentation introduced by
$newOrder is of type object<Illuminate\Support\Collection>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
42
        foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) {
43
            $this->assertEquals($newOrder[$i], $dummy->id);
44
        }
45
    }
46
47
    /** @test */
48
    public function it_will_determine_to_sort_when_creating_if_sortable_attribute_does_not_exist()
49
    {
50
        $model = new Dummy();
51
52
        $this->assertTrue($model->shouldSortWhenCreating());
53
    }
54
55
    /** @test */
56
    public function it_will_determine_to_sort_when_creating_if_sort_when_creating_setting_does_not_exist()
57
    {
58
        $model = new DummyWithSortableSetting();
59
60
        $this->assertTrue($model->shouldSortWhenCreating());
61
    }
62
63
    /** @test */
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
    /** @test */
76
    public function it_provides_an_ordered_trait()
77
    {
78
        $i = 1;
79
80
        foreach (Dummy::ordered()->get()->pluck('order_column') as $order) {
81
            $this->assertEquals($i++, $order);
82
        }
83
    }
84
85
    /** @test */
86 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...
87
    {
88
        $firstModel = Dummy::find(3);
89
        $secondModel = Dummy::find(4);
90
91
        $this->assertEquals($firstModel->order_column, 3);
92
        $this->assertEquals($secondModel->order_column, 4);
93
94
        $this->assertNotFalse($firstModel->moveOrderDown());
95
96
        $firstModel = Dummy::find(3);
97
        $secondModel = Dummy::find(4);
98
99
        $this->assertEquals($firstModel->order_column, 4);
100
        $this->assertEquals($secondModel->order_column, 3);
101
    }
102
103
    /** @test */
104
    public function it_will_not_fail_when_it_cant_move_the_order_down()
105
    {
106
        $lastModel = Dummy::all()->last();
107
108
        $this->assertEquals($lastModel->order_column, 20);
109
        $this->assertEquals($lastModel, $lastModel->moveOrderDown());
110
    }
111
112
    /** @test */
113 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...
114
    {
115
        $firstModel = Dummy::find(3);
116
        $secondModel = Dummy::find(4);
117
118
        $this->assertEquals($firstModel->order_column, 3);
119
        $this->assertEquals($secondModel->order_column, 4);
120
121
        $this->assertNotFalse($secondModel->moveOrderUp());
122
123
        $firstModel = Dummy::find(3);
124
        $secondModel = Dummy::find(4);
125
126
        $this->assertEquals($firstModel->order_column, 4);
127
        $this->assertEquals($secondModel->order_column, 3);
128
    }
129
130
    /** @test */
131
    public function it_will_not_break_when_it_cant_move_the_order_up()
132
    {
133
        $lastModel = Dummy::first();
134
135
        $this->assertEquals($lastModel->order_column, 1);
136
        $this->assertEquals($lastModel, $lastModel->moveOrderUp());
137
    }
138
139
    /** @test */
140 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...
141
    {
142
        $firstModel = Dummy::find(3);
143
        $secondModel = Dummy::find(4);
144
145
        $this->assertEquals($firstModel->order_column, 3);
146
        $this->assertEquals($secondModel->order_column, 4);
147
148
        Dummy::swapOrder($firstModel, $secondModel);
149
150
        $this->assertEquals($firstModel->order_column, 4);
151
        $this->assertEquals($secondModel->order_column, 3);
152
    }
153
154
    /** @test */
155 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...
156
    {
157
        $firstModel = Dummy::find(3);
158
        $secondModel = Dummy::find(4);
159
160
        $this->assertEquals($firstModel->order_column, 3);
161
        $this->assertEquals($secondModel->order_column, 4);
162
163
        $firstModel->swapOrderWithModel($secondModel);
164
165
        $this->assertEquals($firstModel->order_column, 4);
166
        $this->assertEquals($secondModel->order_column, 3);
167
    }
168
169
    /** @test */
170
    public function it_can_move_a_model_to_the_first_place()
171
    {
172
        $position = 3;
173
174
        $oldModels = Dummy::whereNot('id', $position)->get();
175
176
        $model = Dummy::find($position);
177
178
        $this->assertEquals(3, $model->order_column);
179
180
        $model = $model->moveToStart();
181
182
        $this->assertEquals(1, $model->order_column);
183
184
        $oldModels = $oldModels->pluck('order_column', 'id');
185
        $newModels = Dummy::whereNot('id', $position)->get()->pluck('order_column', 'id');
186
187
        foreach ($oldModels as $key => $oldModel) {
188
            $this->assertEquals($oldModel + 1, $newModels[$key]);
189
        }
190
    }
191
192
    /**
193
     * @test
194
     */
195
    public function it_can_move_a_model_to_the_last_place()
196
    {
197
        $position = 3;
198
199
        $oldModels = Dummy::whereNot('id', $position)->get();
200
201
        $model = Dummy::find($position);
202
203
        $this->assertNotEquals(20, $model->order_column);
204
205
        $model = $model->moveToEnd();
206
207
        $this->assertEquals(20, $model->order_column);
208
209
        $oldModels = $oldModels->pluck('order_column', 'id');
210
211
        $newModels = Dummy::whereNot('id', $position)->get()->pluck('order_column', 'id');
212
213
        foreach ($oldModels as $key => $order) {
214
            if ($order > $position) {
215
                $this->assertEquals($order - 1, $newModels[$key]);
216
            } else {
217
                $this->assertEquals($order, $newModels[$key]);
218
            }
219
        }
220
    }
221
}
222