processor = $this->createMock(TestProcessor::class); $this->transport = $this->createMock(TServerTransport::class); $this->inputTransportFactory = $this->createMock(TTransportFactoryInterface::class); $this->outputTransportFactory = $this->createMock(TTransportFactoryInterface::class); $this->inputProtocolFactory = $this->createMock(TProtocolFactory::class); $this->outputProtocolFactory = $this->createMock(TProtocolFactory::class); $this->server = new TSimpleServer( $this->processor, $this->transport, $this->inputTransportFactory, $this->outputTransportFactory, $this->inputProtocolFactory, $this->outputProtocolFactory ); } protected function tearDown(): void { unset( $this->processor, $this->transport, $this->inputTransportFactory, $this->outputTransportFactory, $this->inputProtocolFactory, $this->outputProtocolFactory, $this->server ); } /** * @dataProvider serveDataProvider */ public function testServe( $serveLoopCount, array $processLoopResult ): void { $transport = $this->createMock(TTransport::class); $this->transport->expects($this->once()) ->method('listen'); $this->transport->expects($this->exactly($serveLoopCount)) ->method('accept') ->willReturn($transport); $this->inputTransportFactory->expects($this->exactly($serveLoopCount)) ->method('getTransport') ->willReturn($this->createMock(TServerTransport::class)); $this->outputTransportFactory->expects($this->exactly($serveLoopCount)) ->method('getTransport') ->willReturn($this->createMock(TServerTransport::class)); $inputProtocol = $this->createMock(TServerTransport::class); $this->inputProtocolFactory->expects($this->exactly($serveLoopCount)) ->method('getProtocol') ->willReturn($inputProtocol); $outputProtocol = $this->createMock(TServerTransport::class); $this->outputProtocolFactory->expects($this->exactly($serveLoopCount)) ->method('getProtocol') ->willReturn($outputProtocol); /** * ATTENTION! * it is a hack to stop the server loop in unit test * last call of process can return any value, but should stop server for removing infinite loop **/ $processLoopResult[] = $this->returnCallback(function () { $this->server->stop(); return false; }); $this->processor->expects($this->exactly(count($processLoopResult))) ->method('process') ->with( $this->equalTo($inputProtocol), $this->equalTo($outputProtocol) ) ->willReturnOnConsecutiveCalls(...$processLoopResult); $this->server->serve(); } public function serveDataProvider() { yield 'one serve loop' => [ 'serveLoopCount' => 1, 'processLoopResult' => [ true, ] ]; yield 'two serve loop' => [ 'serveLoopCount' => 2, 'processLoopResult' => [ true, false, ] ]; } }