@@ -61,6 +61,31 @@ public function test_connect_to_presence_channel_with_valid_signature()
6161 });
6262 }
6363
64+ public function test_connect_to_presence_channel_when_user_with_same_ids_is_already_joined ()
65+ {
66+ $ rick = $ this ->newPresenceConnection ('presence-channel ' , ['user_id ' => 1 ]);
67+ $ morty = $ this ->newPresenceConnection ('presence-channel ' , ['user_id ' => 2 ]);
68+ $ pickleRick = $ this ->newPresenceConnection ('presence-channel ' , ['user_id ' => 1 ]);
69+
70+ foreach ([$ rick , $ morty , $ pickleRick ] as $ connection ) {
71+ $ connection ->assertSentEvent ('pusher_internal:subscription_succeeded ' , [
72+ 'channel ' => 'presence-channel ' ,
73+ ]);
74+ }
75+
76+ $ this ->channelManager
77+ ->getGlobalConnectionsCount ('1234 ' , 'presence-channel ' )
78+ ->then (function ($ total ) {
79+ $ this ->assertEquals (3 , $ total );
80+ });
81+
82+ $ this ->channelManager
83+ ->getChannelMembers ('1234 ' , 'presence-channel ' )
84+ ->then (function ($ members ) {
85+ $ this ->assertCount (2 , $ members );
86+ });
87+ }
88+
6489 public function test_presence_channel_broadcast_member_events ()
6590 {
6691 $ rick = $ this ->newPresenceConnection ('presence-channel ' , ['user_id ' => 1 ]);
@@ -200,4 +225,56 @@ public function test_local_connections_for_presence_channels()
200225 }
201226 });
202227 }
228+
229+ public function test_multiple_clients_with_same_user_id_trigger_member_added_and_removed_event_only_on_first_and_last_socket_connection ()
230+ {
231+ // Connect the `observer` user to the server
232+ $ observerConnection = $ this ->newPresenceConnection ('presence-channel ' , ['user_id ' => 'observer ' ]);
233+
234+ // Connect the first socket for user `1` to the server
235+ $ firstConnection = $ this ->newPresenceConnection ('presence-channel ' , ['user_id ' => '1 ' ]);
236+
237+ // Make sure the observer sees a `member_added` event for `user:1`
238+ $ observerConnection ->assertSentEvent ('pusher_internal:member_added ' , [
239+ 'event ' => 'pusher_internal:member_added ' ,
240+ 'channel ' => 'presence-channel ' ,
241+ 'data ' => json_encode (['user_id ' => '1 ' ]),
242+ ])->resetEvents ();
243+
244+ // Connect the second socket for user `1` to the server
245+ $ secondConnection = $ this ->newPresenceConnection ('presence-channel ' , ['user_id ' => '1 ' ]);
246+
247+ // Make sure the observer was not notified of a `member_added` event (user was already connected)
248+ $ observerConnection ->assertNotSentEvent ('pusher_internal:member_added ' );
249+
250+ // Disconnect the first socket for user `1` on the server
251+ $ this ->pusherServer ->onClose ($ firstConnection );
252+
253+ // Make sure the observer was not notified of a `member_removed` event (user still connected on another socket)
254+ $ observerConnection ->assertNotSentEvent ('pusher_internal:member_removed ' );
255+
256+ // Disconnect the second (and last) socket for user `1` on the server
257+ $ this ->pusherServer ->onClose ($ secondConnection );
258+
259+ // Make sure the observer was notified of a `member_removed` event (last socket for user was disconnected)
260+ $ observerConnection ->assertSentEvent ('pusher_internal:member_removed ' );
261+
262+ $ this ->channelManager
263+ ->getMemberSockets ('1 ' , '1234 ' , 'presence-channel ' )
264+ ->then (function ($ sockets ) {
265+ $ this ->assertCount (0 , $ sockets );
266+ });
267+
268+ $ this ->channelManager
269+ ->getMemberSockets ('2 ' , '1234 ' , 'presence-channel ' )
270+ ->then (function ($ sockets ) {
271+ $ this ->assertCount (0 , $ sockets );
272+ });
273+
274+ $ this ->channelManager
275+ ->getMemberSockets ('observer ' , '1234 ' , 'presence-channel ' )
276+ ->then (function ($ sockets ) {
277+ $ this ->assertCount (1 , $ sockets );
278+ });
279+ }
203280}
0 commit comments