22
33namespace BeyondCode \LaravelWebSockets \Tests \Channels ;
44
5+ use BeyondCode \LaravelWebSockets \Tests \Mocks \Message ;
56use BeyondCode \LaravelWebSockets \Tests \TestCase ;
67
78class ChannelReplicationTest extends TestCase
@@ -16,10 +17,142 @@ public function setUp(): void
1617 $ this ->runOnlyOnRedisReplication ();
1718 }
1819
19- public function test_not_implemented ()
20+ /** @test */
21+ public function replication_clients_can_subscribe_to_channels ()
2022 {
21- $ this ->markTestIncomplete (
22- 'Not yet implemented tests. '
23- );
23+ $ connection = $ this ->getWebSocketConnection ();
24+
25+ $ message = new Message (json_encode ([
26+ 'event ' => 'pusher:subscribe ' ,
27+ 'data ' => [
28+ 'channel ' => 'basic-channel ' ,
29+ ],
30+ ]));
31+
32+ $ this ->pusherServer ->onOpen ($ connection );
33+
34+ $ this ->pusherServer ->onMessage ($ connection , $ message );
35+
36+ $ connection ->assertSentEvent ('pusher_internal:subscription_succeeded ' , [
37+ 'channel ' => 'basic-channel ' ,
38+ ]);
39+ }
40+
41+ /** @test */
42+ public function replication_clients_can_unsubscribe_from_channels ()
43+ {
44+ $ connection = $ this ->getConnectedWebSocketConnection (['test-channel ' ]);
45+
46+ $ channel = $ this ->getChannel ($ connection , 'test-channel ' );
47+
48+ $ this ->assertTrue ($ channel ->hasConnections ());
49+
50+ $ message = new Message (json_encode ([
51+ 'event ' => 'pusher:unsubscribe ' ,
52+ 'data ' => [
53+ 'channel ' => 'test-channel ' ,
54+ ],
55+ ]));
56+
57+ $ this ->pusherServer ->onMessage ($ connection , $ message );
58+
59+ $ this ->assertFalse ($ channel ->hasConnections ());
60+ }
61+
62+ /** @test */
63+ public function replication_a_client_cannot_broadcast_to_other_clients_by_default ()
64+ {
65+ // One connection inside channel "test-channel".
66+ $ existingConnection = $ this ->getConnectedWebSocketConnection (['test-channel ' ]);
67+
68+ $ connection = $ this ->getConnectedWebSocketConnection (['test-channel ' ]);
69+
70+ $ message = new Message ('{"event": "client-test", "data": {}, "channel": "test-channel"} ' );
71+
72+ $ this ->pusherServer ->onMessage ($ connection , $ message );
73+
74+ $ existingConnection ->assertNotSentEvent ('client-test ' );
75+ }
76+
77+ /** @test */
78+ public function replication_a_client_can_be_enabled_to_broadcast_to_other_clients ()
79+ {
80+ config ()->set ('websockets.apps.0.enable_client_messages ' , true );
81+
82+ // One connection inside channel "test-channel".
83+ $ existingConnection = $ this ->getConnectedWebSocketConnection (['test-channel ' ]);
84+
85+ $ connection = $ this ->getConnectedWebSocketConnection (['test-channel ' ]);
86+
87+ $ message = new Message ('{"event": "client-test", "data": {}, "channel": "test-channel"} ' );
88+
89+ $ this ->pusherServer ->onMessage ($ connection , $ message );
90+
91+ $ existingConnection ->assertSentEvent ('client-test ' );
92+ }
93+
94+ /** @test */
95+ public function replication_closed_connections_get_removed_from_all_connected_channels ()
96+ {
97+ $ connection = $ this ->getConnectedWebSocketConnection (['test-channel-1 ' , 'test-channel-2 ' ]);
98+
99+ $ channel1 = $ this ->getChannel ($ connection , 'test-channel-1 ' );
100+ $ channel2 = $ this ->getChannel ($ connection , 'test-channel-2 ' );
101+
102+ $ this ->assertTrue ($ channel1 ->hasConnections ());
103+ $ this ->assertTrue ($ channel2 ->hasConnections ());
104+
105+ $ this ->pusherServer ->onClose ($ connection );
106+
107+ $ this ->assertFalse ($ channel1 ->hasConnections ());
108+ $ this ->assertFalse ($ channel2 ->hasConnections ());
109+ }
110+
111+ /** @test */
112+ public function replication_channels_can_broadcast_messages_to_all_connections ()
113+ {
114+ $ connection1 = $ this ->getConnectedWebSocketConnection (['test-channel ' ]);
115+ $ connection2 = $ this ->getConnectedWebSocketConnection (['test-channel ' ]);
116+
117+ $ channel = $ this ->getChannel ($ connection1 , 'test-channel ' );
118+
119+ $ channel ->broadcast ([
120+ 'event ' => 'broadcasted-event ' ,
121+ 'channel ' => 'test-channel ' ,
122+ ]);
123+
124+ $ connection1 ->assertSentEvent ('broadcasted-event ' );
125+ $ connection2 ->assertSentEvent ('broadcasted-event ' );
126+ }
127+
128+ /** @test */
129+ public function replication_channels_can_broadcast_messages_to_all_connections_except_the_given_connection ()
130+ {
131+ $ connection1 = $ this ->getConnectedWebSocketConnection (['test-channel ' ]);
132+ $ connection2 = $ this ->getConnectedWebSocketConnection (['test-channel ' ]);
133+
134+ $ channel = $ this ->getChannel ($ connection1 , 'test-channel ' );
135+
136+ $ channel ->broadcastToOthers ($ connection1 , (object ) [
137+ 'event ' => 'broadcasted-event ' ,
138+ 'channel ' => 'test-channel ' ,
139+ ]);
140+
141+ $ connection1 ->assertNotSentEvent ('broadcasted-event ' );
142+ $ connection2 ->assertSentEvent ('broadcasted-event ' );
143+ }
144+
145+ /** @test */
146+ public function replication_it_responds_correctly_to_the_ping_message ()
147+ {
148+ $ connection = $ this ->getConnectedWebSocketConnection ();
149+
150+ $ message = new Message (json_encode ([
151+ 'event ' => 'pusher:ping ' ,
152+ ]));
153+
154+ $ this ->pusherServer ->onMessage ($ connection , $ message );
155+
156+ $ connection ->assertSentEvent ('pusher:pong ' );
24157 }
25158}
0 commit comments