@@ -127,4 +127,140 @@ public function test_destroy()
127127 ->assertSuccessful ();
128128 $ this ->assertCount (2 , Chat::conversation ($ conversation )->setParticipant ($ userModel )->getMessages ());
129129 }
130+
131+ public function test_index_with_cursor ()
132+ {
133+ $ conversation = factory (Conversation::class)->create ();
134+ $ userModel = factory (User::class)->create ();
135+ $ clientModel = factory (Client::class)->create ();
136+
137+ Chat::conversation ($ conversation )->addParticipants ([$ userModel , $ clientModel ]);
138+ Chat::message ('message 1 ' )->from ($ userModel )->to ($ conversation )->send ();
139+ Chat::message ('message 2 ' )->from ($ clientModel )->to ($ conversation )->send ();
140+ Chat::message ('message 3 ' )->from ($ userModel )->to ($ conversation )->send ();
141+
142+ $ parameters = [
143+ $ conversation ->getKey (),
144+ 'participant_id ' => $ userModel ->getKey (),
145+ 'participant_type ' => $ userModel ->getMorphClass (),
146+ 'perPage ' => 2 ,
147+ 'sorting ' => 'asc ' ,
148+ ];
149+
150+ $ response = $ this ->getJson (route ('conversations.messages.index.cursor ' , $ parameters ))
151+ ->assertStatus (200 )
152+ ->assertJsonStructure ([
153+ 'data ' => [
154+ [
155+ 'sender ' ,
156+ 'body ' ,
157+ ],
158+ ],
159+ 'next_cursor ' ,
160+ 'next_page_url ' ,
161+ 'path ' ,
162+ 'per_page ' ,
163+ 'prev_cursor ' ,
164+ 'prev_page_url ' ,
165+ ]);
166+
167+ $ this ->assertCount (2 , $ response ->json ('data ' ));
168+ }
169+
170+ public function test_index_with_cursor_pagination_navigation ()
171+ {
172+ $ conversation = factory (Conversation::class)->create ();
173+ $ userModel = factory (User::class)->create ();
174+ $ clientModel = factory (Client::class)->create ();
175+
176+ Chat::conversation ($ conversation )->addParticipants ([$ userModel , $ clientModel ]);
177+
178+ // Create 5 messages
179+ Chat::message ('message 1 ' )->from ($ userModel )->to ($ conversation )->send ();
180+ Chat::message ('message 2 ' )->from ($ clientModel )->to ($ conversation )->send ();
181+ Chat::message ('message 3 ' )->from ($ userModel )->to ($ conversation )->send ();
182+ Chat::message ('message 4 ' )->from ($ clientModel )->to ($ conversation )->send ();
183+ Chat::message ('message 5 ' )->from ($ userModel )->to ($ conversation )->send ();
184+
185+ // Get first page with 2 items
186+ $ parameters = [
187+ $ conversation ->getKey (),
188+ 'participant_id ' => $ userModel ->getKey (),
189+ 'participant_type ' => $ userModel ->getMorphClass (),
190+ 'perPage ' => 2 ,
191+ 'sorting ' => 'asc ' ,
192+ ];
193+
194+ $ firstPage = $ this ->getJson (route ('conversations.messages.index.cursor ' , $ parameters ))
195+ ->assertStatus (200 );
196+
197+ $ this ->assertCount (2 , $ firstPage ->json ('data ' ));
198+ $ this ->assertEquals ('message 1 ' , $ firstPage ->json ('data.0.body ' ));
199+ $ this ->assertEquals ('message 2 ' , $ firstPage ->json ('data.1.body ' ));
200+ $ this ->assertNotNull ($ firstPage ->json ('next_cursor ' ));
201+
202+ // Get second page using cursor
203+ $ nextCursor = $ firstPage ->json ('next_cursor ' );
204+ $ parameters ['cursor ' ] = $ nextCursor ;
205+
206+ $ secondPage = $ this ->getJson (route ('conversations.messages.index.cursor ' , $ parameters ))
207+ ->assertStatus (200 );
208+
209+ $ this ->assertCount (2 , $ secondPage ->json ('data ' ));
210+ $ this ->assertEquals ('message 3 ' , $ secondPage ->json ('data.0.body ' ));
211+ $ this ->assertEquals ('message 4 ' , $ secondPage ->json ('data.1.body ' ));
212+
213+ // Get third page
214+ $ nextCursor = $ secondPage ->json ('next_cursor ' );
215+ $ parameters ['cursor ' ] = $ nextCursor ;
216+
217+ $ thirdPage = $ this ->getJson (route ('conversations.messages.index.cursor ' , $ parameters ))
218+ ->assertStatus (200 );
219+
220+ $ this ->assertCount (1 , $ thirdPage ->json ('data ' ));
221+ $ this ->assertEquals ('message 5 ' , $ thirdPage ->json ('data.0.body ' ));
222+ $ this ->assertNull ($ thirdPage ->json ('next_cursor ' ));
223+ }
224+
225+ public function test_index_with_cursor_descending_order ()
226+ {
227+ $ conversation = factory (Conversation::class)->create ();
228+ $ userModel = factory (User::class)->create ();
229+ $ clientModel = factory (Client::class)->create ();
230+
231+ Chat::conversation ($ conversation )->addParticipants ([$ userModel , $ clientModel ]);
232+ Chat::message ('message 1 ' )->from ($ userModel )->to ($ conversation )->send ();
233+ Chat::message ('message 2 ' )->from ($ clientModel )->to ($ conversation )->send ();
234+ Chat::message ('message 3 ' )->from ($ userModel )->to ($ conversation )->send ();
235+
236+ $ parameters = [
237+ $ conversation ->getKey (),
238+ 'participant_id ' => $ userModel ->getKey (),
239+ 'participant_type ' => $ userModel ->getMorphClass (),
240+ 'perPage ' => 2 ,
241+ 'sorting ' => 'desc ' ,
242+ ];
243+
244+ $ response = $ this ->getJson (route ('conversations.messages.index.cursor ' , $ parameters ))
245+ ->assertStatus (200 );
246+
247+ $ this ->assertCount (2 , $ response ->json ('data ' ));
248+ // In descending order, newest messages come first
249+ $ this ->assertEquals ('message 3 ' , $ response ->json ('data.0.body ' ));
250+ $ this ->assertEquals ('message 2 ' , $ response ->json ('data.1.body ' ));
251+ }
252+
253+ public function test_index_with_cursor_requires_participant ()
254+ {
255+ $ conversation = factory (Conversation::class)->create ();
256+
257+ $ parameters = [
258+ $ conversation ->getKey (),
259+ 'perPage ' => 10 ,
260+ ];
261+
262+ $ this ->getJson (route ('conversations.messages.index.cursor ' , $ parameters ))
263+ ->assertStatus (422 )
264+ ->assertJsonValidationErrors (['participant_id ' , 'participant_type ' ]);
265+ }
130266}
0 commit comments