Key Takeaways
- PHPUnit, Cucumber, Atoum, Selenium, Dusk, Kahlan, and PHP Testability are highlighted as essential PHP Quality Assurance tools, each offering unique features for testing and improving code quality.
- Continuous integration (CI) services like PHPCI, TravisCI, SemaphoreCI, and Jenkins are crucial for team projects as they automatically check code before it’s merged to the official project repository.
- The adoption of a testing culture, though challenging, is crucial for code quality. Utilizing the mentioned tools can help developers get started with testing and assure quality in their PHP coding practices.
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.
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.
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.
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 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.
Dusk
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 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.
There are a lot of services out there which offer good pricing tiers, but you can use open source tools as well:
- PHPCI: (open source) getting started article.
- TravisCI: (free for open source projects) getting started article.
- SemaphoreCI: (free for open source projects) getting started article.
- Jenkins: getting started article.
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!
Frequently Asked Questions (FAQs) about PHP Quality Assurance Tools
What are the key features to look for in a PHP Quality Assurance tool?
When selecting a PHP Quality Assurance tool, there are several key features to consider. Firstly, the tool should be able to perform static code analysis, which involves checking your source code for potential errors, bugs, or violations of coding standards without executing the program. Secondly, the tool should provide a unit testing framework, which allows you to test individual units of source code to determine whether they are fit for use. Other important features include code coverage analysis, which measures the extent to which your code is tested, and continuous integration, which involves regularly merging all developer working copies to a shared mainline.
How can PHP Quality Assurance tools improve the efficiency of my development process?
PHP Quality Assurance tools can significantly improve the efficiency of your development process by automating many tasks that would otherwise be time-consuming and error-prone. For example, static code analysis can automatically detect potential errors and violations of coding standards, saving you the trouble of manually reviewing your code. Similarly, unit testing frameworks can automatically test individual units of source code, ensuring that they function correctly before they are integrated into the larger system. This can save you a great deal of time and effort in debugging and troubleshooting.
Are there any open-source PHP Quality Assurance tools available?
Yes, there are many open-source PHP Quality Assurance tools available. These include PHP_CodeSniffer, which checks your code for violations of coding standards; PHPUnit, a unit testing framework; and PHPMD, which looks for potential problems in your code such as bugs, suboptimal code, and overcomplicated expressions. These tools are free to use and can be customized to suit your specific needs.
How do I choose the right PHP Quality Assurance tool for my project?
Choosing the right PHP Quality Assurance tool for your project depends on several factors. These include the size and complexity of your project, your team’s familiarity with the tool, the tool’s compatibility with your development environment, and the specific features you need. It’s a good idea to try out several tools and see which one best meets your needs.
Can PHP Quality Assurance tools help me write better code?
Absolutely. PHP Quality Assurance tools can help you write better code by enforcing good coding practices, detecting potential errors and bugs, and providing feedback on the quality of your code. By using these tools, you can ensure that your code is clean, efficient, and maintainable.
How do PHP Quality Assurance tools integrate with other development tools?
Many PHP Quality Assurance tools can integrate with other development tools such as IDEs, version control systems, and continuous integration servers. This allows you to seamlessly incorporate quality assurance into your development process, making it easier to maintain high standards of code quality.
What are some common challenges when using PHP Quality Assurance tools, and how can I overcome them?
Some common challenges when using PHP Quality Assurance tools include learning how to use the tool, configuring the tool to suit your specific needs, and dealing with false positives. To overcome these challenges, it’s important to invest time in learning the tool, reading the documentation, and seeking help from the community if needed.
Can I use multiple PHP Quality Assurance tools in the same project?
Yes, you can use multiple PHP Quality Assurance tools in the same project. In fact, it’s often beneficial to do so, as different tools may excel in different areas. For example, you might use one tool for static code analysis, another for unit testing, and another for code coverage analysis.
How often should I run PHP Quality Assurance tools on my code?
Ideally, you should run PHP Quality Assurance tools on your code every time you make changes. This allows you to catch potential errors and bugs as early as possible, making them easier to fix. Many tools can be configured to run automatically whenever you save your code or commit changes to your version control system.
What is the future of PHP Quality Assurance tools?
The future of PHP Quality Assurance tools looks bright, with many exciting developments on the horizon. These include advancements in machine learning and artificial intelligence, which could be used to automatically detect complex bugs and suggest fixes. We can also expect to see more integration with other development tools, making it even easier to incorporate quality assurance into your development process.
Younes is a freelance web developer, technical writer and a blogger from Morocco. He's worked with JAVA, J2EE, JavaScript, etc., but his language of choice is PHP. You can learn more about him on his website.