PHPUnit RabbitMQ:为创建连接函数编写测试

dic*_*wan 5 php phpunit rabbitmq

我面临以下问题。我编写了一个函数,根据所需参数创建连接对象 (AMQPConnection)。现在我想编写相应的单元测试。我只是不知道在没有运行 RabbitMQ 代理的情况下如何做到这一点。这是有问题的函数:

public function getConnection($hostKey, array $params)
{
    $connection = null;
    try {

        $connection = new AMQPConnection(
            $params['host'],
            $params['port'],
            $params['username'],
            $params['password'],
            $params['vhost']
        );

        // set this server as default for next connection connectionAttempt
        $this->setDefaultHostConfig($hostKey, $params);

        return $connection;
    } catch (\Exception $ex) {

        if ($this->isAttemptExceeded()) {
            return $connection;
        } else {
            // increment connection connectionAttempt
            $this->setConnectionAttempt($this->getConnectionAttempt() + 1);

            return $this->getConnection($hostKey, $params);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

cho*_*lla 4

您通常不会将这样的代码作为单元测试进行测试,因为结果更有可能告诉您您的服务器安装正确,而不是您的代码正在运行。

您是否测试 PDO 是否返回有效的数据库连接?

如果您测试安装但不测试 php c 库是否正常工作,这可能是有意义的。