By James Titcumb
This article was published in the September 2017 issue of php[architect] magazine. You can see how it looks as part of the magazine in the Free Article PDF. Check out our subscription options to become one today. We have digital and print options starting at $4.99 per month.

There is a new movement in the PHP community! Well, it isn’t exactly new, but an event called PHP TestFest is back. PHP TestFest is a global event organized by the PHP community at large, where an effort is made each time to increase the test coverage of the PHP engine itself. Around eight or nine years ago, user groups around the world coordinated to organize events spanning a few months each year, with some groups continuing events annually for several years after.PHP itself is a massive project with over 200,000 lines of code, used by possibly millions of developers. To ensure it doesn’t break in unexpected ways there needs to be a good coverage of tests. Now, before you run off, the thing that puts many people off is thinking they need to know C to contribute, but there’s some great news: the tests for PHP are written in PHP—so you don’t need to know C! PHP TestFest encourages user groups, conferences, or even just an ad-hoc gathering of developers to join forces to write these tests for the engine. PHP TestFest runs from September to December this year, so there’s a good window of opportunity to get involved. If you attend or organize a local user group, or want to plan one of these events, it’s easy to get started. First, you’ll need to set a date for your event. Agree on a date where as many interested folks can attend as possible—that might be on an evening during the week, or even during the weekend. If you’re part of a PHP user group, it might be a cool change to replace your regular monthly meet up with a PHP TestFest event. If you’re unsure, consider sending out a poll to anyone interested to see what works best for them. For the event, you’ll need tables for laptops and so on, as well as a good, stable Wi-Fi connection—and don’t forget you’ll need more bandwidth the more attendees you have.
Writing Tests
So how does one actually write these tests? In brief, it’s a text file with a few sections:TEST, FILE, and an EXPECT or EXPECTF. The TEST section is the description of the test, and it helps to explain what behavior you’re trying to test here. The FILE section contains the PHP code that will be run. Finally, the EXPECT or EXPECTF section is for the expected output; the latter, EXPECTF, allowing for formatting marks. For example:
--TEST--
Trivial "Hello World" test
--FILE--
<?php echo "Hello world" ?>
--EXPECT--
Hello World
EXPECTF works proves very useful. For example, if you are writing out a file path where the document root will be different on each system, you can replace the root path with %s:
--TEST--
Example using EXPECTF
--FILE--
<?php
echo sys_get_temp_dir() . "/foo\n";
?>
--EXPECTF--
%s/foo
phpqa run path/to/my/test.phpt all
Biography
James is a consultant, trainer and developer at Roave. He is a prolific contributor to various open source projects and is a Zend Certified Engineer. He also founded the UK based PHP Hampshire user group and PHP South Coast conference. @asgrim