PHPUnit RabbitMQ : write test for create connection function

标签 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);
        }
    }
}

最佳答案

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

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

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

关于PHPUnit RabbitMQ : write test for create connection function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30343887/

相关文章:

PhpAmqpLib : Uncaught exception 'ErrorException' with message 'fwrite(): send of 19 bytes failed with errno=104 Connection reset by peer'

php - 如何在mysql中对所有结果求和

php - 创建正则表达式以匹配 00 :00:00 for duration (not time) 的格式

javascript - 无法组合脚本和 php div.innerHTML += "<form action='\"{{route(' stock.store')}}\"' method ='post' >@csrf</form>";

php - 在XDK中使用ajax和php删除sql中的数据

拉拉维尔 5.2 |测试 UploadedFile 在发布后缺少 $test 值。漏洞?

php - 使用 PHPUnit 在 php 中测试关闭例程 (register_shutdown_function)

python - 设置队列中的最大消息数

ajax - 获取 Controller 内部的环境

rabbitmq - 如何修复这个rabbitmq/celery错误?