PHPUnit RabbitMQ: write a test to create a join function

I ran into the following problem. I wrote a function that creates a connection object (AMQPConnection) with the given parameters. Now I want to write the appropriate unit test. I just don't know how to do this without using the RabbitMQ broker. Here is the function in question:

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);
        }
    }
}
+4
source share
3 answers

Usually you don’t test code like Unittest, as the result will more likely tell you that your server is installed correctly, and not that your code is working.

Do you verify that the PDO returns a valid database connection?

This may make sense if you are testing your installation but not verifying that php c lib is working properly.

+4
source

.

  • AMQPConnector . .
  • .
  • , mock .

, .

amqp interop, - . , .

0

@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);
        }
    }
}

For unit test, I did the following:

$mockConnection = $this->getMockBuilder('PhpAmqpLib\Connection\AMQPStreamConnection')
        ->disableOriginalConstructor()
        ->getMock();

$connectionFunction = function ($params) use ($mockConnection) {
    return $mockConnection;
};

Or it has an Exception.

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

NB : I use getConnection ()AMQPStreamConnection in the function since it is AMQPConnectionmarked as deprecated inPhpAmqpLib

-1
source

Source: https://habr.com/ru/post/1588945/


All Articles