Structuring tests in php-amqplib, the most used PHP library for RabbitMQ

in #utopian-io6 years ago (edited)

Repository

https://github.com/php-amqplib/php-amqplib

Introduction

RabbitMQ is a popular message broker which enables to defer time-consuming tasks like file conversions, sending emails, or report generation, just to name a few, in order to provide a good user experience to your web visitors.

A message broker is great for web applications to run tasks on the background due to the nature of the HTTP protocol, which is stateless. In other words, requests must be performed quickly because you don’t want users to wait time until a resource-intensive task is completed.


sending-emails.jpg

Learning to scale apps with RabbitMQ is out of the scope of this post – please read the tutorials to get familiar with work queues as well as to learn more about the publish-subscribe pattern.

There are practical examples in Python, Java, Ruby, PHP and many other programming languages; so chances are that you can use a RabbitMQ library in your app right now.

Technology Stack

  • RabbitMQ 2
  • PHP
  • PHPUnit
  • The php-amqplib library

Bug Fixes

Using RabbitMQ in PHP apps is a snap with php-amqplib, the most widely used PHP client for RabbitMQ.

However, in my opinion adding new features is not a great experience from a developer perspective since the library's current testing scaffold, which is written in PHPUnit, still needs some tweaking.

More specifically, what brought me to start structuring the tests was the fact that I wanted to remove the BC Match extension -- if that makes any sense -- as it is suggested at bcadd and bcmul are replaced by built-in operators #573, but found that couldn’t do it very easily.


mail.jpg

So, the PR Structuring tests #574 cleans up a little bit the previous testing structure. It also introduces a few conventions when it comes to writing the tests as described in the following boilerplate PHPUnit code.

class FooTest extends TestCase
{
    public function setUp()
    {
        // ...
    }

    public function tearDown()
    {
        // ...
    }

    /**
     * @test
     */
    public function the_name_of_this_test_has_underscores_to_be_readable()
    {
        // ...
    }

    /**
     * @test
     */
    public function another_test()
    {
        // ...
    }

    /**
     * @test
     * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException
     */
    public function this_one_throws_an_exception()
    {
        // ...
    }

    /**
     * @dataProvider fooData
     * @test
     */
    public function this_test_uses_a_data_provider($value, $expected)
    {
        // ...
    }

    // public methods here (camelCased) 

    // private and protected methods here (camelCased)
}

The name of the methods are written in snake case in order to be a bit more readable. On the other hand, it is recommended to write PHPUnit's setUp() and tearDown() methods at the beginning of the class. Data providers are written below the testing methods as a rule of thumb, and exceptions are tested through annotations.

IMHO, a boilerplate is always a good starting point that will help to build a nice testing scaffold for any app. The important thing actually is to stick with a convention for the sake of consistency.

Finally, let me remind how to run the tests just in case you’d want to contribute now as well:

git clone [email protected]:php-amqplib/php-amqplib.git
cd php-amqplib
composer install
vendor/bin/phpunit 

Thanks so very much for reading.

GitHub Account

https://github.com/programarivm

Sort:  

Thanks for the contribution, @programarivm! It's great to see you've gone out of your way to improve the experience of adding features for other developers. I agree with you that a boilerplate is always a good starting point, and hopefully this will help other contributors also write tests for the features they add - all in all it should result in a more streamlined contributing experience, great work!


Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks for the review @amosbastian

Thank you for your review, @amosbastian!

So far this week you've reviewed 13 contributions. Keep up the good work!

You got a free upvote from @reversed-bidbot! Follow me to earn steem by interacting with my promoted posts.

Hi @programarivm!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @programarivm!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!