22
33namespace SwooleTW \Http \Tests \Websocket ;
44
5- use Illuminate \Container \Container ;
6- use Illuminate \Contracts \Auth \Authenticatable as AuthenticatableContract ;
5+ use Mockery as m ;
76use Illuminate \Http \Request ;
7+ use InvalidArgumentException ;
88use Illuminate \Pipeline \Pipeline ;
9+ use SwooleTW \Http \Server \Manager ;
10+ use SwooleTW \Http \Tests \TestCase ;
11+ use Illuminate \Container \Container ;
912use Illuminate \Support \Facades \App ;
1013use Illuminate \Support \Facades \Config ;
11- use InvalidArgumentException ;
12- use Mockery as m ;
14+ use SwooleTW \Http \Websocket \Websocket ;
1315use SwooleTW \Http \Server \Facades \Server ;
14- use SwooleTW \Http \Tests \TestCase ;
1516use SwooleTW \Http \Websocket \Rooms \RoomContract ;
16- use SwooleTW \ Http \ Websocket \ Websocket ;
17+ use Illuminate \ Contracts \ Auth \ Authenticatable as AuthenticatableContract ;
1718
1819class WebsocketTest extends TestCase
1920{
@@ -326,12 +327,15 @@ public function testEmit()
326327 ->times (3 )
327328 ->andReturn ([3 , 4 , 5 ]);
328329
330+ $ server = m::mock ('server ' );
331+ $ server ->taskworker = false ;
332+
329333 App::shouldReceive ('make ' )
330334 ->with (Server::class)
331335 ->once ()
332- ->andReturnSelf ( );
336+ ->andReturn ( $ server );
333337
334- App:: shouldReceive ('task ' )
338+ $ server -> shouldReceive ('task ' )
335339 ->with ([
336340 'action ' => 'push ' ,
337341 'data ' => [
@@ -355,6 +359,54 @@ public function testEmit()
355359 $ this ->assertFalse ($ websocket ->getIsBroadcast ());
356360 }
357361
362+ public function testEmitInTaskWorker ()
363+ {
364+ $ sender = 1 ;
365+ $ to = [1 , 2 , 'a ' , 'b ' , 'c ' ];
366+ $ broadcast = true ;
367+ $ room = m::mock (RoomContract::class);
368+ $ room ->shouldReceive ('getClients ' )
369+ ->with (m::type ('string ' ))
370+ ->times (3 )
371+ ->andReturn ([3 , 4 , 5 ]);
372+
373+ $ payload = [
374+ 'sender ' => $ sender ,
375+ 'fds ' => [1 , 2 , 3 , 4 , 5 ],
376+ 'broadcast ' => $ broadcast ,
377+ 'assigned ' => true ,
378+ 'event ' => $ event = 'event ' ,
379+ 'message ' => $ data = 'data ' ,
380+ ];
381+
382+ $ server = m::mock ('server ' );
383+ $ server ->taskworker = true ;
384+
385+ $ manager = m::mock (Manager::class);
386+ $ manager ->shouldReceive ('pushMessage ' )
387+ ->with ($ server , $ payload )
388+ ->once ();
389+
390+ App::shouldReceive ('make ' )
391+ ->with (Server::class)
392+ ->once ()
393+ ->andReturn ($ server );
394+
395+ App::shouldReceive ('make ' )
396+ ->with (Manager::class)
397+ ->once ()
398+ ->andReturn ($ manager );
399+
400+ $ websocket = $ this ->getWebsocket ($ room );
401+ $ websocket ->setSender ($ sender )
402+ ->to ($ to )
403+ ->broadcast ()
404+ ->emit ($ event , $ data );
405+
406+ $ this ->assertSame ([], $ websocket ->getTo ());
407+ $ this ->assertFalse ($ websocket ->getIsBroadcast ());
408+ }
409+
358410 public function testClose ()
359411 {
360412 $ fd = 1 ;
0 commit comments