@@ -25,39 +25,115 @@ public function test_publishing_client_gets_subscribed()
2525 ->assertCalledWithArgs ('subscribe ' , [$ this ->channelManager ->getRedisKey ('1234 ' , 'public-channel ' )]);
2626 }
2727
28- public function test_events_get_replicated_across_connections ()
28+ public function test_events_get_replicated_across_connections_for_public_channels ()
2929 {
3030 $ connection = $ this ->newActiveConnection (['public-channel ' ]);
31+ $ receiver = $ this ->newActiveConnection (['public-channel ' ]);
3132
32- $ message = [
33+ $ message = new Mocks \ Message ( [
3334 'appId ' => '1234 ' ,
3435 'serverId ' => $ this ->channelManager ->getServerId (),
3536 'event ' => 'some-event ' ,
3637 'data ' => [
3738 'channel ' => 'public-channel ' ,
3839 'test ' => 'yes ' ,
3940 ],
40- ];
41+ 'socketId ' => $ connection ->socketId ,
42+ ]);
4143
4244 $ channel = $ this ->channelManager ->find ('1234 ' , 'public-channel ' );
4345
4446 $ channel ->broadcastToEveryoneExcept (
45- ( object ) $ message, null , '1234 ' , true
47+ $ message-> getPayloadAsObject (), $ connection -> socketId , '1234 ' , true
4648 );
4749
48- $ connection ->assertSentEvent ('some-event ' , [
50+ $ receiver ->assertSentEvent ('some-event ' , $ message ->getPayloadAsArray ());
51+
52+ $ this ->getSubscribeClient ()
53+ ->assertNothingDispatched ();
54+
55+ $ this ->getPublishClient ()
56+ ->assertCalledWithArgs ('publish ' , [
57+ $ this ->channelManager ->getRedisKey ('1234 ' , 'public-channel ' ),
58+ $ message ->getPayload ()
59+ ]);
60+ }
61+
62+ public function test_events_get_replicated_across_connections_for_private_channels ()
63+ {
64+ $ connection = $ this ->newPrivateConnection ('private-channel ' );
65+ $ receiver = $ this ->newPrivateConnection ('private-channel ' );
66+
67+ $ message = new Mocks \SignedMessage ([
4968 'appId ' => '1234 ' ,
5069 'serverId ' => $ this ->channelManager ->getServerId (),
51- 'data ' => ['channel ' => 'public-channel ' , 'test ' => 'yes ' ],
52- ]);
70+ 'event ' => 'some-event ' ,
71+ 'data ' => [
72+ 'channel ' => 'private-channel ' ,
73+ 'test ' => 'yes ' ,
74+ ],
75+ 'socketId ' => $ connection ->socketId ,
76+ ], $ connection , 'private-channel ' );
77+
78+ $ channel = $ this ->channelManager ->find ('1234 ' , 'private-channel ' );
79+
80+ $ channel ->broadcastToEveryoneExcept (
81+ $ message ->getPayloadAsObject (), $ connection ->socketId , '1234 ' , true
82+ );
83+
84+ $ receiver ->assertSentEvent ('some-event ' , $ message ->getPayloadAsArray ());
5385
5486 $ this ->getSubscribeClient ()
5587 ->assertNothingDispatched ();
5688
5789 $ this ->getPublishClient ()
5890 ->assertCalledWithArgs ('publish ' , [
59- $ this ->channelManager ->getRedisKey ('1234 ' , 'public-channel ' ),
60- json_encode ($ message ),
91+ $ this ->channelManager ->getRedisKey ('1234 ' , 'private-channel ' ),
92+ $ message ->getPayload ()
93+ ]);
94+ }
95+
96+ public function test_events_get_replicated_across_connections_for_presence_channels ()
97+ {
98+ $ connection = $ this ->newPresenceConnection ('presence-channel ' );
99+ $ receiver = $ this ->newPresenceConnection ('presence-channel ' , ['user_id ' => 2 ]);
100+
101+ $ user = [
102+ 'user_id ' => 1 ,
103+ 'user_info ' => [
104+ 'name ' => 'Rick ' ,
105+ ],
106+ ];
107+
108+ $ encodedUser = json_encode ($ user );
109+
110+ $ message = new Mocks \SignedMessage ([
111+ 'appId ' => '1234 ' ,
112+ 'serverId ' => $ this ->channelManager ->getServerId (),
113+ 'event ' => 'some-event ' ,
114+ 'data ' => [
115+ 'channel ' => 'presence-channel ' ,
116+ 'channel_data ' => $ encodedUser ,
117+ 'test ' => 'yes ' ,
118+ ],
119+ 'socketId ' => $ connection ->socketId ,
120+ ], $ connection , 'presence-channel ' , $ encodedUser );
121+
122+ $ channel = $ this ->channelManager ->find ('1234 ' , 'presence-channel ' );
123+
124+ $ channel ->broadcastToEveryoneExcept (
125+ $ message ->getPayloadAsObject (), $ connection ->socketId , '1234 ' , true
126+ );
127+
128+ $ receiver ->assertSentEvent ('some-event ' , $ message ->getPayloadAsArray ());
129+
130+ $ this ->getSubscribeClient ()
131+ ->assertNothingDispatched ();
132+
133+ $ this ->getPublishClient ()
134+ ->assertCalledWithArgs ('publish ' , [
135+ $ this ->channelManager ->getRedisKey ('1234 ' , 'presence-channel ' ),
136+ $ message ->getPayload ()
61137 ]);
62138 }
63139
@@ -186,4 +262,108 @@ public function test_not_ponged_connections_do_get_removed_for_presence_channels
186262 $ this ->assertCount (1 , $ members );
187263 });
188264 }
265+
266+ public function test_events_are_processed_by_on_message_on_public_channels ()
267+ {
268+ $ connection = $ this ->newActiveConnection (['public-channel ' ]);
269+
270+ $ message = new Mocks \Message ([
271+ 'appId ' => '1234 ' ,
272+ 'serverId ' => 'different_server_id ' ,
273+ 'event ' => 'some-event ' ,
274+ 'data ' => [
275+ 'channel ' => 'public-channel ' ,
276+ 'test ' => 'yes ' ,
277+ ],
278+ ]);
279+
280+ $ this ->channelManager ->onMessage (
281+ $ this ->channelManager ->getRedisKey ('1234 ' , 'public-channel ' ),
282+ $ message ->getPayload ()
283+ );
284+
285+ // The message does not contain appId and serverId anymore.
286+ $ message = new Mocks \Message ([
287+ 'event ' => 'some-event ' ,
288+ 'data ' => [
289+ 'channel ' => 'public-channel ' ,
290+ 'test ' => 'yes ' ,
291+ ],
292+ ]);
293+
294+ $ connection ->assertSentEvent ('some-event ' , $ message ->getPayloadAsArray ());
295+ }
296+
297+ public function test_events_are_processed_by_on_message_on_private_channels ()
298+ {
299+ $ connection = $ this ->newPrivateConnection ('private-channel ' );
300+
301+ $ message = new Mocks \SignedMessage ([
302+ 'appId ' => '1234 ' ,
303+ 'serverId ' => 'different_server_id ' ,
304+ 'event ' => 'some-event ' ,
305+ 'data ' => [
306+ 'channel ' => 'private-channel ' ,
307+ 'test ' => 'yes ' ,
308+ ],
309+ ], $ connection , 'private-channel ' );
310+
311+ $ this ->channelManager ->onMessage (
312+ $ this ->channelManager ->getRedisKey ('1234 ' , 'private-channel ' ),
313+ $ message ->getPayload ()
314+ );
315+
316+ // The message does not contain appId and serverId anymore.
317+ $ message = new Mocks \SignedMessage ([
318+ 'event ' => 'some-event ' ,
319+ 'data ' => [
320+ 'channel ' => 'private-channel ' ,
321+ 'test ' => 'yes ' ,
322+ ],
323+ ], $ connection , 'private-channel ' );
324+
325+ $ connection ->assertSentEvent ('some-event ' , $ message ->getPayloadAsArray ());
326+ }
327+
328+ public function test_events_are_processed_by_on_message_on_presence_channels ()
329+ {
330+ $ user = [
331+ 'user_id ' => 1 ,
332+ 'user_info ' => [
333+ 'name ' => 'Rick ' ,
334+ ],
335+ ];
336+
337+ $ connection = $ this ->newPresenceConnection ('presence-channel ' , $ user );
338+
339+ $ encodedUser = json_encode ($ user );
340+
341+ $ message = new Mocks \SignedMessage ([
342+ 'appId ' => '1234 ' ,
343+ 'serverId ' => 'different_server_id ' ,
344+ 'event ' => 'some-event ' ,
345+ 'data ' => [
346+ 'channel ' => 'presence-channel ' ,
347+ 'channel_data ' => $ encodedUser ,
348+ 'test ' => 'yes ' ,
349+ ],
350+ ], $ connection , 'presence-channel ' , $ encodedUser );
351+
352+ $ this ->channelManager ->onMessage (
353+ $ this ->channelManager ->getRedisKey ('1234 ' , 'presence-channel ' ),
354+ $ message ->getPayload ()
355+ );
356+
357+ // The message does not contain appId and serverId anymore.
358+ $ message = new Mocks \SignedMessage ([
359+ 'event ' => 'some-event ' ,
360+ 'data ' => [
361+ 'channel ' => 'presence-channel ' ,
362+ 'channel_data ' => $ encodedUser ,
363+ 'test ' => 'yes ' ,
364+ ],
365+ ], $ connection , 'presence-channel ' , $ encodedUser );
366+
367+ $ connection ->assertSentEvent ('some-event ' , $ message ->getPayloadAsArray ());
368+ }
189369}
0 commit comments