8 Must Have PHP Quality Assurance Tools
The site is a simple list of interesting tools that can help you properly test your application, while also analyzing it and producing interesting data for you to look at. This post will be little more than a re-iteration of some of those tools with added information, use cases and advice about them – however, we’re open to expanding this list with other QA tools you throw our way that get plenty of votes in the comments below or seem interesting and/or useful enough.
1. PHPUnit
Mr. Bergmann’s own PHPUnit is, unsurprisingly, present on the list. PHPUnit is the most widely used and fully featured unit testing tool which we’ve covered extensively before – I believe it needs no further introduction.PHPUnit can be installed globally, by running
composer global require "phpunit/phpunit=4.1.*"
.2. Behat
Behat, a BDD framework, is a framework which will be covered in great detail on our channel soon, but in a nutshell, it allows you to write highly readable feature tests – tests that can be easily understood by even non-technical people. They’re actual stories you write, in this form, as per their site:
1
2
3
4
5
6
7
8
9
10
| Scenario: List 2 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 """ |
1
2
3
4
5
| /** @Given /^I have a file named "([^"]*)"$/ */ public function iHaveAFileNamed( $file ) { touch( $file ); } |
These “tests” are then writeable by non technical people with minimal training, and can in some cases decrease the management-development misunderstanding gap. Which tests should you write though? BDD or TDD?
BDD vs TDD
There are several guides, videos and articles on the comparison of BDD and TDD, but what it boils down to it – “BDD is TDD done right”.BDD is TDD done rightBDD tests your app in a more readable, and contextually specific manner. What’s more, the two aren’t mutually exclusive – BDD can use common assertion tools like those from PHPUnit. It’s very important to note the following distinction, however:
There is no code coverage in BDD!How do you know what methods you’ve tested, and which need to be covered still? Put simply, when doing only BDD, you don’t (unless you use third party code coverage libs). But that’s just it – TDD is code oriented, while BDD is client oriented. TDD tests if your methods provide the asserted outcome, while BDD tests the functionality of your app – in other words, BDD tests if your app does what it should be doing, and considers a test passed if it doesn’t throw an exception. In BDD, some of your functions can remain untested because they’re simply irrelevant to the end result – the whole is greater than the sum of its parts – while in TDD, only the parts exist, and there is no whole. BDD is not for control freaks.
If you can, I encourage you to write both. If you can’t, I encourage you to go with BDD. It’ll be far more valuable in the long run.
Behat can be installed globally via
composer global require behat/behat='~3.0.6'
.3. vfsStream
Further improving on the topic of testing, there’s vfsStream:… a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system.It’s role is simple – fake a file system for mocking in your tests. The example it offers on the wiki page is self-explanatory, but perhaps the most noteworthy of features is its ability to “copy” a virtual directory structure from the actual file system. That way, you can run the test from the example on the virtual copy and treat it as the real deal (permissions are copied, too, as are files that don’t exceed a pre-set limit), without actually polluting the real file system and risk post-test-fail leftovers the original example warns against in the anti-teardown argument.
4. PHPLOC
Another of Mr. Bergmann’s own tools, PHPLOC (LOC = lines of code) is a tool that measures the size of your project in lines of code, number of files, number of classes, and so on. The tool can walk through Git revisions, too, and generate retroactive reports in CSV format.The tool can be globally installed via Composer by running
composer global require 'phploc/phploc=*'
and then becomes accessible to any project from any location. All you need to do is run phploc FOLDER
where “FOLDER” is the folder you’d like to analyze.The usefulness of this tool is questionable at best, but it’s a nice bragging tool and I can see it generating some very interesting graphs in the sidebar of a PHP project’s Github page. Further documentation is available on the project’s Github page.
The list also mentioned PHP_Depend, though I see it as a somewhat outdated and rather tackily designed competitor to PHPLOC, so I won’t be mentioning it here – in part also because the tool we mention next makes some under-the-radar use of it.
5. PHP Mess Detector
PHP Mess Detector will, when installed, pull in PHP_Depend as well and heavily use its generated metrics to generate its own reports. PHPMD actually looks for messy code: possible bugs, suboptimal code, overcomplicated methods, unused expressions and so on.PHPMD can also be installed globally via Composer by running
composer global require 'phpmd/phpmd=*'
PHPMD works through Rules – specific sets of instructions on what to look for in source code. There’s a wide variety of prepared ones and users can write their own.
More detailed PHPMD posts are coming soon.
6. PHP CodeSniffer
Similar to PHPMD, CodeSniffer sniffs through your code and detects violations in syntax. Unlike PHPMD, CS also covers CSS and JS.The usual method of installation was through PEAR, though since PEAR is (luckily) on its way out, you can also install it globally through Composer:
composer global require 'squizlabs/php_codesniffer=*'
.Usage is fairly simple, as described by their Github wiki, and we’ll be publishing detailed CS integration tutorials soon, too.
7. + 8. DCD + CPD
Two more of Mr. Bergmann’s tools, Dead Code Detector and Copy Paste Detector, test for unused code (methods and functions that were declared but are never used in a project) and for duplicate code respectively.These absolutely essential tools make sure your code is DRY, clean and lean. Like some of the previously mentioned tools, these too can be installed globally via Composer:
composer global require 'sebastian/phpcpd=*'
composer global require 'sebastian/phpdcd=*'
Convenience Bundle
Executing the following set of commands will install Composer globally, and then all the tools we mentioned above, except vfsStream, which you should require manually in yourcomposer.json
file:
1
2
| composer global require 'sebastian/phpcpd=*' composer global require 'sebastian/phpdcd=*' |
1
2
3
4
5
6
7
| composer global require "phpunit/phpunit=4.1.*" composer global require behat/behat='~3.0.6' composer global require 'phploc/phploc=*' composer global require 'phpmd/phpmd=*' composer global require 'squizlabs/php_codesniffer=*' composer global require 'sebastian/phpcpd=*' composer global require 'sebastian/phpdcd=*' |
Conclusion
In this post, we covered a part of the list from PHP QA Tools, focusing on actual QA tools only, and gave them some more context. Finally, we created a convenience shell provision file for your Vagrant boxes, so you can install all the QA tools globally at once.While you probably won’t be allowed to use all these at work where speed is more important than quality, you should definitely give them a go in your passion projects – your ideas deserve them.
Do you use any of these? If not, why? Think we’re missing some? Let us know and we’ll do our best to expand the list with essential tools – but make sure they’re actual QA tools!
Bruno
is a professional web developer from Croatia with Master's degrees in
Computer Science and English Language and Literature. After having left
his position as lead developer for a large online open access publisher,
he now works as the PHP editor for Sitepoint and on various freelance
projects. When picking them, he makes sure they all involve new and
exciting web technologies. In his free time, he writes tutorials on his blog and stalks Google's job vacancy boards.
http://www.bitfalls.com/
0 comments: