我正在尝试使用php套接字为个人项目创建库。为此,我开始使用phpUnit来学习和编写(或多或少)定性库。
当我在testConnection方法中没有提供try / catch块时,php会给出连接超时的错误(这是正常的,因为设备没有连接) 。但php应该在下面的execute方法中处理异常,而不是在testConnection方法中。我似乎无法解决这个问题。
这是错误:
PHPUnit_Framework_Error_Warning : stream_socket_client(): unable to connect to tcp://x.x.x.x:* (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
使用方法和try / catch的Testclass不应该存在:
public function testConnection() {
$adu = new Adu();
$adu->setPort('AS0');
$adu->setData('?');
$command = new Command('x.x.x.x', *);
$command->setAduSent($adu);
try
{
$command->execute();
}
catch (Exception $e)
{
echo $e->getMessage();
}
}
这个(执行方法)是应该处理异常的地方:
public function execute()
{
try {
$this->stream = $this->createStream($this->address, $this->port, $this->timeout);
}
catch(Exception $e) {
$this->logger->error('Exception (' . $e->getCode() . '): ' . $e->getMessage() . ' on line ' . $e->getLine(), $e);
}
$this->send($this->stream, $this->aduSent);
$this->aduReceived = $this->receive($this->stream);
}
private function createStream($address, $port, $timeout = 2)
{
$stream = stream_socket_client('tcp://' . $address . ':' . $port, $errorCode, $errorMessage, $timeout);
if(!$stream) {
throw new Exception('Failed to connect(' . $errorCode . '): ' . $errorMessage);
}
return $stream;
}
因为try / catch不会捕获错误/警告我不得不抑制stream_socket_client触发的警告。然后检查返回值是否为false或流对象。如果为false,则抛出适当的异常。
$stream = @stream_socket_client('tcp://' . $address . ':' . $port, $errorCode, $errorMessage, $timeout);
答案 0 :(得分:1)
stream_socket_client语句产生警告,而不是异常,并且try / catch块不会捕获警告。
但PHPUnit会捕获警告,并在这种情况下抛出异常,因此会触发错误。您可以将PHPUnit配置为不将警告视为错误,但我不建议这样做。您的代码应该是免费警告。 PHPUnit docs