PHP
Article
By Younes Rafie

8 Must Have PHP Quality Assurance Tools

By Younes Rafie

This popular post has been updated on June 30th 2017 to include newest technologies and tools.


For shipping quality code, we must have testing in mind while coding (if not doing TDD). However, with the wide range of PHP testing tools out there, it’s hard to make a choice! Exploring PHP is a fun adventure (premium course on that here!) but it’s hard to assemble a toolbelt that’s not too heavy to wear to work!

This popular article will highlight the most popular testing tools and has been updated to reflect the state of QA tools in 2017.

Untested code is broken code.

Lab testing environment illustration

PHPUnit

PHPUnit is the go to testing framework for PHP. It was created by Sebastian Bergmann in 2004 and current in version 6 that requires PHP 7.

We have plenty of tutorials coming up about it, but here are some you can already consume.

Cucumber

Cucumber is a framework for creating acceptance tests from specifications. It’s known for it descriptive generated texts that can be read as just plain English. The official PHP implementation for Cucumber is Behat.

Behat logo

We have a getting started tutorial about it here on SitePoint. The below example taken from the documentation is a good example on how expressive those expectations are.

Feature: Listing command
  In order to change the structure of the folder I am currently in
  As a UNIX user
  I need to be able see the currently available files and folders there

  Scenario: Listing two files in a directory
    Given I am in a directory "test"
    And I have a file named "foo"
    And I have a file named "bar"
    When I run "ls"
    Then I should get:
      """
      bar
      foo
      """

Atoum

Atoum logo

Atoum is another unit testing framework for PHP. It’s a standalone package that you can install via GitHub, Composer or via a PHAR executable file.

Atoum tests are very readable with expressive method names and chaining.

$this->integer($classInstance->myMethod())
        ->isEqualTo(10);

$this->string($classInstance->myMethod())
        ->contains("Something heppened");

You want to learn more about PHP unit testing with Atoum, you can follow this tutorial.

Selenium

Selenium is a tool for automated browser testing (Integration and acceptance testing). It transforms the tests to browser API commands and it asserts the expected results. It supports most of the available browsers out there.

We can use Selenium with PHPUnit using an extension.

composer require --dev phpunit/phpunit
composer require --dev phpunit/phpunit-selenium

Here’s a simple example:

class UserSubscriptionTest extends PHPUnit_Extensions_Selenium2TestCase
{
    public function testFormSubmissionWithUsername()
    {
        $this->byName('username')->value('name');
        $this->byId('subscriptionForm')->submit();
    }
}

You can follow this series if you want to learn more about Testing with PHPUnit and Selenium.

--ADVERTISEMENT--

Dusk

Laravel Dusk Logo

Dusk from Laravel is another browser automation tool. It can be used standalone (with chromedriver) or with Selenium. It has an easy to use API and covers all the testing possibilities like waiting for elements, file upload, mouse control, etc. Here’s a simple example:

class LanguagesControllerTest extends DuskTestCase
{
    public function testCreate()
    {
        $this->browse(function (Browser $browser) {
            $user = $this->getAdminUser();

            $browser->loginAs($user)
                ->visit('/panel/core/languages')
                ->click('#add')
                ->assertPathIs('/panel/core/languages/create')
                ->type('name', 'Arabic')
                ->select('direction', 'rtl')
                ->press('Submit')
                ->assertSee('Language: Arabic')
                ->assertSee('ar')
                ->assertSee('rtl')
                ->assertSee('Language created');
        });
    }
}

You can check this tutorial to get started on testing with Dusk.

Kahlan

Kahlan logo

Kahlan is a full-featured Unit & BDD test framework which uses a describe-it syntax.

describe("Positive Expectation", function() {
    it("expects that 5 > 4", function() {
        expect(5)->toBeGreaterThan(4);
    });
});

You can see from the syntax above that it’s similar to Behat tests. Kahlan supports stubbing and mocking out of the box with no dependencies, code coverage, reporting, etc.

it("makes a instance double with a parent class", function() {
    $double = Double::instance(['extends' => 'Kahlan\Util\Text']);

    expect(is_object($double))->toBe(true);
    expect(get_parent_class($double))->toBe('Kahlan\Util\Text');
});

php_testability

The last package I want mention here is PHP Testability. It’s a static analysis tool that tells you about testability issues in your program and generates a detailed report.

The package doesn’t currently have a tagged release that you can rely on, but you can safely use it in development. You can install it via Composer:

composer require edsonmedina/php_testability "dev-master"

Then run it like this:

vendor/bin/testability . -x vendor

Continuous integration (CI) Services

An important part in delivering code when working with teams is the ability to automatically check code before it’s merged to the official repo of the project. Most available CI services/tools provide the ability to test code on different platforms and configurations to make sure your code is safe to merge.

Thumbs up and down in one

There are a lot of services out there which offer good pricing tiers, but you can use open source tools as well:

Conclusion

Adopting the testing culture is hard, but it grows slowly with practice. If you care about your code, you should test it! The above tools and resources will help you get started quickly.

What is your experience with the tools mentioned above? Did we miss something? Let us know and we’ll do our best to expand the list with essential tools!

Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.