Overview of PHP Quality Assurance Tools: A Practical Guide to Improving the Quality of PHP Code
This article highlights key PHP quality assurance tools such as PHPUnit, Cucumber, Atoum, Selenium, Dusk, Kahlan and PHP Testability, each providing unique testing and code quality improvement capabilities. Additionally, continuous integration (CI) services such as PHPCI, TravisCI, SemaphoreCI, and Jenkins are critical for team projects because they automatically check the code before it is merged into the official project repository.
While building a test culture is challenging, it is crucial to code quality. Using the above tools can help developers get started with testing and ensure the quality of their PHP coding practices.
(This popular article was updated on June 30, 2017 to include the latest technologies and tools.)
To deliver high-quality code, we must consider testing when encoding (if not test-driven development (TDD). However, given the wide variety of PHP testing tools, it is difficult to make a choice! Exploring PHP is a fun adventure, but it’s hard to form a toolbox that won’t be too heavy!
This article will focus on the most popular testing tools and has been updated to reflect the current status of the quality assurance tools in 2017.
Untested code is the code in question.
PHPUnit is the preferred testing framework for PHP. It was created in 2004 by Sebastian Bergmann and currently has version 6 and requires PHP 7.
We have a lot of tutorials about it coming soon.
Cucumber is a framework for creating acceptance tests based on specifications. It is known for its descriptively generated texts that can be read like normal English. The official PHP implementation of Cucumber is Behat.
We have a tutorial on getting started on SitePoint here. The following examples excerpted from the documentation illustrate well how these desired expressions are expressed.
<code>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 """</code>
Atoum is another unit testing framework for PHP. It is a standalone package that you can install via GitHub, Composer, or PHAR executables.
Atoum test is very readable, with clear method names and link expressions.
<code>$this->integer($classInstance->myMethod()) ->isEqualTo(10); $this->string($classInstance->myMethod()) ->contains("Something heppened"); </code>
If you want to learn more about using Atoum for PHP unit testing, you can read this tutorial.
Selenium is a tool for automated browser testing (integration and acceptance testing). It converts the tests into browser API commands and asserts the expected results. It supports most available browsers.
We can use extensions to use Selenium with PHPUnit.
<code>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 """</code>
This is a simple example:
<code>$this->integer($classInstance->myMethod()) ->isEqualTo(10); $this->string($classInstance->myMethod()) ->contains("Something heppened"); </code>
If you want to learn more about testing with PHPUnit and Selenium, you can read this series of articles.
Laravel's Dusk is another browser automation tool. It can be used independently (using chromedriver) or in conjunction with Selenium. It has an easy-to-use API that covers all testing possibilities such as waiting for elements, file uploads, mouse controls, and more. Here is a simple example:
<code>composer require --dev phpunit/phpunit composer require --dev phpunit/phpunit-selenium </code>
You can check this tutorial to get started with Dusk for testing.
Kahlan is a fully functional unit and BDD testing framework that uses describe-it syntax.
<code>class UserSubscriptionTest extends PHPUnit_Extensions_Selenium2TestCase { public function testFormSubmissionWithUsername() { $this->byName('username')->value('name'); $this->byId('subscriptionForm')->submit(); } } </code>
As can be seen from the above syntax, it is similar to the Behat test. Kahlan supports out-of-the-box stubs and simulations, without dependencies, code coverage, reporting, etc.
<code>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'); }); } } </code>
The last package to be mentioned is PHP Testability. It is a static analysis tool that tells you about testability issues in your program and generates detailed reports.
The package currently does not have a tagged version that you can rely on, but you can use it safely in development. You can install it through Composer:
<code>describe("Positive Expectation", function() { it("expects that 5 > 4", function() { expect(5)->toBeGreaterThan(4); }); }); </code>
Then run it like this:
<code>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'); }); </code>
A important part of when working with a team to deliver code is the ability to automatically check the code before merging it into the official repository of the project. Most of the available CI services/tools are able to test code on different platforms and configurations to ensure that your code can be safely merged.
There are many services that offer good price ratings, but you can also use open source tools:
Building a test culture is difficult, but it will grow slowly with practice. If you care about your code, you should test it! The above tools and resources will help you get started quickly.
How is your experience with the above tools? Have we missed something? Please let us know that we will do our best to expand the list with the necessary tools!
When choosing a PHP quality assurance tool, several key features need to be considered. First, the tool should be able to perform static code analysis, which involves checking the source code for potential errors, bugs, or violations of encoding standards without executing a program. Second, the tool should provide a unit testing framework that allows you to test individual units of the source code to determine whether they are suitable for use. Other important features include code coverage analysis (measure the degree of code testing) and continuous integration (regularly merge all developers’ working copies onto the shared mainline).
PHP quality assurance tools can significantly increase the efficiency of the development process by automating many otherwise time-consuming and error-prone tasks. For example, static code analysis can automatically detect potential errors and violations of coding standards, eliminating the hassle of manually checking your code. Likewise, the unit testing framework can automatically test individual units of the source code, ensuring that they can function properly before being integrated into a larger system. This can save you a lot of time and effort for debugging and troubleshooting.
Yes, there are many open source PHP quality assurance tools available. These include PHP_CodeSniffer (checking for encoding standards violations in the code); PHPUnit (unit testing framework); and PHPMD (find potential problems in the code such as bugs, suboptimal code, and overly complex expressions). These tools are free to use and can be customized to your specific needs.
(The following FAQ answer is similarly rewritten, keeping the original meaning unchanged and adjusting the language style to make it smoother and more natural.)
The above is the detailed content of 8 Must Have PHP Quality Assurance Tools. For more information, please follow other related articles on the PHP Chinese website!