【问题标题】:PHPUnit RabbitMQ: write test for create connection functionPHPUnit RabbitMQ:为创建连接函数编写测试
【发布时间】:2015-05-20 08:11:19
【问题描述】:

我面临以下问题。我编写了一个函数,它在给定所需参数的情况下创建连接对象 (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);
        }
    }
}

【问题讨论】:

    标签: php phpunit rabbitmq


    【解决方案1】:

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

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

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

    【讨论】:

    • 嗨 chozilla,感谢您的快速反应。此权利与PDOdatabase 无关。事情可能会以同样的方式处理,我只是不知道。这是关于 RabbitMQ 和与代理的连接。
    • @dickwan 是一样的——你不测试与数据库的连接,也不测试与rabbitmq的连接,只是因为它不是你的代码——它只是工作,相信它。否则你必须在开始使用之前测试整个 php 标准库。
    • @zerkms:哦,谢谢你的澄清。现在我想知道如何确保覆盖到代码部分。
    • @dickwan 制作“集成”或“安装”测试套件。但不要运行它。仅当您安装服务器时。
    • @chozilla:在您回复之后,我选择了——为了在我的单元测试中实现良好的代码覆盖率——使用匿名函数返回(注入)一个 AMQPConnection 对象。从而将创建与服务器连接的代码与其他代码隔离开来。我稍后会发布我在代码中所做的更改。
    【解决方案2】:

    您应该添加更改正在实例化的类的功能。

    1. 添加通过构造函数或设置器更改连接器类的功能,默认情况下使用默认 AMQPConnector 类。使用它来创建连接器对象。 Example
    2. 为单元测试创​​建一个模拟连接器类。 Example
    3. 在测试中使用它通过构造函数或设置器设置模拟类。 Example

    您还可以对传递给连接器构造函数的参数进行断言。

    另一种方法是使用amqp interop,这样您就不会与任何实现耦合。当您只处理纯接口时,测试起来要容易得多。

    【讨论】:

      【解决方案3】:

      @chozilla @zerkms 因此,感谢您的提示,我决定使用 Closure 来隔离连接到服务器的代码部分。 这是关闭:

      $connectionFunction = function ($params) {
                  return new AMQPStreamConnection(
                      $params['host'],
                      $params['port'],
                      $params['username'],
                      $params['password'],
                      $params['vhost']
                  );
              };
      

      这里是修改后的getConnection()函数

      /**
       * @param string $hostKey The array key of the host connection parameter set
       * @param array $params The connection parameters set
       * @return null|AMQPStreamConnection
       */
      public function getConnection($hostKey, array $params)
      {
          $connection = null;
          try {
              $connection = call_user_func($connectionFunction, $params);
      
              // 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);
              }
          }
      }
      

      对于单元测试,我做了以下工作:

      $mockConnection = $this->getMockBuilder('PhpAmqpLib\Connection\AMQPStreamConnection')
              ->disableOriginalConstructor()
              ->getMock();
      
      $connectionFunction = function ($params) use ($mockConnection) {
          return $mockConnection;
      };
      

      或者这有一个例外。

      $connectionFunction = function ($params)  {
          throw new \Exception;
      };
      

      注意:我在 getConnection() 函数中使用AMQPStreamConnection,因为AMQPConnectionPhpAmqpLib 中被标记为已弃用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-26
        • 1970-01-01
        • 2013-12-05
        • 2013-11-20
        • 2019-11-02
        • 2014-10-06
        • 1970-01-01
        相关资源
        最近更新 更多