@@ -101,55 +101,56 @@ public function it_syncs_positions_when_group_is_changed(): void
101
101
$ category = CategoryFactory::new ()->create ();
102
102
$ anotherCategory = CategoryFactory::new ()->create ();
103
103
104
- $ books = BookFactory::new ()
104
+ $ book = BookFactory::new ()
105
105
->forCategory ($ category )
106
- ->createMany (3 );
107
-
108
- static ::assertSame (0 , $ books [0 ]->getPosition ());
109
- static ::assertSame (1 , $ books [1 ]->getPosition ());
110
- static ::assertSame (2 , $ books [2 ]->getPosition ());
106
+ ->create ();
111
107
112
- BookFactory::new ()
108
+ $ anotherBook = BookFactory::new ()
113
109
->forCategory ($ anotherCategory )
114
110
->create ();
115
111
116
- $ books [ 1 ]-> category ()
117
- -> associate ( $ anotherCategory)
118
- -> save ( );
112
+ $ book -> update ([
113
+ ' category_id ' => $ anotherCategory-> id ,
114
+ ] );
119
115
120
- static ::assertSame (0 , $ books [0 ]->fresh ()->getPosition ());
121
- static ::assertSame (1 , $ books [1 ]->fresh ()->getPosition ());
122
- static ::assertSame (1 , $ books [2 ]->fresh ()->getPosition ());
116
+ static ::assertSame (0 , $ book ->fresh ()->getPosition ());
117
+ static ::assertSame (1 , $ anotherBook ->fresh ()->getPosition ());
123
118
}
124
119
125
120
/**
126
121
* @test
127
122
*/
128
- public function it_sets_next_position_correctly_when_group_is_changed (): void
123
+ public function it_updates_group_along_with_position_correctly (): void
129
124
{
130
125
$ category = CategoryFactory::new ()->create ();
131
126
$ anotherCategory = CategoryFactory::new ()->create ();
132
127
133
- $ book = BookFactory::new ()
128
+ $ books = BookFactory::new ()
134
129
->forCategory ($ category )
135
- ->create ( );
130
+ ->createMany ( 3 );
136
131
137
- $ anotherBook = BookFactory::new ()
132
+ $ anotherBooks = BookFactory::new ()
138
133
->forCategory ($ anotherCategory )
139
- ->create ( );
134
+ ->createMany ( 3 );
140
135
141
- $ book ->category ()
142
- ->associate ($ anotherCategory )
143
- ->save ();
136
+ $ books [0 ]->update ([
137
+ 'category_id ' => $ anotherCategory ->id ,
138
+ 'position ' => 1 ,
139
+ ]);
144
140
145
- static ::assertSame (0 , $ anotherBook ->fresh ()->getPosition ());
146
- static ::assertSame (1 , $ book ->fresh ()->getPosition ());
141
+ static ::assertSame (0 , $ books [1 ]->fresh ()->position );
142
+ static ::assertSame (1 , $ books [2 ]->fresh ()->position );
143
+
144
+ static ::assertSame (0 , $ anotherBooks [0 ]->fresh ()->position );
145
+ static ::assertSame (1 , $ books [0 ]->fresh ()->position );
146
+ static ::assertSame (2 , $ anotherBooks [1 ]->fresh ()->position );
147
+ static ::assertSame (3 , $ anotherBooks [2 ]->fresh ()->position );
147
148
}
148
149
149
150
/**
150
151
* @test
151
152
*/
152
- public function it_updates_group_along_with_position_correctly (): void
153
+ public function it_moves_model_at_start_of_sequence_of_another_group (): void
153
154
{
154
155
$ category = CategoryFactory::new ()->create ();
155
156
$ anotherCategory = CategoryFactory::new ()->create ();
@@ -162,20 +163,50 @@ public function it_updates_group_along_with_position_correctly(): void
162
163
->forCategory ($ anotherCategory )
163
164
->createMany (3 );
164
165
165
- $ books [0 ]->category ()
166
- -> associate ( $ anotherCategory)
167
- -> setPosition ( 1 )
168
- -> save ( );
166
+ $ books [0 ]->update ([
167
+ ' category_id ' => $ anotherCategory-> id ,
168
+ ' position ' => 0 ,
169
+ ] );
169
170
170
171
static ::assertSame (0 , $ books [1 ]->fresh ()->position );
171
172
static ::assertSame (1 , $ books [2 ]->fresh ()->position );
172
173
173
- static ::assertSame (0 , $ anotherBooks [0 ]->fresh ()->position );
174
- static ::assertSame (1 , $ books [0 ]->fresh ()->position );
174
+ static ::assertSame (0 , $ books [0 ]->fresh ()->position );
175
+ static ::assertSame (1 , $ anotherBooks [0 ]->fresh ()->position );
175
176
static ::assertSame (2 , $ anotherBooks [1 ]->fresh ()->position );
176
177
static ::assertSame (3 , $ anotherBooks [2 ]->fresh ()->position );
177
178
}
178
179
180
+ /**
181
+ * @test
182
+ */
183
+ public function it_moves_model_at_end_of_sequence_of_another_group (): void
184
+ {
185
+ $ category = CategoryFactory::new ()->create ();
186
+ $ anotherCategory = CategoryFactory::new ()->create ();
187
+
188
+ $ books = BookFactory::new ()
189
+ ->forCategory ($ category )
190
+ ->createMany (3 );
191
+
192
+ $ anotherBooks = BookFactory::new ()
193
+ ->forCategory ($ anotherCategory )
194
+ ->createMany (3 );
195
+
196
+ $ books [0 ]->update ([
197
+ 'category_id ' => $ anotherCategory ->id ,
198
+ 'position ' => -1 ,
199
+ ]);
200
+
201
+ static ::assertSame (0 , $ books [1 ]->fresh ()->position );
202
+ static ::assertSame (1 , $ books [2 ]->fresh ()->position );
203
+
204
+ static ::assertSame (0 , $ anotherBooks [0 ]->fresh ()->position );
205
+ static ::assertSame (1 , $ anotherBooks [1 ]->fresh ()->position );
206
+ static ::assertSame (2 , $ anotherBooks [2 ]->fresh ()->position );
207
+ static ::assertSame (3 , $ books [0 ]->fresh ()->position );
208
+ }
209
+
179
210
/**
180
211
* @test
181
212
*/
@@ -190,9 +221,10 @@ public function it_executes_3_queries_to_move_model_at_end_of_sequence_of_anothe
190
221
191
222
Category::query ()->getConnection ()->enableQueryLog ();
192
223
193
- $ book ->category ()
194
- ->associate ($ anotherCategory )
195
- ->save ();
224
+ $ book ->update ([
225
+ 'category_id ' => $ anotherCategory ->id ,
226
+ 'position ' => -1 ,
227
+ ]);
196
228
197
229
self ::assertCount (3 , Category::query ()->getConnection ()->getQueryLog ());
198
230
}
0 commit comments