@@ -94,4 +94,56 @@ public function testCommandLaunchesWebSocketServerWithUriFromArguments(): void
9494 $ commandTester = new CommandTester ($ command );
9595 $ commandTester ->execute (['uri ' => $ uri ]);
9696 }
97+
98+ public function testCommandRegistersTheShutdownSignalsOnTheEventLoop (): void
99+ {
100+ $ uri = 'tcp://127.0.0.1:8080 ' ;
101+
102+ /** @var Stub&ServerInterface $socketServer */
103+ $ socketServer = self ::createStub (ServerInterface::class);
104+
105+ /** @var MockObject&Server $server */
106+ $ server = $ this ->createMock (Server::class);
107+ $ server ->expects (self ::once ())
108+ ->method ('run ' );
109+
110+ /** @var MockObject&SocketServerFactory $socketServerFactory */
111+ $ socketServerFactory = $ this ->createMock (SocketServerFactory::class);
112+ $ socketServerFactory ->method ('build ' )
113+ ->with ($ uri )
114+ ->willReturn ($ socketServer );
115+
116+ /** @var MockObject&ServerFactory $serverFactory */
117+ $ serverFactory = $ this ->createMock (ServerFactory::class);
118+ $ serverFactory ->method ('build ' )
119+ ->with ($ socketServer )
120+ ->willReturn ($ server );
121+
122+ $ registeredSignals = [];
123+
124+ /** @var Stub&LoopInterface $loop */
125+ $ loop = $ this ->createStub (LoopInterface::class);
126+ $ loop ->method ('addSignal ' )
127+ ->willReturnCallback (function (int $ signal ) use (&$ registeredSignals ): void {
128+ $ registeredSignals [] = $ signal ;
129+ });
130+
131+ $ command = new RunWebSocketServerCommand (null , $ socketServerFactory , $ serverFactory , $ loop , $ uri );
132+
133+ $ commandTester = new CommandTester ($ command );
134+ $ commandTester ->execute ([]);
135+
136+ // The command guards each registration with \defined() since the SIG* constants
137+ // require ext-pcntl, so only assert on the signals available in this environment.
138+ $ expectedSignals = array_values (array_filter (
139+ [
140+ \defined ('SIGINT ' ) ? \SIGINT : null ,
141+ \defined ('SIGTERM ' ) ? \SIGTERM : null ,
142+ \defined ('SIGQUIT ' ) ? \SIGQUIT : null ,
143+ ],
144+ static fn (?int $ signal ): bool => null !== $ signal ,
145+ ));
146+
147+ self ::assertSame ($ expectedSignals , $ registeredSignals );
148+ }
97149}
0 commit comments