如何通过单元测试覆盖PHP套接字服务器

如何通过单元测试覆盖PHP套接字服务器

问题描述:

我正在创建多线程完全可扩展的PHP套接字服务器库,但我不知道的是如何用单元测试来覆盖这个库。



假设我们有一个名为Server的课程方法:

- >创造

- >连接

- >断开

- >开始

- >停止

- >重新启动



方法创建是简单可测试的,我们测试方法是否已被调用,如果没有异常。



方法连接包含此代码

I'm creating multi-thread fully scalable PHP socket server library, but what I do not know is how to cover this library with unit test's.

Let's say we have class called Server with methods:
-> create
-> connect
-> disconnect
-> start
-> stop
-> restart

Method create is simple testable, we testing if the method have been called and if there is no exception.

Method connect contain this code

public function connect($socket){
    if(($acceptSocket = socket_accept($socket)) < 0){
          //exception
    }

   $this->sockets[] = $socket;
}





问题是当你调用socket_accept时,PHP会被卡在此处,直到新连接进入此套接字。所以在这种情况下,我无法测试它是否被接受因为单位测试不会在这里继续,但只是停止。



与方法运行相同,其中是无限循环。 />


我怎么想做测试?



我尝试了什么:



我试图找到关于这个问题的回复,但是找到一个,workig one没那么幸运。



Issue is when you call socket_accept, PHP will get stuck here until new connection will come to this socket. So in this case I cannot test if it was accepted becaseu unit test will not continue here but just het stop.

Same with method run where is infinite loop.

How then I'm suppose to do tests ?

What I have tried:

I've tried to find response regarding to this issue but haven't been that lucky to found one, workig one.

socket){
if((
socket){ if((


acceptSocket = socket_accept(
acceptSocket = socket_accept(


socket))&lt ; 0){
// exception
}
socket)) < 0){ //exception }